3

我正在编写一个应用程序来检查我们的“网络服务”并返回“托管”或“非托管”状态,我在列表视图中保留正在运行的搜索历史记录(直到用户清除它),这一切正常。

我想做的是,如果状态是“托管”,我希望单个 textview 项目为绿色,如果它是“未托管”,我希望单个项目为红色。

    //This is the String Array which gets populated later.
    ArrayList<String> _searchHistoryList = new ArrayList<String>();

    //Here is where I override the getView to try to change the color.
    listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, _searchHistoryList) {
    @Override
    public View getView(int position, View convertView,  ViewGroup parent) {
        TextView textView = (TextView) super.getView(position, convertView, parent);

        String _currentStatus = appSettings.getString("current_status", "na");

        int textColor;
        if (_currentStatus.equals("Managed")){
            textColor = R.color.green_text;
        } else {
            textColor = R.color.red_text;
        }
        textView.setTextColor(getResources().getColor(textColor));

        return textView;
    }
};
lvSearchHistory.setAdapter(listAdapter); 

上面的代码实际上把整个listview变成了最后一个搜索项的颜色。(例如,如果最后一个搜索结果为“托管”结果 - 整个项目列表变为绿色,而不仅仅是单个项目)。

谁能指出我正确的方向。

4

3 回答 3

2

在我看来你的问题的根源是这个电话:

String _currentStatus = appSettings.getString("current_status", "na");

此调用中没有任何内容表明您正在检查哪个列表元素,因此它将始终为列表中的每个视图返回相同的结果。您应该传入 position 参数并使用它来获取相关列表条目的状态(您如何执行此操作取决于您如何实际获取列表元素的状态,但这应该不会太难)。

于 2012-10-03T03:11:40.003 回答
1

试试下面的代码

if (_currentStatus.equals("Managed")){
        textView.setTextColor(0xFF008B45);
    } else {
        textView.setTextColor(0xFFB0171F);
    }
于 2012-10-03T04:55:05.983 回答
0

试试下面的代码:

       int textColor;
    if (_currentStatus.equals("Managed")){
        textColor = R.color.green_text;
     textView.setTextColor(getResources().getColor(textColor));
    } else {
        textColor = R.color.red_text;
       textView.setTextColor(getResources().getColor(textColor));
    }


    return textView;
于 2012-10-03T03:23:32.803 回答