Question

I want to show a layout which contain (edit & delete ) so when user longClick on an item of the list he will get a dialog to choose if he want to delete or to edit this item how can I do that ? and thinks

public class MissionAct extends ListActivity {
      private DbaseManager datasource;
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addmission_activity);
        datasource = new DbaseManager(this);
        datasource.open(getBaseContext());
        List<Missions> values = datasource.getAllMissions();
        ArrayAdapter<Missions> adapter = new ArrayAdapter<Missions>(this,
                android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);
        this.getListView().setLongClickable(true);
        this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> adpter, View v, int position, long id) {
                Dialog dialog = new Dialog(getBaseContext());
                dialog.setContentView(R.layout.dialg);
                dialog.setTitle("Nouveau point");
                dialog.show();
                return true;
            }
        });                      
 }
          }

and the error Log is

04-29 13:42:45.048: E/Babel(31576): canonicalizeMccMnc: invalid mccmnc nullnull
04-29 13:42:46.067: E/dalvikvm(31611): Could not find class 'android.app.AppOpsManager', referenced from method box.a
04-29 13:42:46.957: E/CellLocation(30121): create GsmCellLocation
04-29 13:42:47.412: E/ActivityManager(245): mtprof entry can not found!
04-29 13:42:47.412: E/ActivityManager(245): java.io.FileNotFoundException: /proc/mtprof/status: open failed: ENOENT (No such file or directory)
04-29 13:42:47.412: E/ActivityManager(245):     at libcore.io.IoBridge.open(IoBridge.java:448)
04-29 13:42:47.412: E/ActivityManager(245):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
04-29 13:42:47.412: E/ActivityManager(245):     at java.io.FileInputStream.<init>(FileInputStream.java:105)
04-29 13:42:47.412: E/ActivityManager(245):     at com.android.server.am.ActivityRecord.mtProf(ActivityRecord.java:852)
04-29 13:42:47.412: E/ActivityManager(245):     at com.android.server.am.ActivityRecord.windowsDrawn(ActivityRecord.java:653)
04-29 13:42:47.412: E/ActivityManager(245):     at com.android.server.am.ActivityRecord$Token.windowsDrawn(ActivityRecord.java:225)
04-29 13:42:47.412: E/ActivityManager(245):     at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:6994)
04-29 13:42:47.412: E/ActivityManager(245):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 13:42:47.412: E/ActivityManager(245):     at android.os.Looper.loop(Looper.java:154)
04-29 13:42:47.412: E/ActivityManager(245):     at com.android.server.wm.WindowManagerService$WMThread.run(WindowManagerService.java:754)
04-29 13:42:47.412: E/ActivityManager(245): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
Was it helpful?

Solution

You can implement it like this;

this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ctx);
    dialogBuilder.setTitle("Title");
    dialogBuilder.setMessage("Message?");
    dialogBuilder.setPositiveButton("Edit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
               //Edit
        }
    });
    dialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
               //Delete
        }
    });
    dialogBuilder.create().show();
        return true;
    }
});

And you can use position as index to access list element.

OTHER TIPS

You need to add an OnItemLongClickListener for the ListView like this, So that when an item is long clicked onItemLongClick will be called

getListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)      { 
            // do your other stuff here based on the list item position
            return true;
        }
    });
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
        //set your dialog here
        return true;
    }
});

Try this..

OnLongClickListener and getListView()

getListView().setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
});

EDIT

you can use Context Menu also refer below examples

How do you implement context menu in a ListActivity on Android?

Using contextmenu with listview in android

http://www.tktutorials.com/2013/06/android-context-menu-example.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top