2

i want to get the item from listview. but when i try to click listview i got error. can some body help me?

this my class activity.

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);

this is my log cat

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

my method

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;
}

my 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>

thank for helping me.. Regards..

androidDev
  • 141
  • 1
  • 4
  • 9

3 Answers3

4

Instead of

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

Use

@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));
        }
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • i have try ur code but i got the same error, can u please help me :( i really need the solution .. – androidDev May 06 '13 at 07:56
  • Did you change all your listCursor to cursor? – Hoan Nguyen May 06 '13 at 07:58
  • post DataPesanKeluar() method – Hoan Nguyen May 06 '13 at 08:08
  • i tried but my listview didn't respon when i click the list :( – androidDev May 06 '13 at 08:17
  • So now there is no more error but the list does not respond on click? – Hoan Nguyen May 06 '13 at 08:19
  • Post your baris_keluar layout. – Hoan Nguyen May 06 '13 at 08:24
  • In your listview layout add android:choiceMode="singleChoice" to your ListView – Hoan Nguyen May 06 '13 at 08:34
  • I think you should post another question like "ListView onItemClickListener does not fired" and post the complete code for onItemClickListener and I will continue to try to help but you would get other people having this problem before that may help you better than I. – Hoan Nguyen May 06 '13 at 08:48
  • Did you call startActivity(isiPesanKeluar) after the line isiPesanKeluar.putExtra("waktu", waktu);? – Hoan Nguyen May 06 '13 at 08:57
  • thanks for ur answer, it's really useful. and i'm sorry if i unaccurate my code clearly :) – androidDev May 06 '13 at 09:24
  • If your list still did not respond on click, put a log in onItemClick like Log.d("ActivityName", "onItemclick") and see if onItemClick being called. And post the question as I suggested. If nobody answer, leave a message here with the link to your post and I will take a look tomorrow as I get really late here and I am going to sleep. – Hoan Nguyen May 06 '13 at 09:29
  • You should unaccept my answer in your post at http://stackoverflow.com/questions/16382416/caused-by-java-lang-illegalargumentexception-column-id-does-not-exist as Yoann answer correctly a few second before my answer. An upvote is enough for me, I did upvote the Yoann answer after I wrote the answer and saw Yoann answer. – Hoan Nguyen May 06 '13 at 09:36
  • my list is working now, thanks to you :D if i had error again i'll contact you :D – androidDev May 06 '13 at 10:35
  • can u help me in my post? http://stackoverflow.com/questions/16474958/my-sms-receiver-cant-accept-new-sms – androidDev May 10 '13 at 04:33
1

To retrieve the item at position, using the a CursorAdapter, you can simple call Adapter.getItemAtPosition(position) casting the return object to Cursor. Afterwards you do not need to move it again. The returned value is what you have clicked:

@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
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
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();
        }
    });
Kao
  • 7,225
  • 9
  • 41
  • 65