Question

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

Was it helpful?

Solution

try this, While ListView item long ClickListener you can do it

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

OTHER TIPS

This should help you.

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
                   
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
        itemArrey.remove(position);     
        itemAdapter.notifyDataSetChanged();            
        return true;
    }
}); 

Put this method in your onCreate:

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

Of course it is possible. ArrayList.html#remove

Or if you don't know the index just iterate through the list. Calling remove in foreach loop in Java

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;

            }
        });

To delete item from list just do one thing, get onLongClickListener of listView. Then open the context menu which has two options

  1. Delete

  2. Cancel

When user picks first item delete it from list. Then call listadapter notify datasetChanged method.

Use this library, I used this and it works very fine, if you prefer Swipe try this:

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

Try below code:

single click:

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top