[英]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.