行颜色特定listview android

行颜色特定listview android,android,listview,Android,Listview,我有listview。我只想用颜色表示status=OK行。 列表视图如下图所示 参考号名称状态 0001 A取消 0002 B好的 0003 C取消 0004 D好的 代码如下 如何为status=OK行设置背景色(仅限…)。。。。 请帮助..在这里,我建议您使用BaseAdapter概念来定制listview。也可以使用ArrayAdapter。但在这里,我通过扩展自定义适配器给出了答案。我已经在需要的地方给出了评论&它还带有类似于视图可恢复性的模式 ListViewCustomAdapte

我有listview。我只想用颜色表示status=OK行。 列表视图如下图所示 参考号名称状态 0001 A取消 0002 B好的 0003 C取消 0004 D好的

代码如下

如何为status=OK行设置背景色(仅限…)。。。。
请帮助..

在这里,我建议您使用BaseAdapter概念来定制listview。也可以使用ArrayAdapter。但在这里,我通过扩展自定义适配器给出了答案。我已经在需要的地方给出了评论&它还带有类似于视图可恢复性的模式

ListViewCustomAdapter

一次创建

更新


有一个在选民下来请留下评论,以便我可以纠正这一点?
    @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));
            }


            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, allDetails);
            listview.setAdapter(adapter);
}
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;
        }
    }
@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();
        }