2014-03-31 30 views
0

我做一個列表視圖,並從數據庫中獲取數據。每個視圖或行已經從數據庫的價值機器人通過id來的ArrayList

但怎麼也通過ID從數據庫列表視圖

所以當我點擊一個一個觀點我得到的ID,做我喜歡的事情(刪除...其中id = ...)

this how it looks

這是我的代碼謝謝:

private void setAdapter() { 


dbHelper=new DatabaseHelper(this); 
Cursor c = dbHelper.getcursor("M_table","M_title"); 
String s=""; 
Integer i=0; 
ArrayList<String> values = new ArrayList<String>(); 
if (c != null) { 
if (c.moveToFirst()) { 
do { 
i++; 
int Mid = c.getInt(c.getColumnIndex("Mid")); 
String M_title = c.getString(c.getColumnIndex("M_title")); 






values.add(M_title); 



}while (c.moveToNext()); 
} 
} 


ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listview,values); 

setListAdapter(adapter); 
adapter.notifyDataSetChanged(); 
} 
+0

編寫自定義列表適配器。這是一個很好的教程。 http://www.vogella.com/tutorials/AndroidListView/article.html – Ne0

回答

0

首先,你必須在你的listview XML文件中存儲id。有一個這樣的字段,其次是你的其他字段。

<TextView android:id="@+id/tv_Id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" />

然後在適配器中的代碼,你將不得不從這一領域取得的ID,並用包把它傳遞到下一個活動。

public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      String clickid = ((TextView) view.findViewById(R.id.tv_Id)) 
        .getText().toString(); 

      Bundle job_info = new Bundle(); 
      id_info.putString("key", clickid); 
      Intent i = new Intent(CurrentActivity.this, NextActivity.class); 
      i.putExtras(id_info); 
      startActivity(i); 
     }