2014-10-19 30 views
0

我試過,但無法得到它... 我嘗試添加這樣如何獲取它的ID ... SQLite SimplecursorAdapter?

new String[] {"name", "_id"}, // I want the value of _id 
new int[] {R.id.textView1, id}); 

但仍力工作

public class Veg extends SherlockActivity implements OnItemClickListener { 

private Cursor data; 
private DBManager db; 
ListView lv; 
int id; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.nonveg); 
    lv = (ListView) findViewById(R.id.list1); 
    db = new DBManager(this, null, null, 0); 
    data = db.getVeg(); // you would not typically call this on the main thread 
    @SuppressWarnings("deprecation") 
    ListAdapter adapter = new SimpleCursorAdapter(this, 
      R.layout.veg_item, 
      data, 
      new String[] {"name", "_id"}, 
      new int[] {R.id.textView1}); 
    lv.setAdapter(adapter); 
    lv.setOnItemClickListener(this); 
} 
@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    data.close(); 
    db.close(); 
} 
@Override 
public void onItemClick(AdapterView<?> arg0, View view, int i, long l) { 
    // TODO Auto-generated method stub 
    //Toast.makeText(this, id, Toast.LENGTH_LONG).show(); 
    //Intent in = new Intent(Veg.this, Veg_View.class); 
    //in.putExtra("id", String.valueOf(i)); /// ID need to passed here 
    //startActivity(in); 
} 

我需要這個ID傳遞到一個新的活動。 而在新的活動,我需要中檢索相應的數據

回答

0

這是轉移到其他活動

Intent in = new Intent(Veg.this, Veg_View.class); 
in.putExtra("id", String.valueOf(i)); /// ID need to passed here 
startActivity(in); 

發送數據在新的活動只需要使用這個

Intent in = getIntent(); 
Bundle bu = in.getExtras(); 
int id = in.getInt("id"); 
+0

它正在工作,但我沒有得到_id的價值, m db – 2014-10-20 00:09:05

+0

從OnItemClick獲得的值i表示適配器中的位置,而不是從數據庫中提取的值,這就是爲什麼您無法在其他活動中獲得正確值的原因,您需要從中獲取正確的項目適配器在那個位置(i),然後從適配器 – nunoh123 2014-10-20 01:27:44

+0

中獲得該對象的_id,我該怎麼做? – 2014-10-20 02:12:26

0
public void onItemClick(AdapterView<?> arg0, View view, int i, long l) 

正確最後一個參數是你想要的_id OnItemClick方法簽名是 public void onItemClick(AdapterView par耳鼻喉科,瀏覽視圖,INT位置,長ID)

parent是適配器

view是該項目的根視圖點擊

position是在Adapter

id該ID的位置適用於CursorAdapters中匹配項的適配器_id

相關問題