我正在使用 java 开发一个 android 应用程序,但我遇到了以下问题。 我有一个主要活动,其中有一个“添加”按钮和一个列表视图。当我单击添加按钮时,它将打开另一个活动,我可以在其中向列表视图添加项目。添加此项后,当我单击第二个活动中的后退按钮时,我希望执行主活动中的 Refresh() 方法以将此项直接添加到主活动中的列表视图中。我找不到解决方法。我试图将此方法设为静态,但出现了很多错误,并且所有应用程序都停止了。此外,我尝试在第二个活动的 onBackPressed() 方法中创建主要活动的新实例,但该应用程序也已停止。任何人都可以帮我解决这个问题吗? 谢谢。
答案 0 :(得分:0)
阅读此内容:https://developer.android.com/training/basics/intents/result 然后在您收到指示第二个活动已完成的结果后,在您的 refresh
中添加对您的 MainActivity
方法的调用。
答案 1 :(得分:0)
我相信以下工作示例展示了如何完成您想要的:-
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");
}
}
注释应该解释代码
请注意,此方法确实使用了已弃用的 (startActivityForResult
),因此您可能希望考虑查看 Getting a result from an activity
结果
运行时应用程序显示:-
点击 NEXT 按钮将带您进入第二个活动 :-
点击 FINISH 按钮(活动添加一个新元素)返回到 MainActivity,现在是 :-
即新元素会相应地显示在 ListView 中