同一个类上的setOnItemLongClickListener和setOnItemClickListener

时间:2016-08-07 17:54:37

标签: java android android-activity onclicklistener onlongclicklistener

我的问题是这个问题:当我长按项目时,我会看到两个视图。

请参阅下面的java类代码。请帮助。

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {

            Intent intent = new Intent(MyGoodsActivity.this, CurrentActivity.class);
            Bundle bundle = new Bundle();
            bundle.putLong(Record.BUNDLE_SHOP_ID, mShopId);
            bundle.putLong(Record.BUNDLE_the_ID, mGoodsAdapter.getItemId(position));
            intent.putExtras(bundle);
            MyGoodsActivity.this.startActivity(intent);
        }



    });

    mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {



        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {




            AlertDialog.Builder dialog = new AlertDialog.Builder(MyGoodsActivity.this);
            dialog.setTitle(R.string.tip);
            dialog.setMessage(R.string.info_confirm_delete_this_item);
            dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MyDB db = new MyDB(MyGoodsActivity.this);
                    db.deleteGoodsById(mGoodsAdapter.getItemId(position));
                    refreshListgoods();
                }
            });
            dialog.setNegativeButton(R.string.ko, null);
            dialog.show();
            return false;
        }
    }


    );

1 个答案:

答案 0 :(得分:1)

在分析了我的代码后,我将juste返回true以解决我的问题。

            dialog.setNegativeButton(R.string.ko, null);
        dialog.show();
        return true;
    }