2013-05-05 69 views
1

我想從listview中獲取項目。但是當我嘗試點擊listview我得到錯誤。 可以幫助我一些身體?如何在ListView中使用onItemClickListener獲取項目

這是我的課堂活動。

data = new dbHelper(this); 

    listKeluar = (ListView)findViewById(R.id.listKeluar); 

    String [] keluar = { data.k_nama, data.m_chiperteks, data.kel_waktu }; 
    int[] k = { R.id.tNama, R.id.tChiper, R.id.tWaktu }; 
    cursor = data.DataPesanKeluar(); 
    SimpleCursorAdapter keluarAdapter = new SimpleCursorAdapter(this, R.layout.baris_keluar, cursor, keluar, k); //this line my error 
    listKeluar.setAdapter(keluarAdapter); 
    listKeluar.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { 

      if(cursor.moveToPosition(arg2)) { 
       idkeluar = cursor.getString(cursor.getColumnIndex(data.kel_id));//my line error, if i delete this line my listview didn't respon when i click 
       nama = cursor.getString(cursor.getColumnIndex(data.k_nama)); 
       chiperteks = cursor.getString(cursor.getColumnIndex(data.m_chiperteks)); 
       waktu = cursor.getString(cursor.getColumnIndex(data.kel_waktu)); 
      } 

      Intent isiPesanKeluar = new Intent(); 
      isiPesanKeluar.setClass(KotakKeluar.this, IsiPesanKeluar.class); 
      isiPesanKeluar.putExtra("idkeluar", idkeluar); 
      isiPesanKeluar.putExtra("nama", nama); 
      isiPesanKeluar.putExtra("chiperteks", chiperteks); 
      isiPesanKeluar.putExtra("waktu", waktu); 

這是我的日誌貓

Java.lang.IllegalStateException: get field slot from row 0 col -1 failed 

我的方法

public Cursor DataPesanKeluar() { 
    Cursor c = dba.rawQuery(
      " SELECT " 
      + kel_id + " AS _id," 
      + e_chiperteks + "," 
      + k_nama + "," 
      + kel_waktu + 
      " FROM " + tbPesan + " INNER JOIN " + tbPesanKeluar + 
      " ON " + tbPesan + "." + p_idpesan + "=" + tbPesanKeluar + "." + kel_idpesan + 
      " INNER JOIN " + tbEnkrip + 
      " ON " + tbPesan + "." + p_idenkrip + "=" + tbEnkrip + "." + e_idenkrip + 
      " INNER JOIN " + tbKontak + 
      " ON " + tbPesan + "." + p_idkontak + "=" + tbKontak + "." + k_id , null); 
    return c; 
} 

我baris_keluar.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/tNama" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/tChiper" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/tWaktu" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Medium Text" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 

感謝幫助我.. 問候..

回答

3

而不是

Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2); 
     if(listCursor.moveToFirst()) 

使用

@Override 
    public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { 
      if(cursor.moveToPosition(arg2)) { 
      idkeluar = cursor.getString(cursor.getColumnIndex("_id")); 
      nama = cursor.getString(cursor.getColumnIndex(data.k_nama)); 
      chiperteks = cursor.getString(cursor.getColumnIndex(data.m_chiperteks)); 
      waktu = cursor.getString(cursor.getColumnIndex(data.kel_waktu)); 
     } 
+0

我有嘗試我們的代碼,但我得到了同樣的錯誤,可以ü請幫助我:(我真的需要的解決方案.. – androidDev 2013-05-06 07:56:48

+0

你改變你所有的listCursor to cursor? – 2013-05-06 07:58:28

+0

是的,我已經改變了像ur代碼,但它仍然是相同的錯誤 – androidDev 2013-05-06 08:06:35

0

要在位置檢索項目,使用一個CursorAdapter,你可以簡單的調用Adapter.getItemAtPosition(position)鑄造返回對象Cursor。之後你不需要再移動它。返回的值是你點擊什麼:

@Override 
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { 
    Cursor tmpCursor = arg0.getItemAtPosition(arg2); 
    // use the cursor to retrieve the content of the columns 
} 
0
eventlist.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) { 
      TextView eventname = (TextView) v.findViewById(android.R.id.text1); 
      Toast.makeText(v.getContext(),"eventname is" + eventname.getText().toString(),Toast.LENGTH_LONG).show(); 
     } 
    }); 
相關問題