0

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.

  • You should take a look a this response http://stackoverflow.com/a/8166802/5215998 – Kassisdion Nov 06 '15 at 01:21
  • Create a subclass of `SimpleAdapter`, override `getView()`, chain to the superclass implementation to get all the existing behavior, modify the background to suit, and return the modified row `View`. Then, use your subclass of `SimpleAdapter` in your `ListView`. – CommonsWare Nov 06 '15 at 01:22
  • declare images like this Integer[] imgid={R.drawable.pic1,R.drawable.pic2}; and fetch your images in custom adapter with this statement layout.setBackgroundResource(imgid[position]); // layout = parent layout of your list_item – Nisarg Nov 06 '15 at 04:22
  • Thanks man .. It worked, Posting the final code for custom adapter in answer. – user3834795 Nov 06 '15 at 05:05

1 Answers1

0

Here is the final code for the custom adaptor implementing simpleadapter

public class CustomAdapter extends SimpleAdapter{


    private ArrayList<HashMap<String, String>> results;
    private final int[] bcgImage;
    public CustomAdapter(Context context,
                           ArrayList<HashMap<String, String>> data, int resource,
                           String[] from, int[] to, int[] bcgImage) {
        super(context, data, resource, from, to);

        this.bcgImage = bcgImage;
        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(bcgImage[position]);
        TextView name = (TextView) v.findViewById(R.id.pname);
        name.setText(results.get(position).get(Config.TAG_PCKG_NAME));
        TextView price = (TextView) v.findViewById(R.id.price);
        price.setText(results.get(position).get(Config.TAG_PCKG_PRICE));

        return v;
    }

}