Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ImageViews source dynamically from ArrayList<String> with Drawable image name?

I have a ListView with an ImageView, a TextView and another ImageView in each row.

The code below is the xml of my ListView row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#FFF">
<ImageView
android:id="@+id/left"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:src="@drawable/ic_default"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_weight="1"
android:textSize="16sp" />
<ImageView
android:id="@+id/right"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/ic_cena_play"
android:layout_marginRight="0dp"
android:layout_marginLeft="10dp"
android:clickable="true"
android:background="@drawable/mybuttonplay" />
</LinearLayout>

I have an ArrayList < String > with the names of Drawables I want to put in the first ImageView of the rows. I want to change the ImageViews source "ic_default" to the ones which have the name in ArrayList, dinamically.

In my Activity, inside onCreate(), I am using an Adapter, as the code below:

ArrayList<String> items = new ArrayList();
items.add("Star");
items.add("Moon");
items.add("Dog");
items.add("Bird");
items.add("Tree");
ArrayList<String> itemsimg = new ArrayList();
itemsimg.add("ic_star");
itemsimg.add("ic_moon");
itemsimg.add("ic_dog");
itemsimg.add("ic_bird");
itemsimg.add("ic_tree");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.scenesrow, R.id.text, items) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row =  super.getView(position, convertView, parent);
            ImageView left = (ImageView) row.findViewById(R.id.left);
            //here I want to change the resource
            //Like if I had a function called getRosourceId(). DOES IT EXIST?!
            int x = getResourceId( itemsimg.get(position) );
            left.setImageResource(x);
            View right = row.findViewById(R.id.right);
            right.setTag(position);
            right.setOnClickListener(ScenesActivity.this);
            return row;
        }
    };

Updated

Thanks for answers! Now I can change the ImageView source dinamically (but not yet from the ArrayList < String >). The setImageResource() method was not available before because I had a View. With the cast to ImageView you all suggested, big part of the problem was okay. But the answers do not cover the dinamically thing of the "name of the resource".

The last problem now is the left.setImageResource(resId); method asks for an ID from Resource, and I have a String. How to do it?

And sorry, other code version had some mistakes. Now it is fixed. I think the last problem is clear now.

like image 412
Sarah Sakamoto Avatar asked Jan 13 '14 11:01

Sarah Sakamoto


2 Answers

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.scenesrow, R.id.text, items) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
        View row =  super.getView(position, convertView, parent);
        ImageView left = (ImageView)row.findViewById(R.id.left);
        left.setOnClickListener(ScenesActivity.this);
        ImageView right = (ImageView)row.findViewById(R.id.right);
        right.setTag(position);

       right.setImageResource(itemsimg.get(position));
       left.setImageResource(itemsimg.get(position));

       right.setOnClickListener(ScenesActivity.this);
       return row;
    }

change itemsimg with:

ArrayList<Integer> itemsimg = new ArrayList<Integer>();
itemsimg.add(R.drawable.ic_star);
itemsimg.add(R.drawable.ic_moon);
itemsimg.add(R.drawable.ic_dog);
itemsimg.add(R.drawable.ic_bird);
itemsimg.add(R.drawable.ic_tree);

for getting drawableId with String you can do this:

Context context = imageView.getContext();
int id = context.getResources().getIdentifier("ic_tree", "drawable", context.getPackageName());
imageView.setImageResource(id);

It's not efficient, but it works to look up occasional resources.

like image 158
Shayan Pourvatan Avatar answered Oct 04 '22 11:10

Shayan Pourvatan


Try this::

right.setImageResource(R.drawable.addphoto);/*select a picture from drawable*/

same for left also

left.setImageResource(R.drawable.addphoto);/*select a picture from drawable*/

After Discussion

       ArrayList<String> items = new ArrayList();
    items.add("ic_star");
    items.add("ic_moon");
    items.add("ic_dog");
    items.add("ic_bird");
    items.add("ic_tree");
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.scenesrow, R.id.text, items) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View row =  super.getView(position, convertView, parent);
/*View : not required to declare everytimes*/
                ImageView left = row.findViewById(R.id.left);
                left.setOnClickListener(ScenesActivity.this);
                ImageView right = row.findViewById(R.id.right);
                right.setTag(position);
                right.setOnClickListener(ScenesActivity.this);
                right.setImageResource(R.drawable.addphoto);/*select a picture from drawable*/
                left.setImageResource(R.drawable.addphoto);/*select a picture from drawable*/

                return row;
            }
        };
like image 27
Satyaki Mukherjee Avatar answered Oct 04 '22 13:10

Satyaki Mukherjee