I have populated an Arraylist as follows:
JSONObject jsonObject = null;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
jsonObject = new JSONObject(JSON_STRING);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
String rs = "Rs. ";
String name;
for (int i = 0; i < result.length(); i++) {
JSONObject jo = result.getJSONObject(i);
String pname = jo.getString(Config.TAG_PCKG_NAME);
String price = rs + jo.getString(Config.TAG_PCKG_PRICE);
String Tag= "debugger2";
Log.i(Tag, pname);
HashMap<String, String> employees = new HashMap<>();
employees.put(Config.TAG_PCKG_NAME, pname);
employees.put(Config.TAG_PCKG_PRICE, price);
list.add(employees);
}
} catch (JSONException e) {
e.printStackTrace();
}
now I am using the an listadapter to populate the listview as follows:
ListAdapter adapter = new SimpleAdapter(
test.this, list, R.layout.list_item,
new String[]{Config.TAG_PCKG_NAME, Config.TAG_PCKG_PRICE},
new int[]{R.id.pname, R.id.price});
listView.setAdapter(adapter);
I want to now have different background images for each of these list items. I have saved the images (6 in total as there will be 6 items in the list) in drawable. I tried doing a setbackground(R.drawable.filename) thing with having all the file names related to the variable i in the for loop but that is not the way to do it. Can you suggest an alternative way. `
Edit1: This is what I have implemented now:
public class CustomAdapter extends SimpleAdapter{
private ArrayList<HashMap<String, String>> results;
private Context context;
public CustomAdapter(Context context,
ArrayList<HashMap<String, String>> data, int resource,
String[] from, int[] to,int []image) {
super(context, data, resource, from, to);
this.results = data;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
v.setBackgroundResource(position);
return v;
}
}
Question:
I don't know how will the setBackgroundResource know which array to point to; for this I added int []image in the customadapter deceleration but still not sure how this will work.