Prexx Prexx - 6 months ago 26
Java Question

set ImageView src via ListAdapter

Hey there,
i have one question..

i try to write an android app with java.

i have a hashmaplist(1), saved in a list(2).
to show the list(2) entrys(those, who saved in hashmap(1)), i use a ListAdapter object.

everything works fine, except of the following:
one of the entrys, saved in the hashmaplist(1), is "country", like 'at', 'de', 'gb', ...
now i want to change a pics' source, that was created on the layout, depends on the country entry.

here some code..

save entrys and show it with a ListAdapter object:

for (int i = 0; i < itemArray.length(); ++i) {
JSONObject rec = itemArray.getJSONObject(i);

//save entrys in hashmap
map = new HashMap<String, String>();
map.put("name", rec.getString("name"));
map.put("country", rec.getString("country"));


//save hashmap entry in list
mylist.add(map);
}

ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.listitem,
new String[] {"name"}, new int[] {R.id.area});

listview.setAdapter(mSchedule);


Now the Layout XML:

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

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

<TextView
android:id="@+id/area"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>

</LinearLayout>


Any ideas?

Answer

you have to create your own view binder like

class CustomViewBinder extends SimplerAdapter.ViewBinder
{
            @Override
    public boolean setViewValue(View view, Object data,
            String textRepresentation) {
        int id=view.getId();
            String country=(String)data; 
                switch(id)
                {
                  case R.id.country:
                            if(country.equals("us")
                                setYourImage();
                   .....

            }
        }
}

and in your activity use your simpleAdapter as

        SimplerAdapter sa=new SimpleAdapter(this, mylist, R.layout.listitem,
                new String[] {"name","country"}, new int[] {R.id.area,r.id.country});
        sa.setViewBinder(new CustomViewBinder());

Hope this will help u