请帮我建立了一个上下文菜单在AlertDialog [英] Please help me set up a Context Menu in an AlertDialog

查看:103
本文介绍了请帮我建立了一个上下文菜单在AlertDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这工作了一整天,我真的很接近,但就是无法得到这个工作。我有一个按钮,拉起填充保存的条目,包括名称和价格的AlertDialog。现在,我可以单击该对话框中的项目,并把它在我的活动名称和价格字段自动填写。我希望也能长preSS一个项目,并收到一个选项,将其删除。这是我第一次尝试在一个Android应用程序,和很多这是从记事本教程改变用途。有两件事情我想不通:

1)是我registerForContextMenu足够/正确吗?

2)什么是我与我的错onCreateContextMenu做?

感谢。

  savedItems.setOnClickListener(新View.OnClickListener(){
     公共无效的onClick(视图v){      cDbHelper.open();
      mNotesCursor = cDbHelper.fetchAllSaved();
            startManagingCursor(mNotesCursor);            //创建名称的数组和相应的价格从分贝
            的String [] =由新的String [] {Sa​​vedItemsDbAdapter.KEY_NAME,SavedItemsDbAdapter.KEY_PRICE};            //和字段的数组我们要绑定这些字段
            INT []为= INT新[] {R.id.text1,R.id.text2};            //现在创建一个简单的游标适配器,并将其设置为显示
            SimpleCursorAdapter保存=
                 新SimpleCursorAdapter(NewEntry.this,R.layout.saved_row,mNotesCursor,从,到);      //建立一个AlertDialog来保存这个名单
            AlertDialog.Builder建设者=新AlertDialog.Builder(NewEntry.this);
      builder.setTitle(从列表中选择);
      //这是足以注册上下文菜单?
      registerForContextMenu(五);
      builder.setAdapter(保存,新DialogInterface.OnClickListener(){       //当单击从列表中的项目,它会自动填充在活动的名称和价格领域
       @覆盖
       公共无效的onClick(DialogInterface对话,诠释项){
 光标C = mNotesCursor;
       c.moveToPosition(项目);
 意图I =新意图(NewEntry.this,NewEntry.class);
       i.putExtra(名,c.getString(
         c.getColumnIndexOrThrow(SavedItemsDbAdapter.KEY_NAME)));
       i.putExtra(价格,c.getString(
         c.getColumnIndexOrThrow(SavedItemsDbAdapter.KEY_PRICE)));
       startActivityForResult(ⅰ,ACTIVITY_AUTO);
       完();
}      //尝试和失败设置的上下文菜单 - 我们的目标是能够长期preSS,
      //有一个删除?选项​​弹出,这将删除该条目点击时
      @覆盖
      公共无效onCreateContextMenu(文本菜单菜单视图V,
    ContextMenuInfo menuInfo){
    super.onCreateContextMenu(菜单,V,menuInfo);
    menu.add(0,DELETE_ID,0,R.string.menu_delete);
   }   公共布尔onContextItemSelected(菜单项项){    开关(item.getItemId()){
       案例DELETE_ID:
        AdapterContextMenuInfo信息=(AdapterContextMenuInfo)item.getMenuInfo();
        mDbHelper.deleteItem(info.id);
        返回true;
    }
    返回false;
   }
      });      AlertDialog警报= builder.create();         alert.show();        }     });    }


解决方案

  

是我registerForContextMenu足够/正确吗?


您正在注册的任何上下文菜单 savedItems 是。如果这是你想要的上下文菜单中,那么一切都OK。

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

I've been working at this all day, and I'm really close but just can't get this to work. I have a button that pulls up an AlertDialog populated with saved entries that include Name and Price. Right now, I can click an item in the Dialog and have it automatically fill in the Name and Price fields in my activity. I want to also be able to long press an item and receive an option to delete it. This is my first try at an Android app, and a lot of this is repurposed from the Notepad Tutorial. Two things I can't figure out:

1) Is my registerForContextMenu sufficient/correct?

2) What am I doing wrong with my onCreateContextMenu?

Thanks.

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

        }

     });

    }

解决方案

Is my registerForContextMenu sufficient/correct?

You are registering a context menu for whatever savedItems is. If that is what you want the context menu on, then you are OK.

If your goal is to have a context menu on the items in the list in the dialog, your approach is wrong. You will not be able to use AlertDialog.Builder. You will need to create a custom subclass of AlertDialog, so you can override onCreateContextMenu() there.

这篇关于请帮我建立了一个上下文菜单在AlertDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆