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. 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. What am I doing wrong? How to change the text color of a specific name? Is it possible using ViewBinder
?
This is my part of of code for 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. I think that "Ameer Zhann" is last result in your list, so only this text left in TextView
.
Method setViewValue(...)
called for every element of Cursor
. So, you don't need any cycle, just fill text with cursor value 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
.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.