Search code examples
androidandroid-listviewsimplecursoradapterandroid-viewbinder

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. 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:

Image

How can I solve this?


Solution

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