Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从其他活动更改菜单项的可见性?_Java_Android_Database_Eclipse - Fatal编程技术网

Java 如何从其他活动更改菜单项的可见性?

Java 如何从其他活动更改菜单项的可见性?,java,android,database,eclipse,Java,Android,Database,Eclipse,有人能告诉我如何从其他活动更改菜单项的可见性吗? 我有两个活动“活动A和B”。在一个活动A中,当我按下菜单项时,它会将一些字符串保存到活动B的列表中,在活动A中,菜单项可见性设置为false。现在我想知道,当我从活动B中删除我从活动A中保存的项目时,使用此删除操作,活动A中的菜单项可见性将更改为true,并再次变为可见?那么我该怎么做呢。我使用数据库填充listview 活动A public boolean onCreateOptionsMenu(Menu menu) { //

有人能告诉我如何从其他活动更改菜单项的可见性吗? 我有两个活动“活动A和B”。在一个活动A中,当我按下菜单项时,它会将一些字符串保存到活动B的列表中,在活动A中,菜单项可见性设置为false。现在我想知道,当我从活动B中删除我从活动A中保存的项目时,使用此删除操作,活动A中的菜单项可见性将更改为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;

            }

        });
private void populateListViewFromDB(){
Cursor=myDb.getAllRows();
//允许活动管理游标的生存期。
//不推荐!在UI线程上运行,对于小/短查询可以。
开始管理游标(游标);
//设置从光标到视图字段的映射:
字符串[]fromFieldNames=新字符串[]
{DBAdapter.KEY_NAME,DBAdapter.KEY_STUDENTNUM};
int[]toViewIDs=新int[]
{R.id.item_name};
//创建适配器,将DB的列添加到UI中的elemesnt上。
SimpleCursorAdapter myCursorAdapter=
新SimpleCorsorAdapter(
这个,//上下文
R.layout.item\u布局,//行布局模板
游标,//游标(要映射的数据库记录集)
fromFieldNames,//DB列名
toViewIDs//查看要放入信息的ID
);
//设置列表视图的适配器
ListView myList=(ListView)findViewById(R.id.favlistView1);
设置适配器(myCursorAdapter);
}
私有无效注册表ListClickCallback(){
ListView myList=(ListView)findViewById(R.id.favlistView1);
//此代码用于从收藏夹列表的listview中删除单个项
myList.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
@凌驾
长单击(AdapterView arg0、视图arg1、,
整数arg2,最终长arg3){
Cursor=myDb.getRow(arg3);
if(cursor.moveToFirst()){
新建AlertDialog.Builder(FavoriteDialog.this)
.setTitle(“删除项”)
.setMessage(“是否要删除此疾病?”)
.setPositiveButton(android.R.string.yes,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int其中){
//继续删除
myDb.deleteItem(arg3);
populateListViewFromDB();
}
})
.setNegativeButton(android.R.string.no,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int其中){
//无所事事
}
})
.show();
}
返回true;
}
});

需要更多的上下文才能得到一个好的答案活动之间的关系如何?一个调用另一个

但通常情况下,如果活动a正在调用活动b,则在启动活动b时可以调用startActivityForResult。当b完成时,返回一个状态,通知活动a项目已被删除

若要更新菜单,请假设您正在通过重写OnCreateOptions菜单创建菜单,然后让该方法检查一个标志以设置菜单项的状态。然后在onActivityResult方法中,根据活动b的结果设置标志并调用InvalidateOptions菜单(),该方法将重新绘制选项菜单