【问题标题】:row color particular listview android行颜色特定的listview android
【发布时间】:2016-01-11 07:04:32
【问题描述】:

我有列表视图。我只想着色状态 = OK 行。 如下所示的列表视图
refno 名称 状态
0001 A 取消
0002 B 确定
0003 C 取消
0004 正常

代码如下

    @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);
}

如何只为 status=OK 行设置背景颜色.... 请帮忙..

【问题讨论】:

标签: android listview


【解决方案1】:

这里我会建议你使用 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();
        }

【讨论】:

  • 反对的选民请发表评论,以便我纠正这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
  • 2014-03-21
  • 2011-05-09
相关资源
最近更新 更多