【问题标题】:How can i pass item position from onItemLongClick to another class?如何将项目位置从 onItemLongClick 传递到另一个类?
【发布时间】:2017-03-21 18:55:45
【问题描述】:

我的问题是:如何将项目位置传递给另一个班级。请看我附上的代码。

当我在 AlertDialog 中按下肯定按钮时,我转到 HandleAlertDialog 类,然后我按 id 删除名称(在数据库中我有两个变量:id 和名称)。

我不知道如何将 id 从项目位置传递给 HandleAlertDialog 类中的 sqhelper.deleteNameByNumber(id)。

感谢您的帮助。

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        AlertDialog dialog = new AlertDialog.Builder(this) 
            .setMessage("Choose one of the options")
            .setPositiveButton("Yes", new HandleAlertDialog())
            .setNeutralButton("No",new HandleAlertDialog())
            .setCancelable(false)
            .create();
        dialog.show();
        return false;
      }

    public  class  HandleAlertDialog implements DialogInterface.OnClickListener {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which==-1){
                sqhelper.deleteNameByNumber(**???**);
            }
        }
    }

【问题讨论】:

    标签: android sqlite listview onitemlongclicklistener


    【解决方案1】:

    您可以在HandleAlertDialog 类中定义id 属性(我假设这是一个String 示例):

    public  class  HandleAlertDialog implements DialogInterface.OnClickListener {
        private String id;
    
        public HandleAlertDialog(String id){
            this.id = id;
        }
    
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which==-1){
                sqhelper.deleteNameByNumber(id);
            }
        }
    }
    

    然后在你的onItemLongClick中使用它:

    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    
        // Retrieve id here (based on view, position ?)
        String id = "THIS IS YOUR ID";
        HandleAlertDialog handleAlertDialog = new HandleAlertDialog(id);
    
        AlertDialog dialog = new AlertDialog.Builder(this) 
            .setMessage("Choose one of the options")
            .setPositiveButton("Yes", handleAlertDialog)
            .setNeutralButton("No", handleAlertDialog)
            .setCancelable(false)
            .create();
        dialog.show();
        return false;
    }
    

    【讨论】:

    • 如何从项目位置检索 id?我有两个变量(id,name)的类名。
    • 使用项目位置从您用来显示它们的列表中获取Nameobject,我无法提供更多帮助,因为我没有名称和列表视图适配器
    【解决方案2】:

    只需在构造函数中传递 id。

    public boolean onItemLongClick (AdapterView parent, View view,int position, final long id){
            AlertDialog dialog = new AlertDialog.Builder(this)
    
                    .setMessage("Choose one of the options")
                    .setPositiveButton("Yes", new HandleAlertDialog(id))
                    .setNeutralButton("No", new HandleAlertDialog(id))
                    .setCancelable(false)
                    .create();
            dialog.show();
    
            return false;
        }
    

    添加构造函数。

    public  class  HandleAlertDialog implements DialogInterface.OnClickListener
    {
        long id;
    
        public HandleAlertDialog(long id) {
            this.id = id;
        }
    
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which==-1){
                sqhelper.deleteNameByNumber(id);
            }
        }
    }
    

    如果不清楚,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 2013-03-16
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多