以编程方式更改自定义Listview的颜色

时间:2013-08-01 19:38:59

标签: android android-listview

我有一个ArrayAdapter来制作一个CustomListView。这是完整的代码。我想以编程方式从Java代码中设置XML R.layout.streampage_custom_row_view的背景颜色。

            adapter = new ArrayAdapter<String>(StreamPage_modified.this,
                    R.layout.streampage_custom_row_view, R.id.title, targetArray) {


                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View row = super.getView(position, convertView, parent);


                    TextView Description = (TextView) row
                            .findViewById(R.id.description);

                    Log.d("SPOC COUNT", "" + SPOC.size());
                    Log.d("SPOC POSitiON", "" + position);

                    Description.setText(SPOC.get(position).getDescription());

                    return row;
                }

            };
            adapter.notifyDataSetChanged();
            list.setAdapter(adapter);
            list.setOnItemClickListener(StreamPage_modified.this);

1 个答案:

答案 0 :(得分:0)

如果你想要set the background color of ListView's row而不是row.setBackgroundColor(Color.RED);内的getView() //(或任何颜色)

所以你的代码应该是

       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = super.getView(position, convertView, parent);
           row.setBackgroundColor(Color.RED); // or any color 

            TextView Description = (TextView) row
                    .findViewById(R.id.description);

            Log.d("SPOC COUNT", "" + SPOC.size());
            Log.d("SPOC POSitiON", "" + position);

            Description.setText(SPOC.get(position).getDescription());

            return row;
        }