C Sharper
C Sharper

Reputation: 8646

Listview background color not getting updated

I have following listview:

<ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1" >
    </ListView>

I want to update background color of its first nth columns.

For that i am using:

int numOfMessages=lst.length-lstNew.length;  
ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst);
lm.setAdapter(adpt);
for (int i=1;i<numOfMessages;i++)
{
tv.setText("Welcome " + i);
lm.getChildAt(i).setBackgroundColor(Color.BLUE);
tv.setText("Welcome :  " + i);
}

But there is something wrong with line:

lm.getChildAt(i).setBackgroundColor(Color.BLUE);

Because after that control is not moving further.

Please help me to change the background color of textview dynamicaly.

Upvotes: 2

Views: 569

Answers (3)

Piyush
Piyush

Reputation: 18923

you can get it By:

ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            if(position==2){
            v.setBackgroundColor(Color.BLUE);
            }else if(position==4){
            v.setBackgroundColor(Color.RED);
            } 
            return v;
        }
    };

Something Like this for that row which you want....

Upvotes: 1

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

try this code:

ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst){
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, convertView, parent);
                v.setBackgroundColor(Color.BLUE);
                return v;
            }
        };

Upvotes: 1

user2639866
user2639866

Reputation:

firstly you should convert lm.getChildAt(i) to here specifique type ... Exemple : if lm.getChildAt(i) is a textview write like that:

TextView txtv1 = (TextView)lm.getChildAt(i);
txtv1.setBackgroundColor(Color.BLUE);

Upvotes: 0

Related Questions