Android dynamic icon in listview
I have a ListActivity in my application. The list is populated from a parsed http response in the form of an ArrayList of hashmaps.
final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>(); ... SimpleAdapter adapter = new SimpleAdapter(this, LIST, R.layout.item_row, new String[]{"author","title"}, new int[]{R.id.text1,R.id.text2}); setListAdapter(adapter); ... for(Element src : lists){ String title = src.select(); String author = src.select(); HashMap<String,String> temp = new HashMap<String,String>(); temp.put("author", author); temp.put("title", title); LIST.add(temp); }
Where author and title are correctly derived from the parsed http.
In the R.layout.item_row XML file I have defined a drawable for text2:
android:drawableLeft="@drawable/default_icon"
And i would like to be able to change that icon to "new_icon" on a given row if the hashmap contains a certain string.
For instance, within the for loop above, I would add something like:
String status = src.select(); String icon = "default"; if(status.contains("test")){ icon = "should_change"; } temp.put("icon", icon); ...etc
I'm leaving out the specific logic for brevity, but I am able to correctly pass whether the icon should change or not as a string value back to the adapter, however, I am not sure how to then actually change the icon.
I am not experienced with either Java or Android so I am quite likely approaching this backwards.
Any help would be 开发者_开发问答great.
I managed to figure it out.
First, instead of using SimpleAdapter, I added a class that extends SimpleAdapter
public class SpecialAdapter extends SimpleAdapter { public SpecialAdapter(Context context, List> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); // TODO Auto-generated constructor stub } }
and added into it a getView method that checks the value of "icon" and sets the drawable accordingly:
@Override public View getView(int position, View convertView, ViewGroup parent){ View row = convertView; if(row==null){ LayoutInflater inflater=getLayoutInflater(); row=inflater.inflate(R.layout.item_row, parent, false); } TextView text1=(TextView)row.findViewById(R.id.text1); text1.setText(LIST.get(position).get("author")); TextView text2=(TextView)row.findViewById(R.id.text2); text2.setText(LIST.get(position).get("title")); ImageView icon=(ImageView)row.findViewById(R.id.icon); if(LIST.get(position).get("icon") == "should_change"){ icon.setImageResource(R.drawable.new_icon); } else { icon.setImageResource(R.drawable.default_icon); } return row; }
Then within my main class, I replaced the SimpleAdapter instance with SpecialAdapter.
And finally, in item_row.xml, I removed the drawableLeft definition, and added an ImageView with id of "icon" in its place
Thanks! LIST didnt work for me - I had to make it the activity class that called the list, like MyActivity.LIST - Also shortend up with a define for details
HashMap<String, String> details = MyActivity.LIST.get(position);
TextView text1=(TextView)view.findViewById(R.id.text1);
text1.setText(details.get("author"));
精彩评论