我有listview。我想颜色说状态= OK行。
列表视图如下
refno 名称 状态
0001 A取消
0002 B OK
0003 C取消
0004 D OK
代码低于
IEnumerable<Object> objList = (IEnumerable<Object>)(dataModel.Value); // This works, List<whatever> is IEnumerable<whatever>, IEnumerable<whatever> can safely be cast to IEnumerable<object>
如何设置status = OK行的背景颜色.... 请帮忙..
答案 0 :(得分:-1)
在这里,我建议您使用BaseAdapter概念来自定义listview。也可以使用ArrayAdapter。但是我在这里通过扩展BaseAdapter给出了自定义适配器的答案。我已将评论发表在需要的地方。它也是ViewHolder模式,就像视图的可重用性
<强> ListViewCustomAdapter 强>
private class ListViewCustomAdapter extends BaseAdapter {
Context context;
int totalDisplayDatasize = 0;
JSONArray codeLeanChapterList;
public ListViewCustomAdapter(Context context,
JSONArray codeLeanChapterList) {
this.context = context;
this.codeLeanChapterList = codeLeanChapterList;
if (this.codeLeanChapterList != null)
totalDisplayDatasize = this.codeLeanChapterList.length();
}
@Override
public int getCount() {
// this could be one of the reason for not showing listview.set
// total data length for count
return totalDisplayDatasize;
}
@Override
public JSONObject getItem(int i) {
try {
return (JSONObject) this.codeLeanChapterList.get(i);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
public long getItemId(int i) {
return i;
}
private class Holder {
TextView textView1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
Holder holder = null;
View view = convertView;
/*
* First time for row if view is not created than inflate the view
* and create instance of the row view Cast the control by using
* findview by id and store it in view tag using holder class
*/
if (view == null) {
holder = new Holder();
// / No need to create LayoutInflater instance in
// constructor
convertView = LayoutInflater.from(this.context).inflate(
android.R.layout.simple_list_item_1, parent, false);
holder.textView1 = (TextView) convertView;
convertView.setTag(holder);
} else {
/*
* Here view next time it wont b null as its created and
* inflated once and in above if statement its created. And
* stored it in view tag. Get the holder class from view tag
*/
holder = (Holder) convertView.getTag();
}
JSONObject jsonObject = this.codeLeanChapterList.getJSONObject(position);
holder.textView1.setText(jsonObject.optString("refno") + " - " + jsonObject.optString("names") + " - " + " - " + jsonObject.optString("status"));
String flagChar = jsonObject.optString("status");
if ("OK".equalsIgnoreCase(flagChar)) {
holder.textView1.setBackgroundColor(Color.GREEN);
/// in this i have given background color to textview
} else {
// here i gave red color if status is not OK
holder.textView1.setBackgroundColor(Color.RED);
}
} catch (JSONException e) {
}
return convertView;
}
}
<强> OnCreate中 强>
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listview = (ListView) findViewById(R.id.List1);
dbhandler = new DBHandler(this);
// refno = new ArrayList<String>();
// names = new ArrayList<String>();
// status=new ArrayList<String>();
//
// allDetails = new ArrayList<String>();
List = dbhandler.getAllList();
// for(int i=0; i< List.size(); i++){
// refno.add(List.get(i).getrefno());
// names.add(List.get(i).getname());
// status.add(List.get(i).getstatus());
// }
// for(int i=0; i<refno.size(); i++){
// allDetails.add(refno.get(i) + " - " + names.get(i) + " - " + " - " + status.get(i));
// }
try {
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < List.size(); i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("refno", List.get(i).getrefno());
jsonObject.put("names", List.get(i).getname());
jsonObject.put("status", List.get(i).getstatus());
}
} catch (JSONException e) {
e.printStackTrace();
}
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, allDetails);
ListViewCustomAdapter customAdapter = new ListViewCustomAdapter(this, jsonArray);
listview.setAdapter(adapter);
}
<强> 更新 强>
try {
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < List.size(); i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("refno", List.get(i).getrefno());
jsonObject.put("names", List.get(i).getname());
jsonObject.put("status", List.get(i).getstatus());
}
} catch (JSONException e) {
e.printStackTrace();
}