如何从其他活动中更改menuitem的可见性?

时间:2014-02-26 23:32:40

标签: java android database eclipse

任何人都可以告诉我如何从其他活动中更改menuitem的可见性吗? 我有两个活动“活动A和B”。在一个活动A中,当我按下菜单项时,它会将一些字符串保存到活动B列表中,并且在活动中将menuitem可见性设置为false。现在我希望当我从活动B中删除我从活动A保存的那个项目时,通过删除活动A中的menuitem可见性变为true并再次可见?所以我该怎么做呢我使用数据库来填充listview。

活动A

 public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.atherosclerosis, menu);


        return true;
    }


    // for starting activity from the option or menu//
      @Override
        public boolean onOptionsItemSelected(MenuItem item) {


          SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(this);
          final SharedPreferences.Editor editor = myPrefs.edit();
          favClicked = myPrefs.getBoolean("menu_item", false);



          switch (item.getItemId()) {


            case R.id.id_favorit:
// Add it to the DB and re-draw the ListView
                myDb.insertRow("Atherosclerosis", 0, "");
                Toast.makeText(getApplicationContext(), "Item Added to favorite list!", Toast.LENGTH_SHORT).show();


                favClicked=true;
                editor.putBoolean("menu_item", favClicked);
                editor.commit();
                invalidateOptionsMenu();
                return true;

            case R.id.id_favorit2:
                myDb.deleteRow("Atherosclerosis");
                Toast.makeText(getApplicationContext(), "Item deleted from favorite list!", Toast.LENGTH_SHORT).show();

                favClicked=false;
                editor.putBoolean("menu_item", favClicked);
                editor.commit();
                invalidateOptionsMenu();
                return super.onOptionsItemSelected(item); 
            }
        return true;
        }
           @Override
            public boolean onPrepareOptionsMenu(Menu menu) {


         if(favClicked==true){
               menu.findItem(R.id.id_favorit).setVisible(false);
                menu.findItem(R.id.id_favorit2).setVisible(true);

         }else{
           menu.findItem(R.id.id_favorit).setVisible(true);
            menu.findItem(R.id.id_favorit2).setVisible(false);

     }

活动B

private void populateListViewFromDB() {
        Cursor cursor = myDb.getAllRows();

        // Allow activity to manage lifetime of the cursor.
        // DEPRECATED! Runs on the UI thread, OK for small/short queries.
        startManagingCursor(cursor);

        // Setup mapping from cursor to view fields:
        String[] fromFieldNames = new String[] 
                {DBAdapter.KEY_NAME, DBAdapter.KEY_STUDENTNUM};
        int[] toViewIDs = new int[]
                {R.id.item_name};

        // Create adapter to may columns of the DB onto elemesnt in the UI.
        SimpleCursorAdapter myCursorAdapter = 
                new SimpleCursorAdapter(
                        this,       // Context
                        R.layout.item_layout,   // Row layout template
                        cursor,                 // cursor (set of DB records to map)
                        fromFieldNames,         // DB Column names
                        toViewIDs               // View IDs to put information in
                        );

        // Set the adapter for the list view
        ListView myList = (ListView) findViewById(R.id.favlistView1);
        myList.setAdapter(myCursorAdapter);
    }

    private void registerListClickCallback() {
        ListView myList = (ListView) findViewById(R.id.favlistView1);



        //This code is for to delete the single item from the listview of favorite list
        myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, final long arg3) {
                Cursor cursor = myDb.getRow(arg3);
                if (cursor.moveToFirst()) {


                        new AlertDialog.Builder(FavoriteDiseases.this)
                        .setTitle("Delete Item")
                        .setMessage("Do you want to delete this disease?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) { 
                                // continue with delete
                                myDb.deleteItem(arg3);
                                populateListViewFromDB();
                        }
                     })
                    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) { 
                            // do nothing
                        }
                     })
                     .show();            
    }
                return true;

            }

        });

1 个答案:

答案 0 :(得分:0)

需要更多的背景以获得良好的答案相关的活动是如何呼叫另一个?

但一般来说,如果活动a是呼叫活动b。然后你可以在启动活动b时调用startActivityForResult。当b完成后,返回一个状态,通知活动a该项目已被删除。

要假设您通过覆盖onCreateOptionsMenu来创建菜单来更新菜单,那么只需使用该方法检查标志以设置菜单项的状态。然后在你的onActivityResult方法中,根据活动b的结果设置标志,并调用invalidateOptionsMenu(),它将重绘你的选项菜单。