2011-11-02 56 views
0

是否可以創建一個由SimpleCursorAdapter驅動的View。這個視圖的內容永遠是來自數據庫的條目。android - SimpleCursorAdapter查看

的視圖(數據視圖)看起來像:

txtData1 
txtData2 
txtData3 
btnPrev btnNext 

我看周圍,tryd設置此行爲。希望它做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; 
     } 
    } 
    }; 
} 

當我從btnStart_onClick我沒有在我的文本框獲取數據:-(運行

有人可以幫它可以像這樣工作

下一個問題:怎麼了?我可以使用上一個或下一個按鈕嗎?可能這是我錯過了「加載」第一個數據的唯一...

編輯:我擴展我的例子與全局mCursor和調用mCursor.moveToFirst() 在我的應用程序上,我也測試了下一個/上一個按鈕和功能mCursor.moveToNext()和mCursor.moveToPrevious()

但它不是:-(

+0

歡迎來到Stackoverflow!如果您發現回覆有幫助,請投票。如果回覆成功回答您的問題,請點擊旁邊的綠色複選標記以接受答案。也請看看http://stackoverflow.com/questions/how-to-ask關於如何寫一個很好的問題的建議 –

+0

你檢查過,以確保SQL查詢實際上是返回的東西? –

+0

我相信你可以使用SimpleCursorAdapter來工作......但爲什麼不只是使用一個Cursor呢? –

回答

0

更改據我所知,有很多的東西,我認爲是概念/您的代碼存在組織/語法問題。首先,適配器通常被諸如ListViewSpinner之類的視圖利用,該視圖通過適配器通過遊標(或任何數據結構支持它)檢索到的數據填充。但是,我沒有在代碼中看到這種模式,我還想知道適配器在你的情況下會有什麼用處。

其次,您在您的點擊偵聽器中執行整個SELECT *查詢,即您檢索所有1000條記錄,每次點擊......呃,究竟是什麼?您可以定義點擊偵聽器,但從不將其設置爲任何東西 - 就像您定義適配器一樣,但您不會將其綁定到任何東西。設置適配器的代碼,數據庫查詢和綁定器應該放置在偵聽器之外。

最後,我相信你發佈的代碼之前嘲笑變量名了一下,因爲下面的代碼片段:

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

我幾乎看不到編譯器打算如何區分在最後兩個txt變量if機身的線。

+0

所以你不能在視圖中使用SimpleCursorAdapter和TextBoxes?我見過一些實現[ViewBinder]的例子(http://developer.android.com/reference/android/widget/SimpleCursorAdapter.ViewBinder.html)。在文檔中,我發現這一點:_您應該使用此類將Cursor中的值綁定到SimpleCursorAdapter不直接支持的視圖,或者更改SimpleCursorAdapter支持的視圖的綁定方式。因此,我想我可以實現它那種方式:) - 我可以或不可以? – BIba

+0

對不起 - 我沒有真正選擇我的sql select中的所有數據。在哪裏被綁定到之前做出的選擇。這也是爲什麼我使用上面寫的onClick處理程序的按鈕 - 該按鈕在主視圖中,並設置2.(數據)視圖。我試圖把這個例子放在一個小問題上,只關注這個問題。 **我如何將適配器綁定到我的示例?或者它仍然不可能?**我認爲它是由構造函數完成的。是的 - 對不起,兩個txt的是我的錯誤在剪輯我改變他們發佈之前,以簡化它 - 其更正 – BIba

+0

通常,您將適配器綁定到[AdapterView](http://developer.android.com/reference/android /widget/AdapterView.html)(即一個視圖,其_children的內容由適配器的數據決定)使用它的'setAdapter'方法。 –