这里我会建议你使用 BaseAdapter 概念来自定义列表视图。也可以使用 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();
}