Android使用数组颜色填充ListView
我想用RGB颜色数组填充ListView。也就是说,每条线必须采用指定的RGB颜色。可能吗Android使用数组颜色填充ListView,android,listview,colors,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;
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);
}
}
您可以创建一个自定义适配器并为此使用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;
}
}
monthListView=(ListView)findViewById(R.id.listView1);
CustomAdapter=新的CustomAdapter(此,列表);
monthListView.setAdapter(适配器);
公共类CustomAdapter扩展了BaseAdapter{
列表颜色列表;
公共CustomAdapter(活动,列表颜色列表){
//TODO自动生成的构造函数存根
this.colorList=productList;
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回此.colorList.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(最终整型位置,视图转换视图,
视图组(父级){
//TODO自动生成的方法存根
开关(位置){
案例1:
视图.setBackgroundColor(R.colors.color\u one);中断;
案例2://等。。
}
});
返回视图;
}
}
这只是一个片段,请尝试根据您的要求修改它