【问题标题】:Long Click on a list view and a delete dialog长按列表视图和删除对话框
【发布时间】:2012-12-30 03:04:03
【问题描述】:

我正在使用onlongitemclick 并且可以生成一个对话框来确认删除,但我无法获得listitem 的位置或文本。

编辑:我无法在 public void onClick(DialogInterface dialog, int which) 函数中获取 selectedValue 字符串值。

lv 是我的listview 对象

    lv.setOnItemLongClickListener(new OnItemLongClickListener() 
    {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
        {
             ListView list1 = (ListView) findViewById(android.R.id.list);
              final String selectedValue = (String) list1.getItemAtPosition(arg2);
                AlertDialog.Builder alertDialog = new  AlertDialog.Builder(RecipeList.this);
                alertDialog.setTitle("Delete");
                alertDialog.setMessage(selectedValue);     
                alertDialog.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {

                                obj Recipe = new obj(selectedValue, RecipeList.this);

                                Recipe.remove(<I need the listview item to create the object and then delete some listing in the DB, seletecValue should do this, but it does not>)
                                Intent intent2 = new Intent(RecipeList.this, RecipeList.class); //go to recipe list
                                startActivity(intent2);

                   } }); 
                   alertDialog.setPositiveButton("Keep", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {
                          // alertDialog.dismiss();
                   } }); 

                   alertDialog.show();
                return true;
        }
    });

【问题讨论】:

  • 试试这个:final String selectedValue = arg0.getItemAtPosition(arg2).toString();我认为 arg0、arg1 等名称不好,因此请将它们更改为文档中有意义的名称。
  • 当我调试并设置最终的 selectedValue 变量时,我得到了我所期望的。但是,当我单击删除按钮并在 public void onClick 中查看 selectedValue 时,我没有得到所选值的任何值

标签: android android-alertdialog


【解决方案1】:

arg1 是您的视图,您应该可以从视图中获取文本。

arg2 是位置。

见:http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html

【讨论】:

  • 当我调试并设置最终的 selectedValue 变量时,我得到了我所期望的。但是,当我单击删除按钮并在公共 void onClick 中查看 selectedValue 时,我没有得到所选值的任何值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多