1

Is there an easy way to delete items in a list using onLongPress?

I would like to use OnGestureListener - onLongPress to listen for longpress and to delete items in a list and update by adapter accordingly.

My problem is that if I use a custom adapter, then onlistItemclick and onItemLongClick conflict and longclicking does not trigger anything!

    public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
    Toast.makeText(listmodified.this, "A long click detected", Toast.LENGTH_SHORT).show();
    if (e.getAction()==MotionEvent.ACTION_DOWN) 
    { 

    OnItemLongClickListener itemDelListener = new OnItemLongClickListener(){

            //@Override
            public boolean onItemLongClick(AdapterView<?> parent, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                itemSelected=parent.getItemAtPosition(position).toString();

                adapter.remove(itemSelected);
                myList.remove(position);
                adapter.notifyDataSetChanged();
                Toast.makeText(listmodified.this, "IN LONGCLICK", Toast.LENGTH_SHORT).show();
                return false;
            }};


        longClickedItem = -1; 

    } 
PLNech
  • 3,087
  • 1
  • 23
  • 52
user803271
  • 115
  • 5
  • 16
  • What exactly is your question ? – Mojo Risin Jun 17 '11 at 13:35
  • I like that you called it a difficult question in the title and didn't ask a question in the actual post. EDIT: I found the question. Question marks are helpful Question: Is there an easy way to delete items in a list using onLongPress? – Matt Jun 17 '11 at 13:38
  • Hi Mojo, I am using an adapter which contains the following :- – user803271 Jun 17 '11 at 13:40
  • Hi Mojo, I am using an adapter which contains the following adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,myList). MyList is a simple array. Now I would like to remove items from the list when onLongPress is detected – user803271 Jun 17 '11 at 13:40
  • Hi Matt, This is my first post :) I hava a list tied to an ArrayAdapter. When onLongPress is detected I want to remove items from the lilst – user803271 Jun 17 '11 at 13:43
  • Yeah, we all start somewhere. I posted the start to an answer below. I'll try to post something more helpful soon. I'm not 100% sure whether you register it with the ListView or the Views associated with simple_list_item. – Matt Jun 17 '11 at 13:51

3 Answers3

1

Use the concept of Handler here.

Step1 Declare a constant

private static final byte UPDATE_LIST = 100;

Step2 Call the handler onclick of button

 OnItemLongClickListener itemDelListener = new OnItemLongClickListener(){

        //@Override
        public boolean onItemLongClick(AdapterView<?> parent, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub
            itemSelected=parent.getItemAtPosition(position).toString();

            adapter.remove(itemSelected);


                    Message msg = new Message();
                msg.what = UPDATE_LIST;
                msg.arg1 = position
                updateListHandler.sendMessage(msg);
            Toast.makeText(listmodified.this, "IN LONGCLICK", Toast.LENGTH_SHORT).show();
            return false;
        }};

Step3 Define the handler

private Handler updateListHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case UPDATE_LIST:
            int position = msg.arg1;
                list.remove(position);
                adapter.notifyDataSetChanged();
                break;

            }
            ;
        };
    };

See my response in How to update UI of listview

Community
  • 1
  • 1
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • Hi, there seems to be a problem with my declaration OnItemLongClickListener itemDelListener = new OnItemLongClickListener() I get back "The local variable itemDelListener is never read" – user803271 Jun 17 '11 at 14:17
0

You need to register the OnClickListener with your Views using View.setOnClickListener().

EDIT: A mediocre example of this can be found at this link:

http://www.androidsnippets.com/clickable-listview-items

page down to where it says activity and look through there. They implement their own class to do everything, but uses methods that are from the super class (and not part of the custom class) I can't promise this will for sure solve your problem though, but should be a step in the right direction.

Matt
  • 5,404
  • 3
  • 27
  • 39
0

Try This:

listView.setOnItemLongClickListener(new OnItemLongClickListener(){
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
            listView.removeViewAt(position);
            return false;
        }
    });
JDx
  • 2,615
  • 3
  • 22
  • 33
  • Hi, there seems to be a problem with my declaration **OnItemLongClickListener itemDelListener = new OnItemLongClickListener() I get back "The local variable itemDelListener is never read"** – user803271 Jun 17 '11 at 14:12
  • Where are you calling onLongPress() from? – JDx Jun 17 '11 at 14:16