【问题标题】:Calling the Refresh() method which is in the main activity from another activity [duplicate]从另一个活动调用主活动中的 Refresh() 方法[重复]
【发布时间】:2021-07-14 08:44:10
【问题描述】:

我正在使用 java 开发一个 android 应用程序,但我遇到了以下问题。 我的主要活动有一个“添加”按钮和一个列表视图。当我单击添加按钮时,它将打开另一个活动,我可以在其中将项目添加到列表视图。添加此项后,当我单击第二个活动的后退按钮时,我希望执行主活动中的 Refresh() 方法以将此项直接添加到主活动中的列表视图中。我找不到解决方法。我试图将此方法设为静态但出现很多错误,并且所有应用程序都停止了。我还尝试在第二个活动的 onBackPressed() 方法中创建主要活动的新实例,但应用程序也已停止。任何人都可以帮我解决这个问题吗? 谢谢。

【问题讨论】:

  • 尝试在主Activity的onCreate方法末尾添加refresh(),能否提供一些代码来看看

标签: java android


【解决方案1】:

阅读此内容:https://developer.android.com/training/basics/intents/result 然后在您获得表明第二个活动已完成的结果后,在您的MainActivity 中添加对您的refresh 方法的调用。

【讨论】:

    【解决方案2】:

    我相信以下工作示例显示了您如何完成您想要的:-

    MainActivity(初始活动):-

    public class MainActivity extends AppCompatActivity {
    
        public static final int ACTIVITY1_REQUEST_CODE = 999;
        public static final String EXTRA_MYARRAY = "extra_myarray";
    
        private Button next;
        private ListView listView;
        ArrayAdapter<String> adapter;
        ArrayList<String> myarray = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            next = this.findViewById(R.id.next);
            listView = this.findViewById(R.id.listview);
    
            // Prepare the Button's onClickListener to start the other activity
            next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(view.getContext(),Activity2.class);
                    // prepare to pass the data to the other activity
                    i.putExtra(EXTRA_MYARRAY,myarray);
                    // Start the other activity
                    startActivityForResult(i,ACTIVITY1_REQUEST_CODE);
                }
            });
    
            // Prepare the data
            myarray.add("a");
            myarray.add("b");
            myarray.add("c");
            // Output data to the log (to show what happens)
            refresh(myarray,"INITIAL", false);
        }
    
        // Prepare to receive and handle the modified data when returning from other activity
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == ACTIVITY1_REQUEST_CODE && resultCode == RESULT_OK) {
                myarray.clear();
                for(String s: data.getStringArrayListExtra(EXTRA_MYARRAY)) {
                    myarray.add(s);
                }
                refresh(data.getStringArrayListExtra(EXTRA_MYARRAY),"RESUMED",true);
            }
        }
    
        /**
         * Refresh
         * @param modifiedData  The modified data to be applied (see modify) as an ArrayList<String>
         * @param tagExtra      String used to indicate where the refresh was called from
         * @param modify        flag to indicate whether or not to rebuild the data
         *                      if coming from the this activity then clear and add would
         *                      empty the array and add nothing
         */
        private void refresh(ArrayList<String> modifiedData, String tagExtra, boolean modify) {
            if (modify) myarray.clear();
            for(String s: modifiedData) {
                if (modify) myarray.add(s);
                Log.d("MA_" + tagExtra,"Value is " + s);
            }
            refreshListView();
        }
    
        private void refreshListView() {
            if (adapter == null) {
                adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,myarray);
                listView.setAdapter(adapter);
            } else {
                adapter.notifyDataSetChanged();
            }
        }
    }
    

    Activity2 调用的/第二个活动(修改列表并在单击按钮时将修改后的列表返回给父级):-

    public class Activity2 extends AppCompatActivity {
    
    
        private Button finish;
        private ArrayList<String> myarray = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_2);
            finish = this.findViewById(R.id.finish);
    
            // Prepare the Button's onCLickListener
            finish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent();
                    // Prepare to return the data
                    i.putExtra(MainActivity.EXTRA_MYARRAY,myarray);
                    // Indicate that all is OK
                    setResult(RESULT_OK,i);
                    // Finish this activity and thus pass control back to the parent activity
                    finish();
                }
            });
    
            // Modify the data
            myarray = this.getIntent().getStringArrayListExtra(MainActivity.EXTRA_MYARRAY);
            myarray.add("d");
    
        }
    }
    

    结果

    应用程序运行时显示:-

    单击“下一步”按钮将带您进入第二个活动:-

    单击 FINISH 按钮(活动添加一个新元素)返回到现在的 MainActivity:-

    即新元素会相应地显示在 ListView 中

    【讨论】:

      猜你喜欢
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多