长按列表视图和删除对话框

时间:2013-01-15 21:44:28

标签: android android-alertdialog

我正在使用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;
        }
    });

1 个答案:

答案 0 :(得分:2)

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

arg2是职位。

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