问题描述:

I am trying to change the color of a text view that is displayed in my list view. I'd like it to change based on what the text says. You'll see that I am attempting to do this in the onPostExecute method. The for loop does not iterate because the getChild call returns 0.

Here is my class:

 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;

}

}

网友答案:

Don't use a SimpleAdapter. Create a custom adapter that overrides getView and make whatever manipulations that you want to the text color based on the current item's text.

The logic will be simpler and clearer than trying to go through the list afterwards.

See /samples/android-8/ApiDemos/src/com/example/android/apis/view/List5.java in your Android SDK folder (download appropriate samples as necessary) for a simple example:

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;
    }
...
}
相关阅读:
Top