从列表视图中删除元素(滑动或单击)

时间:2013-10-10 08:38:41

标签: java android android-listview listviewitem swipe-gesture

我正在尝试创建动态列表视图。我可以添加一个项目,但我现在无法将其删除。代码实际上非常简单,我看到的每个指南都对我和我的代码来说太复杂了。我希望在MainActivity中添加一些简单的东西来删除selceted项。我不在乎以哪种方式,像gmail或通过点击或任何其他方式滑动..我只是希望我以简单的方式删除列表中的元素。这是活动

public class MainActivity extends Activity {
    private EditText etInput;
    private Button btnAdd;
    private ListView lvItem;
    private ArrayList<String> itemArrey;
    private ArrayAdapter<String> itemAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setUpView();

    }

    private void setUpView() {
        // TODO Auto-generated method stub
        etInput = (EditText)this.findViewById(R.id.editText_input);
        btnAdd = (Button)this.findViewById(R.id.addbtn);
        lvItem = (ListView)this.findViewById(R.id.listView_items);


        itemArrey = new ArrayList<String>();
        itemArrey.clear();

        itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,itemArrey);
        lvItem.setAdapter(itemAdapter);


        btnAdd.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                addItemList();
            }
        });

        etInput.setOnKeyListener(new View.OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub

                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    addItemList();
                }
                return true;
            }
        });


    }

    protected void addItemList() {

    if (isInputValid(etInput)) {
        itemArrey.add(0,etInput.getText().toString());
        etInput.setText("");

        itemAdapter.notifyDataSetChanged();

    }   

    }

    protected boolean isInputValid(EditText etInput2) {
        // TODO Auto-generatd method stub
        if (etInput2.getText().toString().trim().length()<1) {
            etInput2.setError("Insert a value");
            return false;
        } else {
            return true;
        }

    }
}

是否可以插入部分代码来删除活动代码中的项目?感谢

8 个答案:

答案 0 :(得分:3)

试试这个,ListView项长ClickListener你可以做到

 lvItem.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v,
                int position, long id) {
            // TODO Auto-generated method stub
                                AlertDialog.Builder adb = new AlertDialog.Builder(
                    YourActivity.this);
            adb.setTitle("Are you sure want to delete this item");
            adb.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                            itemArrey.remove(position);
                            itemAdapter.notifyDataSetChanged();


                        }
                    });
            adb.setNegativeButton("NO",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                             dialog.dismiss();

                        }
                    });
            adb.show();

            return false;
        }
    });

答案 1 :(得分:2)

这应该对你有帮助。

       lv.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long id) {

                itemArrey.remove(position);

                itemAdapter.notifyDataSetChanged();  

                return true;
            }
        }); 

答案 2 :(得分:0)

将此方法放入onCreate:

lvItem .setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
        itemArrey.remove(position);
        itemAdapter.notifyDataSetChanged();
   });
}

答案 3 :(得分:0)

当然有可能。 ArrayList.html#remove

或者,如果您不知道索引只是遍历列表。 Calling remove in foreach loop in Java

答案 4 :(得分:0)

这样做..

lvItem.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int position, long id) {

                itemArrey.remove(position);
                itemAdapter.notifyDataSetChanged();  
                return true;

            }
        });

答案 5 :(得分:0)

要从列表中删除项目,只需执行一项操作,获取listView的onLongClickListener。 然后打开上下文菜单,其中有两个选项

  1. 删除

  2. 取消

  3. 当用户选择第一项时,从列表中删除它。然后调用listadapter notify datasetChanged方法。

答案 6 :(得分:0)

尝试以下代码:

单击:

listview .setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

            showDialog(int position);

       });
    }

public void showDialog(int position){
    AlertDialog alertDialog;
AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
    alertDialog = builder.create();
    alertDialog.setOnDismissListener(new myOnDismissListener());

    alertDialog.setTitle(TITLE OF DIALOG);
    alertDialog.setMessage(MESSAGE YOU WANT TO SHOW IN DIALOG);
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

                    YOUR ARRAY.remove(position);
                    YOUR ADAPTER.notifyDataSetChanged();

        }
    });
    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        }

    });
    alertDialog.show();
}

答案 7 :(得分:0)

使用这个库,我使用它并且它工作得非常好,如果你更喜欢Swipe试试这个:

https://github.com/47deg/android-swipelistview