Android使用数组颜色填充ListView

时间:2014-10-14 14:00:19

标签: android listview colors

我想用一组RGB颜色填充ListView。也就是说,每一行必须采用分配的RGB颜色。有可能吗?

public class Lista_colori extends Activity {

    // Initialize the array
    String[] Array = { "#33B5E5", "#d8e1e4", "#000000" };

    // Declare the UI components
    private ListView monthsListView;

    private ArrayAdapter arrayAdapter;

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

        // Initialize the UI components
        monthsListView = (ListView) findViewById(R.id.listView1);

        // From the third parameter, you plugged the data set to adapter
        arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, Array);

        // By using setAdapter method, you plugged the ListView with adapter
        monthsListView.setAdapter(arrayAdapter);

    }
}

1 个答案:

答案 0 :(得分:0)

你可以创建一个自定义适配器并使用getview方法 - 最好将颜色作为资源保存并执行此操作:

monthsListView = (ListView) findViewById(R.id.listView1);
CustomAdapter adapter = new CustomAdapter(this, list);
monthsListView.setAdapter(adapter);

public class CustomAdapter extends BaseAdapter {

        List<String> colorList;

        public CustomAdapter(Activity activity, List<String> colorList) {
            // TODO Auto-generated constructor stub
            this.colorList = productList;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return this.colorList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }



        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub


switch(position){
case 1:
   view.setBackgroundColor(R.colors.color_one);break;
case 2: //etc..
}

            });
            return convertView;
        }

    }

这只是一个片段,请根据您的要求尝试修改。