首页 > 解决方案 > 如何根据数据更改列表视图中的文本视图颜色

问题描述

我从 sql 数据库中获取停车位编号及其特定区域的相应状态,并在异步类的 onpostexecute 方法中填充列表视图。现在,如果状态为 0,我需要将文本视图的颜色更改为绿色,如果状态为 1,则更改为红色,因为橙色是状态为 1。

我使用 hashmap 将插槽号和状态存储在 jet 值对中。我试图从 textview 获取数据并根据它进行更改,但它显示为 null,因为 listview 和 listview 项目处于不同的布局中。

我获取数据和填充列表视图的代码

     public class GetHttpResponse extends AsyncTask<Void, Void, String> {
    public Context context;

    public GetHttpResponse(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... arg0) {
        String SetServerString = "";
        Log.d("json",FinalJSonObject);
        try {
            if (FinalJSonObject != null) {
                JSONArray jsonArray = null;


                try {

                    jsonArray = new JSONArray(FinalJSonObject);

                    JSONObject jsonObject;

                    for (int i = 0; i < jsonArray.length(); i++) {
                        jsonObject = jsonArray.getJSONObject(i);

                        // Storing Student Name, Phone Number, Class into Variables.
                        sno = jsonObject.getString("slot_number").toString();
                        status = jsonObject.getString("reserv_status").toString();


                        HashMap<String, String> slot = new HashMap<>();
                        // adding each child node to HashMap key => value
                        slot.put("sno", sno);
                        slot.put("status", status);


                        // adding contact to contact list
                        slotList.add(slot);


                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return SetServerString ;
    }

    @Override
    protected void onPostExecute(String result) {
     Log.d("bhjg",result);
        // Setting Student Name, Phone Number, Class into TextView after done all process .
        ListAdapter adapter = new SimpleAdapter(
                ShowSingleRecordActivity.this, slotList,
                R.layout.slotlist_item, new String[]{"sno","status"}, new int[]{R.id.textView1,R.id.textView2
        });

        lv.setAdapter(adapter);
      /*  TextView v1=(TextView)findViewById(R.id.textView1);
        TextView vh=(TextView)findViewById(R.id.textView2);
        String v=vh.getText().toString();
        if (slot.get("status").equals("0"))
        {
         v1   .setTextColor(Color.parseColor("#008000"));
        }
       else if (v.equals("1"))
        {
            v1   .setTextColor(Color.parseColor("#ff0000"));
        }
        else if (v.equals("2"))
        {
            v1   .setTextColor(Color.parseColor("#FFA500"));
        }*/
    }
}

我希望列表视图与 textview 在各自的颜色区分

标签: android

解决方案


SimpleAdaper 适配器 = new SimpleAdapter( ShowSingleRecordActivity.this, slotList, R.layout.slotlist_item, new String[]{"sno","status"}, new int[]{R.id.textView1,R.id.textView2 }) ;

将列表适配器更改为简单适配器


推荐阅读