android - 长按 ListView 和删除对话框

标签 android android-alertdialog

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

编辑:我无法在 public void onClick(DialogInterfacedialog, 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;
        }
    });

最佳答案

arg1 是您的 View ,您应该能够从 View 中获取文本。

arg2 是位置。

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

关于android - 长按 ListView 和删除对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14347497/

相关文章:

android - 使用android :elevation在LinearLayout顶部转换阴影

android - 为警报对话框设置 Android Theme.Light

java - 如何设置连接等待提醒?

android - 当 state_pressed = true 时更改 <selector> 中的文本大小

java - Android Wear与智能手机之间数据传输的安全性

android - 膨胀后 subview 为空

android - Intent 在 Android 中进行文档签名

java - 如何从单独的类中的对话框返回值

android - 正确设置有关 Activity 关闭的警报对话框?

android - 从网上获取一个.txt,对比一下,在一个AsyncTask中打开一个dialog