更改android ListView项目前颜色

时间:2012-04-23 23:17:57

标签: android listview colors

我想根据变量更改ListView项目(填充Async类)的颜色。所以,我有这个

public class Search extends AsyncTask<Void, String, Void> {
    boolean blue = false;
    @Override
    protected Void doInBackground(Void... params) {
        Resources res = getResources();
        List<String> Items = Arrays.asList(res.getStringArray(R.array.Items));
        for (String it : Items) {
            if (it.contains("blue")) { blue = true; } else { blue = false; }    
            publishProgress(it);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onProgressUpdate(String... item) {
        ((ArrayAdapter<String>)getListAdapter()).add(item[0]);
        // Change the color of the current item depending on "blue"
    }
}

这可能吗?谢谢!

2 个答案:

答案 0 :(得分:1)

您需要在自定义适配器中覆盖getView,并提供自己的自定义视图。

答案 1 :(得分:0)

当然,您可以向阵列适配器添加元素。但是有几个问题需要注意:

  1. 传入一个ArrayList或其他任何可以在
  2. 上实际调用add的地方
  3. 不要忘记调用setNotifyOnChange(true);
  4. 所以一般来说应该这样做。但是,我不确定我是否理解为什么以及使用添加项目的AsyncTask所做的事情......

    为什么不在创建ListView时添加它们?有没有我错过的东西?

相关问题