我正在尝试更改列表视图中显示的文本视图的颜色。我希望根据文字说明改变它。您将看到我正在尝试在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;
}
}
答案 0 :(得分:1)
请勿使用SimpleAdapter
。创建一个覆盖getView
的自定义适配器,并根据当前项目的文本对文本颜色进行任何操作。
逻辑将比之后尝试浏览列表更简单,更清晰。
有关简单示例,请参阅Android SDK文件夹中的/samples/android-8/ApiDemos/src/com/example/android/apis/view/List5.java
(根据需要下载相应的示例):
private class MyListAdapter extends BaseAdapter {
...
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = (TextView) LayoutInflater.from(mContext).inflate(
android.R.layout.simple_expandable_list_item_1, parent, false);
} else {
tv = (TextView) convertView;
}
tv.setText(mStrings[position]);
return tv;
}
...
}