简体   繁体   中英

android - SimpleCursorAdapter for a View

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

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, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM