简体   繁体   中英

Android List view coloring items

I have JSON with 2 fields - Name and gender. I want to color each name to blue if gender is male and to pink if it's a female.

Here is my code

ArrayList feedList1 = new ArrayList<HashMap<String, Object>>();


for (int i = 0; i < data.length(); i++) {
    HashMap<String, Object> hm;
    hm = new HashMap<String, Object>();

    hm.put("id", data.getJSONObject(i).getString("id").toString());
    hm.put("gender", data.getJSONObject(i).getString("gender").toString());
    hm.put("name", data.getJSONObject(i).getString("name").toString());
    feedList1.add(hm);
    SimpleAdapter adapter = new SimpleAdapter(feedList.getContext(), feedList1, R.layout.list,
            new String[] {"id","name"}, new int[] { R.id.personId, R.id.personName }){
        @Override
        public void setViewText(TextView v, String text) {
            super.setViewText(v, text);
            if (v.getId() == R.id.personName) {

                if (/* how to get gender of this person*/) 
                    v.setTextColor(R.color.female); 
                else
                    v.setTextColor(R.color.male);
            }
        }

    };

    feedList.setAdapter(adapter);
    feedList.setChoiceMode(feedList.CHOICE_MODE_SINGLE);
}

Tell me please how to add gender to IF statement.

You cannot do so without creating your own custom Adapter, so you could override getView. Check the following code and use CustomAdapter instead of SimpleAdapter

class CustomAdapter extends SimpleAdapter {

    public CustomAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
        super(context, data, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        Map<String, Object> item = (Map<String, Object>) getItem(position);
        if ("male".equals(item.get("gender").toString())) {
            view.setBackgroundColor(R.color.male);
        } else {
            view.setBackgroundColor(R.color.female);
        }

        return view;
    }
}

I think you have to get the String value before using this.. So you have to do this by:

String gender = hm.get("gender");
if (gender.equals("male"))
    dosomething;
else
    doAnotherThing;

I think you also have to make hm as final !!! I hope it's helpful for you!!

You should override getView method instead of setViewText. But if you have a very big wish to use setViewText, you could set name of your object to something like "thomas&man", than you can divide you string to array of strings with divider "&" and second item of array will be you gender. But better for use is override getView method, this is the correct way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM