创建ListView和设定的图的背景颜色的每一行中 [英] Creating a ListView and setting the background color of a view in each row

查看:108
本文介绍了创建ListView和设定的图的背景颜色的每一行中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现的是由包含在左侧后面一个TextView来的,正确的视图行的ListView。我希望能够改变的基础上在ListView它的位置上的第一视图的背景颜色。下面是我在这一点上,但它似乎并没有因任何事。

I am trying to implement a ListView that is composed of rows that contain a View on the left followed by a TextView to the right of that. I want to be able to change the background color of the first View based on it's position in the ListView. Below is what I have at this point but it doesn't seem to due anything.

public class Routes extends ListActivity {
    String[] ROUTES;
    TextView selection;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ROUTES = getResources().getStringArray(R.array.routes);

        setContentView(R.layout.routes);
        setListAdapter(new IconicAdapter());
        selection=(TextView)findViewById(R.id.selection);

    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        selection.setText(ROUTES[position]);

    }

    class IconicAdapter extends ArrayAdapter<String> {
        IconicAdapter() {
            super(Routes.this, R.layout.row, R.id.label, ROUTES);
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.label);

        label.setText(ROUTES[position]);

        View icon = (View) row.findViewById(R.id.icon);

        switch(position){

        case 0:
            icon.setBackgroundColor(R.color.Red);
            break;
        case 1:
            icon.setBackgroundColor(R.color.Red);
            break;
        case 2:
            icon.setBackgroundColor(R.color.Green);
            break;
        case 3:
            icon.setBackgroundColor(R.color.Green);
            break;
        case 4:
            icon.setBackgroundColor(R.color.Blue);
            break;
        case 5:
            icon.setBackgroundColor(R.color.Blue);
            break;
        }

        return(row);
    }

}

任何输入AP preciated,如果您有任何问题,请不要犹豫,问!

Any input is appreciated and if you have any questions don't hesitate to ask!

谢谢,
罗布

推荐答案

发现两个问题:

1)getView()方法是不是我创造所以它甚至没有被调用的内部类。

1) The getView() method was not within the inner class that I had created so it was not even being called.

2),而不是调用setBackgroundColor()的,我需要调用setBackgroundResource()。

2) Instead of calling setBackgroundColor(), I needed to call setBackgroundResource().

这是现在的工作。

这篇关于创建ListView和设定的图的背景颜色的每一行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆