0

我正在尝试更改列表视图中显示的文本视图的颜色。我希望它根据文字内容进行更改。您会看到我正在尝试在 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;
    }
}
4

1 回答 1

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;
    }
...
}
于 2013-10-30T01:13:19.950 回答