OnItemClick监听器内部的Onclick监听器问题



关于谷歌的OnItemclicklistener有很好的解释,但我仍然无法解决我的问题。我需要删除项目listview,当我点击任何项目在listview,一个自定义对话框会弹出删除按钮和更新按钮,当我点击那个删除按钮,它需要删除特定的项目在listview。但现在删除按钮点击时出现异常。请帮我解决这个问题。

rldlist3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> arg0, View arg1, final int position, long arg3) {
                // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom);
                dialog.setTitle("Manage Expenses");
                // set the custom dialog components
                Button up = (Button) dialog.findViewById(R.id.Update);
                Button del= (Button) dialog.findViewById(R.id.Delete);
                // if button is clicked, close the custom dialog
                up.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                del.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Expence row = (Expence) arg0.getItemAtPosition(position);
                        selected_id = row.getE_id();
                       System.out.println(selected_id);
                        db = helper.getWritableDatabase();
                        db.delete(DBhelper.TABLE3, DBhelper.ID1 + "=?", new String[]{selected_id});
                        db.close();
                        fetchData3();
                        Toast.makeText(List_activity.this, "successfully deleted", Toast.LENGTH_LONG).show();

                    }
                });
                dialog.show();
            }
        });

10-27 09:07:16.926 1915-1915/com.example.username.weddingplanningE/AndroidRuntime: FATAL EXCEPTION: main过程:com.example.username。婚礼策划,PID: 1915java.lang.ClassCastException: android.database.sqlite.SQLiteCursor不能被强制转换为com.example.username.weddingplanning.Expence在com.example.username.weddingplanning.List_activity 1美元2.美元onclick (List_activity.java: 73)android.view.View.performClick (View.java: 4756)android.view.View PerformClick.run美元(View.java: 19749)android.os.Handler.handleCallback (Handler.java: 739)android.os.Handler.dispatchMessage (Handler.java: 95)android.os.Looper.loop (Looper.java: 135)android.app.ActivityThread.main (ActivityThread.java: 5221)在java.lang.reflect.Method。调用(本地方法)java.lang.reflect.Method.invoke (Method.java: 372)com.android.internal.os.ZygoteInit MethodAndArgsCaller.run美元(ZygoteInit.java: 899)com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 694)

My expense java class

public class Expence {
    private String category;
    private String description;
    private String amount;
    private String date;
    private String status;
    private String year;
    private String month;
    public Expence(String e_id) {
        this.e_id = e_id;
    }
    private String e_id;
    public String getE_id() {
        return e_id;
    }
    public void setE_id(String e_id) {
        this.e_id = e_id;
    }
    //get methods to access these variables
    public String getCategory() {
        return category;
    }
    //set methods to modify them
    public void setCategory(String category) {
        this.category = category;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }

    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getMonth() {
        return month;
    }
    public void setMonth(String month) {
        this.month = month;
    }
}

简单而完美的解决方案是

Expence row = (Expence) arg0.getItemAtPosition(position);    //This is an error

arg0是一个listview适配器。

从sqlite获取数据并将其存储在arraylist中,并将其设置为适配器。完成后,进行以下更改

使用数组列表对象代替arg0,如下.....

Expence row = (Expence) your_arraylist.getItemAtPosition(position);

日志:

ClassCastException: android.database.sqlite.SQLiteCursor cannot becast to com.example.username.weddingplanning.Expence

表示arg0.getItemAtPosition(position);返回一个游标而不是Expence的类对象。

要获得从游标到Expence类对象的所有数据,首先需要迭代游标,然后Expence类的用户setter方法来准备所需的对象:

Cursor cursor=arg0.getItemAtPosition(position);
Expence row ;
if (cursor.moveToFirst()) {
    do {
        // do what you need with the cursor here
        row=new Expence(<ID>);
        row.setCategory(cursor.getString(cursor.getColumnIndex("Category")));
        ... Do same for other
    } while (cursor.moveToNext());
}

最新更新