android - 如何根据数据更改列表视图中的文本视图颜色
问题描述
我从 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 在各自的颜色区分
解决方案
SimpleAdaper 适配器 = new SimpleAdapter( ShowSingleRecordActivity.this, slotList, R.layout.slotlist_item, new String[]{"sno","status"}, new int[]{R.id.textView1,R.id.textView2 }) ;
将列表适配器更改为简单适配器
推荐阅读
- ansible - 通过 requirements.yml 安装 amazon.aws 或 community.aws 集合不起作用
- c# - 如何在 Ubuntu 上使用 NginX 提供 Blazor 应用程序
- php - 如何在 Laravel 8 中仅从博客文章中提取文本
- xml - 需要一些关于 xpath 的建议。是空间
- java - 将类移动到新文件夹时 Netbeans 出现奇怪的错误
- bash - 执行 bash 命令组时 `ps --forest` 的解释是什么?
- python - 尝试网络抓取我自己的电子邮件时越过登录页面
- python - 如何计算所有可能路线中的二氧化碳排放量,而不仅仅是一条路线,就像代码在行变量“距离”中显示的那样
- php - 在前端删除自定义帖子类型并重定向到特定页面
- .htaccess - htaccess 提供了一个新的 url 但浏览器显示错误 404-未找到