0

is it possible to create a View which is driven by a SimpleCursorAdapter. The content from this view is ever time a entry from DB.

The View (dataView) looks like:

txtData1
txtData2
txtData3
btnPrev btnNext

I read around and tryd to setup this behavior. Hope its make sens:

public class mActivity extends Activity {
  public Context me = this; 
  public SimpleCursorAdapter mAdapter = null;
  public Cursor mCursor = null;

  private OnClickListener btnStart_onClick = new OnClickListener() {
    public void onClick(View v) {
      setContentView(R.layout.dataView);

      mCursor = mDB.rawQuery("SELECT * FROM Data", null);
      startManagingCursor(mCursor);

      mAdapter = new SimpleCursorAdapter(
        me,
        R.layout.dataView,
        mCursor,
        new String[] {"Data1", "Data2", "Data3"},
        new int[] {R.id.txtData1 , R.id.txtData2, R.id.txtData3});

        mAdapter.setViewBinder(VIEW_BINDER);
        mCursor.moveToFirst();
    }
  };
  static final ViewBinder VIEW_BINDER = new ViewBinder() {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex)
    {
      switch (view.getId())
      {
        case R.id.txtData1:
          TextView txt = (TextView) view;
          if (txt != null)
          {
            int index = cursor.getColumnIndex("Data1");
            txt.setText(cursor.getString(index));
          }
          return true;

        case R.id.txtData2:
          TextView txt = (TextView) view;
          if (txt != null)
          {
            int index = cursor.getColumnIndex("Data2");
            txt.setText(cursor.getString(index));
          }
          return true;

        case R.id.txtData3:
          TextView txt = (TextView) view;
          if (txt != null)
          {
            int index = cursor.getColumnIndex("Data3");
            txt.setText(cursor.getString(index));
          }
          return true;

        default:
          return false;
      }
    }
  };
}

When I run from the btnStart_onClick I dont get Data in my Textboxes :-(

Can somebody help? Can it work like this?

Next question: how can I use the Prev or Next Buttons? Possible this is the only thing I miss to "load" the first data...

EDIT: I extended my example with the global mCursor and the call to mCursor.moveToFirst() On my app I also tested with the next / prev buttons and the function mCursor.moveToNext() and mCursor.moveToPrevious()

But its not change :-(

BIba
  • 23
  • 4
  • Welcome to Stackoverflow! If you find a response is helpful, please up vote it. If the response successfully answers your question, please click the green check mark next to it to accept the answer. Also please look at http://stackoverflow.com/questions/how-to-ask for advice on how to write a good question – Kurtis Nusbaum Nov 02 '11 at 21:04
  • Have you checked to make sure that the sql query is actually returning stuff? – Kurtis Nusbaum Nov 02 '11 at 21:05
  • I'm sure you can get it working with a SimpleCursorAdapter... but why not just use a Cursor? – aleph_null Nov 02 '11 at 21:11
  • yes, it must :-) I have also set up a SimpleCursorAdapter for a spinner right before. – BIba Nov 02 '11 at 21:13
  • I dont know 8) why I not use a cursor, I am really new to android/java programming and dont know the difference. fact is, that I have a lot of data (1000 records) and cant use a simple array. – BIba Nov 02 '11 at 21:15
  • how can I use it with a Cursor? Can you point me to a shot example? – BIba Nov 02 '11 at 21:19
  • I think I found the resolutions: have to check them http://stackoverflow.com/questions/5573539/android-using-simplecursoradapter-to-set-colour-not-just-strings http://stackoverflow.com/questions/1505751/android-binding-data-from-a-database-to-a-checkbox-in-a-listview they overwrite the function bindView... hope it helps... – BIba Nov 02 '11 at 22:26
  • ok, with the examples from the other questions I had also no luck. But now I understand your: "but why not just use a Cursor" - Its a lot simpler to implement... so I think I will use only a Cursor... But if somebody has a solutions to use the SimpleCursorAdapter I also will be happy ;) – BIba Nov 02 '11 at 23:28

1 Answers1

0

As far as I can tell, there are a lot of what I think are conceptual/organizational/syntactical problems with your code. First of all, an adapter is usually exploited by a view such as ListView or Spinner, that gets populated with the data retrieved by the adapter via the cursor (or whatever data structure is backing it). However, I don't see this pattern in your code, and I'm left wondering what use an adapter would have in your case.

Second, you perform a whole SELECT * query in your click listener, i.e. you retrieve all your 1000 records for each click on... well, on what, exactly? You define the click listener, but never set it onto anything - just as you define the adapter, but you don't bind it to anything. The code that sets up the adapter, with the database query and the binder should really be placed outside the listener.

Last, I believe you mocked variable names a bit before posting the code, because in the following snippet:

TextView txt = (TextView) view;
if (txt != null)
{
    int index = cursor.getColumnIndex("Data1");
    String txt = cursor.getString(index);
    txt.setText(txt);
}

I could hardly see how the compiler is intended to distinguish the two txt variables on the last line of the if body.

Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62
  • so you can't use the SimpleCursorAdapter with the TextBoxes in a View? I had seen some examples implementing the [ViewBinder](http://developer.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html). In the docs I found this: _You should use this class to bind values from the Cursor to views that are not directly supported by SimpleCursorAdapter or to change the way binding occurs for views supported by SimpleCursorAdapter._ So I was thinking I can just implement it that way:) - I can or not? – BIba Nov 03 '11 at 07:05
  • And sorry - I don't really select all data in my sql select. The where is bound to a before made selection. Thats also why I use a button with the above written onClick Handler - the button is in main-view and is setting up the 2. (data)View . I was trying to hold the example small and hold only focus to the problem. **How can I bind the Adapter to my example? Or is it still not possible?** I was thinking it is made with the constructor. And yes - sorry the two txt's was my mistake in the snipped I changed them before posting to simplify it - its corrected – BIba Nov 03 '11 at 07:05
  • Usually, you bind the adapter to an [AdapterView](http://developer.android.com/reference/android/widget/AdapterView.html) (i.e. a view whose _children's_ content is determined by the adapter's data) using its `setAdapter` method. – Giulio Piancastelli Nov 03 '11 at 11:44