2010-10-27 50 views
2

我有一個自定義的listview行,其中包含一些textView組件。我沒有使用「標準」單一文本項目,而是創建了一個項目,其中每個列表視圖行包含幾位信息。對於這個例子,我有一個記錄ID,一個名稱和一個列表視圖中的每一行的描述。Android自定義ListView選擇的項目的孩子?

我必須通過

this.mDbHelper = new RecordDBAdapter(this); 
this.mDbHelper.open(); 
Cursor c = this.mDbHelper.fetchAllRecords(); 
startManagingCursor(c); 

String[] from = new String[] {RecordDBAdapter.ROW_ID, RecordDBAdapter.NAME, RecordDBAdapter.DESCRIPTION}; 

int[] to = new int[] {R.id.recordId, R.id.lblName, R.id.lblDescription}; 

// Now create an array adapter and set it to display using our row 
SimpleCursorAdapter records = new SimpleCursorAdapter(this, R.layout.record_row, c, from, to); 

this.list.setAdapter(dives); 

填充的ListView我現在我要的是能夠給每個被點擊的項目中訪問的recordId值。我試圖遵循Android教程無濟於事。他們做類似

Object o = adapter.getItemAtPosition(position); 

但仍然沒有幫助。我真正想要的是獲取recordId的值在每個選定的listItem中。有誰知道這將如何完成?這是我的onItemClick事件:

 protected OnItemClickListener onListItemClick = new OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> adapter, View view, int position, long rowId) { 

// My goal is either to grab the value of the 
// recordId value from the textView, or from the adapter. 
       //Object o = adapter.getItemAtPosition(position); //Tried this, no joy 
       Intent intent = new Intent(NeatActivity.this, SomeOtherActivity.class);    
     //  intent.putExtra("selectedRecordId",val); //val here is a numerical record row id being passed to another activity. 
       startActivity(intent); 
      }  
     }; 

回答

0

你試圖做太多的默認適配器類。你需要編寫你自己的適配器類,擴展simplecursoradapter,然後你可以在任何想要的位置創建任何想要的任何方法。

+0

感謝您的回覆。我對java還是比較陌生,並沒有弄清楚所有的錯綜複雜。所以你說沒有子類化的話,沒有辦法在適配器或textview中訪問這些值。在我看來,這些值是可訪問的,因爲我們將適配器和視圖都傳遞給onListItemCLick處理程序。當我放置監視表達式時,可以看到適配器中的列,但無法獲取每列中保存的值。有點困惑,爲什麼我們需要適配器和視圖作爲參數,如果他們不能用於檢索數據值? – Shawn 2010-10-27 00:59:50

+0

我確定他們是可以接受的。只是你可能不得不做更多的事情,而不是上課。基類是作爲一個框架。在做任何事情而不僅僅是顯示值時,你都期望適配器的子類 – Falmarri 2010-10-27 02:37:10