直接接使用ListView组件创建列表视图,可以有两种方式。
一种是通过在XML布局文件中使用<ListView>标记添加,
android:entries="@array/ctype"
<string-array name="type">
<item>AAA</item>
<item>BBB</item>
<item>CCC</item>
</string-array>
private String[] strType = new String[]{"app1", "app2", "app3" };
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_single_choice, strType);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String result = parent.getItemAtPosition(position).toString();
view.setBackgroundColor(Color.RED);
}
});
//adapter = ArrayAdapter.createFromResource(this, R.array.ctype, android.R.layout.simple_list_item_checked);
怎么更新任意一行背景色的问题:
不是直接的调用函数设置颜色。
数据是来自adapter,还是写在adapter里的getView里面先做条件判断,然后在需要更新的地方调用Adapter.notifyDataSetChanged();会更调用回调函数getview
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.fruit_item, null);
TextView textView = convertView.findViewById(R.id.fruit_name);
textView.setText(mList.get(position).getName());
//检测到某个条件,比如说Chao.apple==true
if(position == 0 && Chao.apple) {
convertView.setBackgroundColor(Color.BLUE);
}
return convertView;
}
//Record here first