Search code examples
androidlistviewandroid-viewsimplecursoradapterlistadapter

Make a TextView visible in a ListView row only on one line via adapter


I wanna highlight only one row at the time, I'am doing this by making a TextView visible (or use the BadgeView library, which I used in my first try but produced this bug and I thought it was a flaw / bug of the library).

This is my approach: (I have overriden this method in my SimpleCursorAdapter)

@Override
public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);

    int rowId = Integer.valueOf(((TextView) view.findViewById(R.id.two_line_rowid)).getText().toString());

    if (rowId == mRowIdSelected) {
        ((TextView) view.findViewById(android.R.id.text1)).setText("SELECTED");
        ((TextView) view.findViewById(R.id.select_badge)).setVisibility(View.VISIBLE);
    }

}

It works pretty well, but there is a bug. The text in the android.R.id.text1 only change in the selected row, this is working as intended, but the TextView is made visible in the first row and the selected row, which is not working as intended.

Any ideas?


Solution

  • Can you try this:

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        super.bindView(view, context, cursor);
    
        int rowId = Integer.valueOf(((TextView) view.findViewById(R.id.two_line_rowid)).getText().toString());
    
        if (rowId == mRowIdSelected) {
            ((TextView) view.findViewById(android.R.id.text1)).setText("SELECTED");
            ((TextView) view.findViewById(R.id.select_badge)).setVisibility(View.VISIBLE);
        } else {
            ((TextView) view.findViewById(android.R.id.text1)).setText("NOT-SELECTED");
            ((TextView) view.findViewById(R.id.select_badge)).setVisibility(View.INVISIBLE);
        }
    
    }
    

    Can you provide the layout of the view that your passing?