如何设置onListItemLongClick以从数据库列出?

时间:2014-04-29 00:28:37

标签: android android-listview onlongclicklistener

我想显示一个包含(编辑和删除)的布局,所以当用户 longClick 对列表中的某个项目进行选择时,他会得到一个对话框,用于选择是否要删除或编辑此项目项目 我怎样才能做到这一点 ?并认为

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

错误日志

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)

4 个答案:

答案 0 :(得分:0)

您需要像这样为OnItemLongClickListener添加ListView,以便在长按项目时onItemLongClick将被调用

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

答案 1 :(得分:0)

试试这个..

OnLongClickListenergetListView()

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

修改

您可以使用Context Menu也参考以下示例

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

答案 2 :(得分:0)

你可以像这样实现它;

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

您可以使用position作为索引来访问列表元素。

答案 3 :(得分:0)

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