2014-09-01 47 views
1

我有一個應用程序,它已經有了ListView連接到SQL數據庫... 現在我要更新的應用程序來使用CardsUI https://github.com/nadavfima/cardsui-for-android獲取ID內onContextItemSelected與CardsUI

該代碼已經工作罰款直到現在:

public boolean onContextItemSelected(final MenuItem item) { 
    switch (item.getItemId()) { 
     case DONE_ID: 
      AdapterContextMenuInfo infod = (AdapterContextMenuInfo) item 
        .getMenuInfo(); 
      long infodId = infod.id; 
      Cursor items = dbAdapter.getMyItem(infodId); 
      startManagingCursor(items); 

      String title = items.getString(items 
        .getColumnIndexOrThrow(DbAdapter.KEY_TITLE)); 
      String description = items.getString(items 
        .getColumnIndexOrThrow(DbAdapter.KEY_DESCRIPTION)); 
      int priority = items.getInt(items 
        .getColumnIndexOrThrow(DbAdapter.KEY_PRIORITY)); 

      dbAdapter.updateList(infodId, priority, title, 
        description); 
      return true; 
     case EDIT_ID: 
      AdapterContextMenuInfo idInfo = (AdapterContextMenuInfo) item 
        .getMenuInfo(); 
      long idInfolong = idInfo.id; 
      Intent i = new Intent(this, Detail.class); 
      i.putExtra(DbAdapter.KEY_ROWID, idInfolong); 
      startActivityForResult(i, ACTIVITY_EDIT); 
      return true; 
    } 
    return super.onContextItemSelected(item); 
} 

public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; 

    menu.add(0, EDIT_ID, 0, R.string.menu_edit); 
    menu.add(0, DONE_ID, 0, R.string.menu_done); 
} 

我該如何使用此代碼與卡?它總是崩潰。我讀過,如果你管理一個SQL遊標,它連接到MySQL。我如何獲得onContextItemSelected內的記錄ID?

這是我的logcat輸出:

10-22 19:28:37.213: E/AndroidRuntime(3087): FATAL EXCEPTION: main 
10-22 19:28:37.213: E/AndroidRuntime(3087): Process: com.myapp.mylist, PID: 3087 
10-22 19:28:37.213: E/AndroidRuntime(3087): java.lang.NullPointerException 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.myapp.mylist.MainActivtiy.onContextItemSelected(MainActivity.java:395) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.app.Activity.onMenuItemSelected(Activity.java:2620) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:350) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.support.v7.app.ActionBarActivity.onMenuItemSelected(ActionBarActivity.java:155) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3864) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.widget.AdapterView.performItemClick(AdapterView.java:299) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.widget.AbsListView.performItemClick(AbsListView.java:1113) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.widget.AbsListView$3.run(AbsListView.java:3645) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.os.Handler.handleCallback(Handler.java:733) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.os.Handler.dispatchMessage(Handler.java:95) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.os.Looper.loop(Looper.java:136) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at android.app.ActivityThread.main(ActivityThread.java:5001) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at java.lang.reflect.Method.invoke(Method.java:515) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
10-22 19:28:37.213: E/AndroidRuntime(3087):  at dalvik.system.NativeStart.main(Native Method) 
+0

它是如何崩潰的? – danny117 2014-10-22 19:27:25

+0

對不起,我更新了我的回答 – user754730 2014-10-22 19:31:04

+0

你能粘貼你的MainActivity代碼嗎?至少你的onContextItemSelected代碼 – 2014-10-22 19:34:21

回答

1

SQL _id爲onContextItemSelected()

onCreateContextMenu()通過適配器獲得該項目的數據。然後將itent附加到包含sql行的_id的菜單項。 onContextItemSelected現在有一個附加到包含sql行_id的menuitem的意圖。

@Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 

     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; 
      String name = adapter2.getItem(info.position).get_name(); 
      menu.setHeaderTitle(name); 
      MenuInflater inflater = this.getActivity().getMenuInflater(); 
      inflater.inflate(R.menu.menulistitem, menu); 
      MenuItem mi = menu.findItem(R.id.action_context_delete); 
      Intent i = new Intent(); 
      //put the database _id into the intent for the oncontextitemselected 
      i.putExtra("id", adapter2.getItem(info.position).get_id()); 
      mi.setIntent(i); 

    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 

     switch (item.getItemId()) { 
     case R.id.action_context_delete: 
      Intent i = item.getIntent(); 
      if (i != null) { 
       Bundle b = i.getExtras(); 
       if (b != null) { 
        int id = b.getInt("id"); 
        Uri deleteIdUri = ContentUris.withAppendedId(
          RidesDatabaseProvider.CONTENT_URI, id); 
        context.getContentResolver() 
          .delete(deleteIdUri, null, null); 
        return true; 
       } 
      } 
     } 
     return super.onContextItemSelected(item); 
    } 
+0

有同樣的問題,我通過在menuitem意圖中傳遞_id來解決它 – danny117 2014-10-22 20:01:44

+0

你可以看看我的代碼嗎?我不知道這將如何工作在我的代碼... – user754730 2014-10-22 21:06:54

+0

看看我如何使用列表的適配器的getItem info.postion。您至少需要將info.postion保存到intent中,然後將該意圖附加到menuitem。然後在您選定的oncontextitem中,您實際上知道存儲在意圖中的值選擇了哪個項目。 – danny117 2014-10-23 13:25:03