0

I'm trying to create a dynamic listview. I can add an item but i can't remove it right now. The code actually it's very simple and every guide i saw are too much complicated for me and my code. I want something simple to add in my MainActivity to remove the item selceted. I don't care in which way, swipe like gmail or by click or any other way.. I just want i simple way to remove an element of the list. This is the Activity

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;
        }

    }
}

Is it possible insert some part of code to remove an item inside my activity code? Thanks

4

8 回答 8

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;
        }
    });
于 2013-10-10T08:50:31.657 回答
2

这应该可以帮助你。

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
                   
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
        itemArrey.remove(position);     
        itemAdapter.notifyDataSetChanged();            
        return true;
    }
}); 
于 2013-10-10T08:47:26.433 回答
0

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

  1. 删除

  2. 取消

当用户选择第一个项目时,将其从列表中删除。然后调用 listadapter notify datasetChanged 方法。

于 2013-10-10T09:01:30.703 回答
0

Do this way..

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

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

            }
        });
于 2013-10-10T08:58:28.390 回答
0

当然这是可能的。 ArrayList.html#remove

或者,如果您不知道索引,只需遍历列表即可。 在 Java 的 foreach 循环中调用 remove

于 2013-10-10T08:52:02.520 回答
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();
}
于 2013-10-10T09:03:42.757 回答
0

使用这个库,我用过它,它工作得很好,如果你喜欢 Swipe 试试这个:

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

于 2013-10-10T09:16:51.270 回答
0

将此方法放在您的 onCreate 中:

lvItem .setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
        itemArrey.remove(position);
        itemAdapter.notifyDataSetChanged();
   });
}
于 2013-10-10T08:47:12.560 回答