我想在longpressed时删除ListView上的项目

时间:2016-09-07 21:45:06

标签: java android listview long-press

我希望在longpressed时删除listView上的项目。在此代码中,您可以使用Button添加(使用edittext)列表项,如下所示。

我长时间无法编写删除它们的代码。我该怎么办?

理想情况下,长按将显示一个菜单,用户可以触摸并删除他们想要的部分。

$handle = fopen("curr_enroll_fall.csv", "r");
while (($data = fgetcsv($handle)) {
   printf( '%s <br />', $data[2] );
}
fclose($handle);

3 个答案:

答案 0 :(得分:4)

试试这个:

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

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

              adapter.remove(data[position]);//position of the item you click
              adapter.notifyDataSetChanged();

        return false;
    }

});

答案 1 :(得分:0)

你可以用动画做到:

listView.setOnItemLongClickListener(new OnItemLongClickListener() {

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

              adapter.remove(data[position]);//position of the item you click
              adapter.notifyItemRemoved(position);

        return false;
    }

});

答案 2 :(得分:0)

长按一次使用此列表视图项目删除,当您从列表视图中删除任何项目时,它应该是显示警告对话框

listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            removeItemFromList(position);
            return true;
        }

        private void removeItemFromList(int position) {

            final int deletePosition = position;

            AlertDialog.Builder alert = new AlertDialog.Builder(
                    this);

            alert.setTitle("Delete");
            alert.setMessage("Do you want delete this item?");
            alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TOD O Auto-generated method stub

                    // main code on after clicking yes
                    songsList.remove(deletePosition);
                    songAdapter.notifyDataSetChanged();
                    songAdapter.notifyDataSetInvalidated();

                }
            });
            alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });

            alert.show();
        }