开发者

Android setcolour listview row

I have a listview and two textveiws in it for two columns. My goal is to set the colour of e.g. the 5th row of the listview to blue. Details:

config.xml has the layout for the acitivty: buttons and the listview. Part of it:

 <ListView android:id="@+id/ListView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_below="@+id/TextView01">
</ListView>

row.xml defines the two coloumns for the listview:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
    android:id="@+id开发者_如何转开发/linlay0">
 <LinearLayout android:orientation="horizontal"
    android:id="@+id/linlay"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
     <TextView android:id="@+id/left"
         android:layout_width="160px"
         android:layout_height="wrap_content"
         android:textColor="#FF0000"
         android:paddingLeft="5px"/>    
     <TextView android:id="@+id/right"
         android:layout_width="140px"
         android:layout_height="wrap_content"
         android:maxWidth="140px"
         android:singleLine="false"/>
  </LinearLayout>
</LinearLayout>

MainActivity.java is for the management for the activity, as well as the listview. So contentview is set to config.xml:setContentView(R.layout.config);

This is how i upload the listview with data:

lv1=(ListView)findViewById(R.id.ListView01);

        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
        for (int i=0; i<31; i++)
        {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("left1", date[i]);
            map.put("right1", name[i]);
            mylist.add(map);
        }

        simpleAdapter = new SimpleAdapter(this, mylist, R.layout.row,
                new String[] {"left1", "right1"}, new int[] {R.id.left, R.id.right});
lv1.setAdapter(simpleAdapter); 

date[i] and name[i] are arrays declared and uploaded at the beginning of the class.

I am running some queries, comparing arrays and now i want to the set colour of a specific row in the ListView to blue. Like i said contentview is set to config.xml, while the TextViews of the ListView is in row.xml. So TextView txt1 = (TextView) findViewById(R.id.left); is uninterpretable for Eclipse.


I would have solved this by implemented my own adapter, and used the position parameter in the getView() method of the adapter to set the background of the LinearLayout by code.


In your CustomAdapter in getView method use this

if(position==5)
{
convertView.setBackgroundColor(Color.BLUE);
}


simpleAdapter is a system adpater,you can not control it.In my opioion,if you want set color at 5th row,you should implementation any adapter and override the getView or bindView method ,accord the position(row number) you can do anything you want

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜