0

Does anyone know how I could create a for loop(?) for my imageView and textView without adding them in my XML file? I'm not sure if it is possible too..so I would appreciate if someone could help me in this. For your info, I would like to grab data from another activity. eg. If the user click the add to Favourite button, I would display another imageView & textView in this activity of the clothes image and name. The activity will also display the previous items that is added to the page.

XML file:

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/name"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="a" />

</LinearLayout>

Inside my onCreate method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_favourite);

    editButton = (Button)findViewById(R.id.edit);

    image = (ImageView)findViewById(R.id.image);
    name = (TextView)findViewById(R.id.name);


}

Thank you.

angellineeee
  • 3
  • 1
  • 5

2 Answers2

0

I'm not sure of what you want, maybe this?

for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
linear.addView(btn, params);
btn1 = ((Button) findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Toast.makeText(view.getContext(),
                "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                .show();
    }
});
}
Community
  • 1
  • 1
Enrichman
  • 11,157
  • 11
  • 67
  • 101
0

I think what you are going to want to do is in your layout have a ListView, and then you create something that implements ListAdapter (ArrayAdapter is a good choice). In your adapter you can reuse one xml file that would contain your ImageView and TextView.

There are a lot of good tutorials out there on it, Googling "ListView Adapter" will get you pretty far. Here's a good one: http://www.vogella.com/articles/AndroidListView/article.html

Yours is a pretty simple case, so you can use an ArrayAdapter like this:

Activity:

public class MainActivity extends Activity {

    private ArrayAdapter<String> listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>()) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                View v = convertView;
                if (convertView == null)
                {
                    v = super.getView(position, convertView, parent);
                }

                String text = getItem(position);

                TextView textview = ((TextView) v.findViewById(android.R.id.text1));
                textview.setText(text);

                // Now you can set an image by calling
                // textview.setCompoundDrawablesWithIntrinsicBounds(...);

                return v;
            }
        };

        ((ListView) findViewById(R.id.listView)).setAdapter(listAdapter);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            private int i = 0;
            @Override
            public void onClick(View view) {
                listAdapter.add("item number: " + (++i));
                listAdapter.notifyDataSetChanged();
            }
        });
    }

}

Layout activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/listView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            tools:listitem="@layout/listview_row"
            android:layout_above="@+id/button"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"/>

</RelativeLayout>

Alternative:

If you are bent on not using a ListView or for whatever reason you can't use a list view, you can always inflate a layout and add it to another layout in your app. The code below would then replace the code above in the button's on click:

View row = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null); /* null here means it's not attaching/populating a view you already have */
((TextView) row.findViewById(android.R.id.text1)).setText("item number: " + (++i));

((ViewGroup) findViewById(R.id.linearlayout)).addView(row);
findViewById(R.id.linearlayout).invalidate();

Using a ListView here is the right way to go, so this is kind of a last resort.

xbakesx
  • 13,202
  • 6
  • 48
  • 76