2

我想从列表视图中获取项目。但是当我尝试单击 listview 时出现错误。一些身体可以帮助我吗?

这是我的课堂活动。

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>

谢谢你帮助我..问候..

4

3 回答 3

4

代替

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));
        }
于 2013-05-05T22:08:11.590 回答
1

要在位置检索项目,使用 a 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
}
于 2014-04-29T07:50:22.510 回答
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();
        }
    });
于 2014-09-11T08:45:53.853 回答