OnItemClick侦听器中的Onclick侦听器问题

时间:2015-10-27 04:00:38

标签: java android listview

关于谷歌的OnItemclicklistener有很好的解释,但我仍然无法解决我的问题。 我需要删除项目列表视图,当我点击列表视图中的任何项目时,将弹出一个自定义对话框,其中包含删除按钮和更新按钮,当我单击该删除按钮时,需要删除列表视图中的该特定项目。但现在我点击删除按钮时出现异常。请帮我解决这个问题。

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.weddingplanning   E / AndroidRuntime:致命异常:主要       处理:com.example.username.weddingplanning,PID:1915       java.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)               at java.lang.reflect.Method.invoke(Native 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)

我的费用java类

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

2 个答案:

答案 0 :(得分:1)

简单而完美的解决方案

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

arg0是一个listview适配器..

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

使用arraylist对象代替arg0这样.....

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

答案 1 :(得分:0)

与日志一样:

  

ClassCastException:android.database.sqlite.SQLiteCursor不能   强制转换为com.example.username.weddingplanning.Expence

意味着arg0.getItemAtPosition(position);重新使用Cursor而不是Expence类对象。

要从Cursor获取所有数据到Expence类对象首先需要迭代游标,然后用{set}}类的用户setter方法来准备一个必需的对象:

Expence