How can I put different colors in a ListView

I want to get different colors in each line of the list view

but only one color is displayed.

so how can i do that

ArrayAdapter<String> adapter=new ArrayAdapter<String(this,R.layout.latesthappenings,R.id.LH_Titles,titles);
            setListAdapter(adapter);

I want a different color in each row

tell me how can i

thank

+5
source share
2 answers

You can use anonymous ArrayAdapter, you need to write code insidegetView()

Ship code

ArrayAdapter<String> adapter=new ArrayAdapter<String>(
           this,R.layout.main,R.id.textview1,titles){

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
if(position ==condition){
convertView.setBackgroundResource(R.color.grey);}
else if(second condition){convertView.setBackgroundResource(R.color.something else);}

    return view;};
   };

Another approach is

Why don't you want to customize ListView? You can control the viewing as per your requirement.

Have a Custom ArrayAdapter

Thanks Deepak

+2
source

( ) setBackgroundColor(), .

.

0

All Articles