Sam
Sam

Reputation: 11

How to use different backgrounds for ListView entries?

I've got a ListView and I'm trying to use different backgrounds for the entries, how can I specify which entry should get which background?

What I'm doing is that I load the entries in the code, here:

    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, listEntries));

So how can I specify a layout for every entry?

Kind Regards Sam

Upvotes: 0

Views: 236

Answers (1)

arbuzz
arbuzz

Reputation: 396

Write you own adapter that extends ArrayAdapter and override public View getView(int position, View convertView, ViewGroup parent) method like this

View view;
String myString = getItem(position);
if (convertView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        view = inflater.inflate(R.layout.list_item, null);            
    } else {
        view = convertView;
    }
// here you can change background of view
return view;

Upvotes: 1

Related Questions