请帮我在AlertDialog中设置一个Context菜单

时间:2010-11-13 21:24:08

标签: java android sqlite

我一整天都在这里工作,而且我真的很接近,但却无法让它发挥作用。我有一个按钮,用于提取一个AlertDialog,其中填充了包含Name和Price的已保存条目。现在,我可以单击对话框中的某个项目,让它自动填写我的活动中的名称和价格字段。我想也能够长按一个项目并收到删除它的选项。这是我在Android应用程序中的第一次尝试,其中很多都是从Notepad Tutorial中重新使用的。我无法弄清楚的两件事:

1)我的registerForContextMenu是否足够/正确?

2)我的onCreateContextMenu出了什么问题?

感谢。

savedItems.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {

      cDbHelper.open();
      mNotesCursor = cDbHelper.fetchAllSaved();
            startManagingCursor(mNotesCursor);

            // Create an array of names and corresponding prices from db
            String[] from = new String[]{SavedItemsDbAdapter.KEY_NAME, SavedItemsDbAdapter.KEY_PRICE};

            // and an array of the fields we want to bind those fields to
            int[] to = new int[]{R.id.text1, R.id.text2};

            // Now create a simple cursor adapter and set it to display
            SimpleCursorAdapter saved = 
                 new SimpleCursorAdapter(NewEntry.this, R.layout.saved_row, mNotesCursor, from, to);

      // Build an AlertDialog to hold this list
            AlertDialog.Builder builder = new AlertDialog.Builder(NewEntry.this);
      builder.setTitle("Choose from list");
      // IS THIS SUFFICIENT TO REGISTER FOR CONTEXT MENU?
      registerForContextMenu(v);
      builder.setAdapter(saved, new DialogInterface.OnClickListener() {

       // When an item from the list is clicked, it automatically populates the name and price fields in activity
       @Override
       public void onClick(DialogInterface dialog, int item) {
 Cursor c = mNotesCursor;
       c.moveToPosition(item);
 Intent i = new Intent(NewEntry.this, NewEntry.class);
       i.putExtra("name", c.getString( 
         c.getColumnIndexOrThrow(SavedItemsDbAdapter.KEY_NAME)));
       i.putExtra("price", c.getString(
         c.getColumnIndexOrThrow(SavedItemsDbAdapter.KEY_PRICE)));
       startActivityForResult(i, ACTIVITY_AUTO);
       finish();
}

      // TRYING AND FAILING TO SET UP A CONTEXT MENU - the goal is to be able to long press, 
      // have a "Delete?" option pop up, which will delete the entry when clicked 
      @Override
      public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
   }

   public boolean onContextItemSelected(MenuItem item) {

    switch(item.getItemId()) {
       case DELETE_ID:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        mDbHelper.deleteItem(info.id);
        return true;
    }
    return false;
   }
      });

      AlertDialog alert = builder.create();

         alert.show();

        }

     });

    }

3 个答案:

答案 0 :(得分:1)

我刚刚找到

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {

被召唤,但不是

@Override
public boolean onContextItemSelected(MenuItem item) {

在我的子类AlertDialog中:

public class MyAlertDialog extends AlertDialog implements
OnCreateContextMenuListener {

也许这对其他人有用,因为我很确定你已经解决了手头的问题。

答案 1 :(得分:1)

您只需要实现以下功能。它会起作用。

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
{
    Log.e(LOGTAG, "Tao menu");
    if(v == expList)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        //AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) menuInfo;

        // We know that each row in the adapter is a Map
        //HashMap map =  (HashMap) simpleAdpt.getItem(aInfo.position);
        menu.setHeaderTitle("Options");
        menu.add(1, 1, 1, "Reprint");
        menu.add(1, 2, 1, "Void");

        menu.getItem(0).setOnMenuItemClickListener(new OnMenuItemClickListener()
        {
            public boolean onMenuItemClick(MenuItem clickedItem)
            {
                return true;
            }
        });

        menu.getItem(1).setOnMenuItemClickListener(new OnMenuItemClickListener()
        {
            public boolean onMenuItemClick(MenuItem clickedItem)
            {
                return true;
            }
        });
    }
}

答案 2 :(得分:0)

  

我的registerForContextMenu是否足够/正确?

您正在注册任何savedItems的上下文菜单。如果您想要上下文菜单,那么您就可以了。

如果你的目标是在对话框的列表中的项目上有一个上下文菜单,那么你的方法是错误的。您将无法使用AlertDialog.Builder。您需要创建AlertDialog的自定义子类,以便在那里覆盖onCreateContextMenu()