繁体   English   中英

使用SimpleCursorAdapter.ViewBinder更改Listview中的文本颜色

[英]Changing text color in Listview using SimpleCursorAdapter.ViewBinder

我已经使用SimpleCursorAdapter列出了数据库中的名称,并且想使用SimpleCursorAdapter.ViewBinder的方法更改特定名称的颜色。 我在运行此方法时遇到问题,我的数据库包含不同的名称,但ListView将所有名称显示为一个特定名称。 我究竟做错了什么? 如何更改特定名称的文本颜色? 是否可以使用ViewBinder

这是我的ViewBinder代码的ViewBinder

    SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        // TODO Auto-generated method stub
        String[] temp = dh.return_result();
        tv = (TextView)findViewById(R.id.textView1);
        tv = (TextView) view;
        for(int i = 0; i<temp.length; i++)
        {
            if(temp[i].equalsIgnoreCase("Ameer Zhann"))
            {
                tv.setText(temp[i]);
                tv.setTextColor(Color.rgb(58, 58, 224));
                return true;
            }
        }
        return false;
    }
};

这是我的输出图像:

图片

我该如何解决?

尝试这种方式:

public boolean setViewValue(View view, Cursor cursor, int columnIndex){    
    int getIndex = cursor.getColumnIndex("Name");
    String empname = cursor.getString(getIndex);
    tv = (TextView) view;
    tv.setTextColor(Color.WHITE);
    tv.setText(empname);
    if(empname.equals("Any String"))
    {                   
        tv.setTextColor(Color.rgb(58, 58, 224));
        return true;
    }
    return false;           
}

代码完全按照您的要求进行操作-对于Cursor每个元素,您都要遍历所有列表并设置每个元素的文本。 我认为“ Ameer Zhann”是您列表中的最后一个结果,因此,只有此文本保留在TextView

方法setViewValue(...)Cursor每个元素调用。 因此,您不需要任何循环,只需用光标值tv.setText(Cursor.getString(...));填充文本tv.setText(Cursor.getString(...));

此代码也有些奇怪:

tv = (TextView)findViewById(R.id.textView1);
tv = (TextView) view; 

作为参数出现的view -已经是ID为R.id.textView1视图-因此只需删除对findViewById调用即可。

尝试其他部分

if(temp[i].equalsIgnoreCase("Ameer Zhann")){
    tv.setText(temp[i]);
    tv.setTextColor(Color.rgb(58, 58, 224));
}else{
    tv.setText(temp[i]);
}

最后返回true而不是false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM