我正在尝试更改列表视图中显示的文本视图的颜色。我希望它根据文字内容进行更改。您会看到我正在尝试在 onPostExecute 方法中执行此操作。for 循环不会迭代,因为 getChild 调用返回 0。
这是我的课:
private class GetJSON extends
AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
ArrayList<HashMap<String, String>> AmmoList = new ArrayList<HashMap<String, String>>();
@Override
protected void onPreExecute() {
if (pDialog != null) {
pDialog.dismiss();
}
// Showing progress dialog before sending http request
pDialog = new ProgressDialog(AmmoDisplay.this);
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(AmmoDisplay.this, AmmoList,
R.layout.list_item, new String[] { TAG_DESC, TAG_STOCK,
TAG_PRICE, TAG_RD, TAG_HREF }, new int[] {
R.id.desc, R.id.stock, R.id.price, R.id.rd,
R.id.href });
setListAdapter(adapter);
ListView lv = getListView();
int childCount = lv.getCount();
for (int i = 0; i < childCount; i++) {
View v = lv.getChildAt(i);
TextView tv = (TextView) v.findViewById(R.id.stock);
String stockCol = tv.getText().toString();
System.out.println(stockCol);
if (stockCol == "in stock") {
tv.setTextColor(getResources().getColor(R.color.in_stock));
}
if (stockCol == "out of stock") {
tv.setTextColor(getResources().getColor(R.color.no_stock));
}
}
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String href = ((TextView) view.findViewById(R.id.href))
.getText().toString();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse(href));
startActivity(browserIntent);
}
});
if (pDialog != null) {
pDialog.dismiss();
}
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(
String... params) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(BASE_URL + AMMO);
try {
ammos = json.getJSONArray(TAG_AMMO);
for (int i = 0; i < ammos.length(); i++) {
JSONObject c = ammos.getJSONObject(i);
String stock = c.getString(TAG_STOCK);
String desc = c.getString(TAG_DESC);
String price = c.getString(TAG_PRICE);
String rd = c.getString(TAG_RD);
String href = c.getString(TAG_HREF);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_STOCK, stock);
map.put(TAG_DESC, desc);
map.put(TAG_PRICE, price);
map.put(TAG_RD, rd);
map.put(TAG_HREF, href);
AmmoList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return AmmoList;
}
}