Search code examples
javaandroidlistviewcursor

SimpleCursorAdapter remove values


I have a ListView on each row i have a LinearLayout with some objects in it (mostly some TextViews).

This ListView i fill it dynamically from a cursor. In this cursor i have one value true or false.

I want to hide or make non clickable the lines with value false. I try this code but doesn't work

public void contentProviderInitialized(final Cursor cursor) {

    SimpleCursorAdapter commonTickets = new SimpleCursorAdapter(MyClass.this,
        R.layout.row_ticketing, cursor, new String[] {"price", "productName", "stopName" },
        new int[] { R.id.ticketing_price, R.id.ticketing_product, R.id.ticketing_stop_name }
    ) {
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            String enabledStr = cursor.getString(cursor.getColumnIndex("enabled"));
            String product = cursor.getString(cursor.getColumnIndex("productName"));
            boolean enabled = Boolean.parseBoolean(enabledStr);
            LinearLayout ticketingRow = (LinearLayout) view.findViewById(R.id.ticketing_row);
            if (enabled) {
                ticketingRow.setEnabled(true);
            } else {
                ticketingRow.setEnabled(false);
            }
            super.bindView(view, context, cursor);
        };
        MyClass.this.ticketing_list_view.setAdapter(commonTickets);
    }
}

Solution

  • Override isEnabled on the adapter

    http://developer.android.com/reference/android/widget/BaseAdapter.html#isEnabled(int)

    This answer seems to hint at it. Use movetoposition on the cursor. It sounds like the performance would be bad with that, though, so you might want to do some caching of true/false values based on numeric position? Try it out. See how it goes. The caching might be a waste.