1

我创建了一个上下文菜单。当我长按列表项时,会出现上下文菜单。到现在为止还挺好...

但是当我点击一个上下文项时,什么也没有发生。有谁知道这个问题?这里有什么问题?

使用 listview 打开对话框的按钮:

        Button cmd_fav = (Button) findViewById(R.id.cmd_main_fav);
    cmd_fav.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            List<String> valueList = new ArrayList<String>();

            db = SQLiteDatabase.openDatabase("/data/data/spicysoftware.abugrundwissen/databases/questions", null,
                    SQLiteDatabase.OPEN_READWRITE);
            Cursor c_ = db.rawQuery("SELECT question, _id, answer FROM tbl_questions"+
                    " where favourite = 1", null);


            if (c_ != null ) {
                if  (c_.moveToFirst()) {
                    do {
                        String str_question = c_.getString(c_.getColumnIndex("question"));            
                        valueList.add(str_question);                        
                  } while (c_.moveToNext());
                }

                // custom dialog
                dialog = new Dialog(MainSite.this);
                dialog.setContentView(R.layout.dialog_list);
                dialog.setTitle("Favoriten:");
                adapter = new ArrayAdapter<String>(MainSite.this, android.R.layout.simple_list_item_1, valueList);
                final ListView lv = (ListView)dialog.findViewById(R.id.list_search);

                lv.setAdapter(adapter);

                registerForContextMenu(lv);

                lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {


                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                         menu.add(Menu.NONE, CONTEXT_MENU_DELETE_ITEM, Menu.NONE, "Favorit entfernen");
                         menu.add(Menu.NONE, CONTEXT_MENU_FINISH_ITEM, Menu.NONE, "Frage abschliessen!");
                    }
                });



                lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                lv.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                        String item = (String) lv.getItemAtPosition(position).toString();

                        Cursor c_2 = db.rawQuery("SELECT answer FROM tbl_questions"+
                                " where question = '"+item+"'", null);

                        if (c_2 != null ) {
                            if  (c_2.moveToFirst()) {
                                    answer = c_2.getString(c_2.getColumnIndex("answer"));   
                              }
                            }

                        // custom dialog
                        final Dialog dialog = new Dialog(MainSite.this);
                        dialog.setContentView(R.layout.dialog_answer);
                        dialog.setTitle("Antwort:");

                        // set the custom dialog components - text, image and button
                        TextView text = (TextView) dialog.findViewById(R.id.txt_answer);
                        //text.setText(answer);
                        text.setText(Html.fromHtml(answer), TextView.BufferType.SPANNABLE);

                        Button dialogButton = (Button) dialog.findViewById(R.id.cmd_close_dialog);
                        // if button is clicked, close the custom dialog
                        dialogButton.setOnClickListener(new OnClickListener() {

                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });

                        dialog.show();

                       }

               });



                Button dialogButton = (Button) dialog.findViewById(R.id.cmd_close_dialog2);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();


           }




        }


    });

OnContextItemSelected:

    @Override  
public boolean onContextItemSelected(MenuItem item) { 
    Log.v("tst", "lol");
switch (item.getItemId()) {

      case CONTEXT_MENU_DELETE_ITEM:
          Log.v("DELETED", "TRUE");
          return true;

      case CONTEXT_MENU_FINISH_ITEM:
        Log.v("FINISHED", "TRUE");
          return true;

  }
 Log.v("FINISHED", "LOL");
return false;


  }

最好的问候 MSeiz5

4

2 回答 2

0

我在这里找到了解决方案Android: ContextMenu and ItemSelected in Context Menu

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
          super.onCreateContextMenu(menu, v, menuInfo);
          //MenuInflater inflater = getMenuInflater();
          //inflater.inflate(R.menu.context_menu, menu);

          MenuItem delete = menu.add("delete");
          MenuItem add = menu.add("add");
          add.setIcon(android.R.drawable.ic_menu_upload); //adding icons
          delete.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                  public boolean onMenuItemClick(MenuItem item) {
                      Log.d("ContextCheck","EDIT!");
                        Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
                          return true;
                  }
          });
          add.setOnMenuItemClickListener(new OnMenuItemClickListener() {
              public boolean onMenuItemClick(MenuItem item) {
                  Log.d("ContextCheck","EDIT!");
                    Toast.makeText(Pr.this, "Edit!", Toast.LENGTH_SHORT).show();
                  return true;
              }
      });
        }

正在工作中!

于 2013-01-23T15:25:20.713 回答
0

如果使用 MenuInflater,更通用的代码:

MenuInflater inflater = getMenuInflater();     
        inflater.inflate(R.menu.ruleitem_menu, menu); 
        if (menuInfo instanceof AdapterView.AdapterContextMenuInfo){
            AdapterView.AdapterContextMenuInfo adptrCmi = (AdapterContextMenuInfo) menuInfo;
            String lsItem =  currentRuleListView.getItemAtPosition(adptrCmi.position).toString();
            menu.setHeaderTitle( lsItem);
        }

        //if Activity.onContextItemSelected not triggered, try the following lines
        for (int i=0; i< menu.size();i++){
            menu.getItem(i).setOnMenuItemClickListener(new OnMenuItemClickListener(){

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    return onContextItemSelected(item);
                }

            });
        }
于 2013-03-13T09:47:17.123 回答