用于muticolumn Listview特定行的Android setBackground或文本颜色

时间:2011-08-26 11:42:46

标签: android android-listview

我们如何为特定行设置ListView的背景。

    ListView list = getListView();
     sd = new SimpleAdapter(this, routhPath, R.layout.route_path,new String[] {"routeCode","routeName","outlets" },new int[] { R.id.routeCode,R.id.routeName,R.id.outlets});
     list.setAdapter(sd);
     list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     list.setSelected(true);
     list.setTextFilterEnabled(true);
     list.setItemsCanFocus(true);
     list.setSelectionAfterHeaderView();

     if(isVisitedToVisitHeader()){
         if(defaultRoutePos != 0){
            Log.i("-----" ,"---defaultRoutePos---" + defaultRoutePos +" --count-- "+list.getChildCount());
             list.setItemChecked(defaultRoutePos, true);
            // list.getChildAt(positions).setBackgroundColor(R.color.red);
             //list.getChildAt(defaultRoutePos).setBackgroundColor(R.color.red);
         }
     }

这是我的代码。我使用SimpleAdapter完成了。list.getChildCount()这里给出了{这部分我在onCreate()

请帮帮我......

提前致谢

2 个答案:

答案 0 :(得分:2)

查看自定义列表视图的这个示例,

Example

StackOverflow的答案

StackOverflow Answer

答案 1 :(得分:1)

package com.android.listactivity;

import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] names;

    public MySimpleArrayAdapter(Activity context, String[] names) {
        super(context, R.layout.rowlayout, names);
        this.context = context;
        this.names = names;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.rowlayout, null, true);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(names[position]);
        // Change the icon for Windows and iPhone
        String s = names[position];

//在这里你可以用特定位置的行

做任何事情
        if (s.startsWith("Windows7") || s.startsWith("iPhone")
                || s.startsWith("Solaris")) {

            imageView.setImageResource(R.drawable.no);
        } else {
            imageView.setImageResource(R.drawable.ok);
        }

        return rowView;
    }
}

我建议使用自定义适配器,如上所示:

如果没有,您可以尝试获取list.getPositionForView(view)为所需的特定位置设置背景颜色或图像