I am very new to Android. I am working with a list view which uses a custom list_v.xml to house x2 textviews. I have also included a imageView which I would like to update through the activity.
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:maxHeight="40sp"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:maxHeight="35sp"
android:textColor="#FFFFFF" />
I have updated the textviews via Json using this simple adapter concept but I would like to set the imageView for each row the same. I could hardcode the drawable to the xml above but I would like to reuse this view and update the image for other views if that makes sense.
Heres my activity class where I update the textViews but how do I programatically update the imageView to use the same image for all rows.
The imageView src will not come from my Json request it will be a drawable in the package.
@Override
protected void onPostExecute(JSONArray json) {
pDialog.dismiss();
try {
// Getting JSON Array from URL
for(int i = 0; i < json.length(); i++){
JSONObject c = json.getJSONObject(i);
// Storing JSON item in a Variable
String cat = "Category: " + c.getString(TAG_Cat);
String title = c.getString(TAG_Title);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_Cat, cat);
map.put(TAG_Title, title);
oslist.add(map);
listView = (ListView)findViewById(R.id.listView);
ListAdapter adapter = new SimpleAdapter(TopTipsActivity.this, oslist,
R.layout.list_v,
new String[] { TAG_Cat,TAG_Title},
new int[] {R.id.title,R.id.body});
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(TopTipsActivity.this, "You Clicked at "+oslist.get(+position).get("Title"), Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
I have tried to use
ImageView image = (ImageView) listView.findViewById(R.id.icon);
image.setBackgroundResource(R.drawable.tip_selected);
I am guessing I somehow need to reference list_v not listView.findViewById. I am probably missing the point completely.
That's so simple, just edit your HashMap
to
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_Cat, cat);
map.put(TAG_Title, title);
// the following line will add the default image to your map
map.put("Image", String.valueOf(R.drawable.ic_launcher));
and edit the SimpleAdapter
to
ListAdapter adapter = new SimpleAdapter(TopTipsActivity.this, oslist,
R.layout.list_v,
new String[] { TAG_Cat, TAG_Title, "Image"},
new int[] {R.id.title, R.id.body, R.id.icon});