VMFCsoul VMFCsoul - 5 months ago 27
Android Question

Android dynamically set ImageView in a list

I have a list with the following layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/outName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />

<TextView
android:id="@+id/outSurname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />

<TextView
android:id="@+id/outAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />

<TextView
android:id="@+id/outPhone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/outPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


I retrieve my fields from a BD in JSON format using an Async Task, and I fill the list in a onPostExecute function like so:

// Updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {

// Updating parsed JSON data into ListView
ListAdapter adapter = new SimpleAdapter(
MyActivity.this, MyList,
R.layout.my_layout, new String[] { “name”, “surname”, “address”, "phone" },
new int[] { R.id.outName, R.id.outSurname, R.id.outAddress, R.id.outPhone });
// Updating ListView
setListAdapter(adapter);
}
});

}

}


where MyList is an ArrayList:

ArrayList<HashMap<String, String>> MyList;
MyList = new ArrayList<HashMap<String, String>>();


Every field of my results JSONArray is composed by “name”, “surname”, “address”, "phone", and "photo".
"photo" is a numeric field with 4 possible values ( 0, 1, 2, 3 ) where every values refers to specific image I have on a my drawable folder.
How can I set dinamically the image of ImageView depending of this value?

Answer

You need to write an adapter. make your own based on this.

 public class ListAdapter extends BaseAdapter{

        Context context;
        ArrayList<HashMap<String, String>> myList;

        public BankAdapter(Context context,ArrayList<HashMap<String, String>> myList){
            this.context = context;
    this.myList=myList;
        }

        static class ViewHolder {
    // Include Number of reqd views.
             private TextView item;
             private ImageView pic;


            }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.length;
        }

        @Override
        public Object getItem(int pos) {
            // TODO Auto-generated method stub
            return items[pos];
        }

        @Override
        public long getItemId(int pos) {
            // TODO Auto-generated method stub
            String id = items[pos].getId();
            return Long.parseLong(id);
        }


        @Override
        public View getView(int position, View view, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder mViewHolder = null;
            if(view == null){
                mViewHolder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater)context.getSystemService
                        (Activity.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.listrow,parent,false);
                mViewHolder.item = (TextView)view.findViewById(R.id.items_txt);
                mViewHolder.pic=(ImageView)view.findViewById(R.id.pics_img)
                view.setTag(mViewHolder);
            }

            else{
                mViewHolder = (ViewHolder)view.getTag();
            }


            mViewHolder.item.setText(myList.get(position).getKey());
    if(condition){
    //update Image with correct item.
        mViewHolder.pic.setDrawable(//getitemfromList);
    }
            return view;
        }

    }
Comments