1

I'm using a SimpleCursorAdapter and I'm not sure why this is working only sometimes. If I redo the same query I get different bindings. It has nothing to do with what the cursor has though I know.

All I am trying to do is set the TextView to null if the cursor.getString(cursor.getColumnIndex(...)) is null. It works sometimes, but not all of the time. This makes it very hard to debug of course. I noticed in LogCat that it seems to be getting the right value and setting it to the textview when it is not null, but it's not updating itself in the ListView. I have a long ListView at some times like 732 results. When I scroll through the list, I can see setViewValue being called and saying that it set the text, but nothing changed. The TextView kanjiTextView = (TextView) view; line has never been null either.

    mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
   public boolean setViewValue(View view, Cursor cursor, int columnIndex)
          {
             if (view.getId() == R.id.kanji)
               {
                  if (cursor.getString(cursor.getColumnIndex("kanji")) == null)
                          {
                             view.setVisibility(View.GONE);
                             return true;
                          } else {
                             TextView kanjiTextView = (TextView) view;
                               kanjiTextView.setText(cursor.getString(cursor.getColumnIndex("kanji")));
                      return true; 
                          }
              }
             return false;
          }

Is there something I'm missing here? Let me know if I need to post more LogCat stuff.

ViewBinder creates random content this answer seems kind of similar to my problem but I'm not sure if it's the same reason or not.

Community
  • 1
  • 1
John61590
  • 1,106
  • 1
  • 13
  • 29
  • got it work using a custom cursoradapter. apparently one of the views wasn't visible. `if (cursor.getString(cursor.getColumnIndex("kanji")) == null) { TextView kanjiTextView = (TextView) view.findViewById(R.id.kanji); kanjiTextView.setVisibility(View.GONE); } else { TextView kanjiTextView = (TextView) view.findViewById(R.id.kanji); kanjiTextView.setVisibility(View.VISIBLE); kanjiTextView.setText(cursor.getString(cursor.getColumnIndex("kanji"))); }` – John61590 Jun 17 '13 at 20:25

1 Answers1

1

Thanks for your answer! To clarify the topic for others:

If you need to hide a view where the cursor column doesn't have a value and you want to show it if it has a value, you need to make it visible again! If you don't do that it will be gone after scrolling.

Example:

    public boolean setViewValue(View view, Cursor cursor,
            int columnIndex) {

        final int commentCol = cursor.getColumnIndex(COMMENT);

        // hide comment if empty
        if (columnIndex == commentCol) {

            if (cursor.isNull(commentCol)) {
                view.setVisibility(View.GONE);
            }
            else {
                view.setVisibility(View.VISIBLE);
                ((TextView)view).setText(cursor.getString(commentCol));
            }

            return true;
        }

        return false;
    }