简体   繁体   English

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

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

I've listed the names from database using SimpleCursorAdapter and I want to change the color of a particular name using SimpleCursorAdapter.ViewBinder 's method. 我已经使用SimpleCursorAdapter列出了数据库中的名称,并且想使用SimpleCursorAdapter.ViewBinder的方法更改特定名称的颜色。 I've a problem while running this method, my database contains different names but the ListView will display all the namse as one particular name. 我在运行此方法时遇到问题,我的数据库包含不同的名称,但ListView将所有名称显示为一个特定名称。 What am I doing wrong? 我究竟做错了什么? How to change the text color of a specific name? 如何更改特定名称的文本颜色? Is it possible using ViewBinder ? 是否可以使用ViewBinder

This is my part of of code for 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;
    }
};

and this is my output image: 这是我的输出图像:

图片

How can I solve this? 我该如何解决?

Try this way: 尝试这种方式:

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;           
}

Code do exactly what you ask - for every element in Cursor you go through all list and set text of each element. 代码完全按照您的要求进行操作-对于Cursor每个元素,您都要遍历所有列表并设置每个元素的文本。 I think that "Ameer Zhann" is last result in your list, so only this text left in TextView . 我认为“ Ameer Zhann”是您列表中的最后一个结果,因此,只有此文本保留在TextView

Method setViewValue(...) called for every element of Cursor . 方法setViewValue(...)Cursor每个元素调用。 So, you don't need any cycle, just fill text with cursor value tv.setText(Cursor.getString(...)); 因此,您不需要任何循环,只需用光标值tv.setText(Cursor.getString(...));填充文本tv.setText(Cursor.getString(...)); .

Also there is something strange with this code: 此代码也有些奇怪:

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

view that comes as param - is already view with id R.id.textView1 - so just remove call of findViewById . 作为参数出现的view -已经是ID为R.id.textView1视图-因此只需删除对findViewById调用即可。

try with the else part 尝试其他部分

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

and at the end return true instead of false 最后返回true而不是false

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

相关问题 使用SimpleCursorAdapter.ViewBinder更改listView中的颜色。 安卓 - Using SimpleCursorAdapter.ViewBinder to change color in a listView. android 使用SimpleCursorAdapter.ViewBinder更改TextView的颜色 - Using SimpleCursorAdapter.ViewBinder to change the color of TextView 使用SimpleCursorAdapter.ViewBinder更改视图的颜色 - Using SimpleCursorAdapter.ViewBinder to change the color of View SimpleCursorAdapter.ViewBinder文本未显示 - SimpleCursorAdapter.ViewBinder text not showing Android - SimpleCursorAdapter.ViewBinder - 设置背景颜色 - Android - SimpleCursorAdapter.ViewBinder - Set background color 使用SimpleCursorAdapter.ViewBinder显示数据库中的文本和图像 - Display text and image from database with SimpleCursorAdapter.ViewBinder Android SimpleCursorAdapter.ViewBinder不更新绑定的TextView - Android SimpleCursorAdapter.ViewBinder not updating bound TextView 使用SimpleCursorAdapter和ViewBinder在ListView项目中设置图像 - Setting an image in ListView item using SimpleCursorAdapter and ViewBinder SimpleCursorAdapter.ViewBinder-不同地应用于每一行(ListFragment) - SimpleCursorAdapter.ViewBinder - Apply differently to each row (ListFragment) 使用ViewBinder,SimpleCursorAdapter,SQLite在ListView上显示相机拍摄的图片 - Using ViewBinder, SimpleCursorAdapter, SQLite, to show pictures taken by a camera on ListView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM