英文:
How to edit ArrayList in MainActivity from second activity
问题
我在 MainActivity
中有一个 ArrayList
,我想在 ActivityB
中修改该列表。我该如何实现?
我目前的解决方案是将这个 ArrayList
做成一个静态变量,这样我就可以在 ActivityB
中使用 MainActivity.ArrayList
来访问它。然而,我看到这可能不是一个好的做法,也不符合良好的信息隐藏原则。这个列表是我唯一的静态变量。我能继续使用这种方法吗?如果不能,有什么其他更好的方法吗?
英文:
I have an ArrayList
in MainActivity
, and I want to modify that list in ActivityB
. How would I achieve this?
My current solution is that I made the ArrayList
a static variable so that I can access it in ActivityB
using MainActivity.ArrayList
. However, I have seen that this may not be good practice and is not good information hiding. This list is the only static variable I have. Could I keep using this method or would it be better if I did something else? If so, how else could I do this?
答案1
得分: 1
也许您可以尝试从 MainActivity
将这个数组发送到 SecondActivity
,然后在您的 SecondActivity
中,在您想要的方式下修改该数组,然后将其发送回到 MainActivity
?以下是示例代码,我从 MainActivity
发送一个名为 myValue
的 int
数组到 SecondActivity
,并在那里进行了修改。然后将其发送回到 MainActivity
。
MainActivity:
public class MainActivity extends AppCompatActivity {
private static final int LAUNCH_SECOND_ACTIVITY = 1;
public static final String MY_VALUE_KEY = "MY_VALUE_KEY";
private int myValue = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MyTag", "MAIN ACTIVITY. On create, myValue = " + myValue);
findViewById(R.id.buttonGoToSecond).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startSecondActivity();
}
});
}
private void startSecondActivity() {
Intent i = new Intent(this, SecondActivity.class);
i.putExtra(MY_VALUE_KEY, myValue);
startActivityForResult(i, LAUNCH_SECOND_ACTIVITY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LAUNCH_SECOND_ACTIVITY) {
if (resultCode == Activity.RESULT_OK) {
myValue = data.getIntExtra(MY_VALUE_KEY, -1);
Log.d("MyTag", "MAIN ACTIVITY. Result, myValue = " + myValue);
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.d("MyTag", "No result");
}
}
}
}
SecondActivity:
public class SecondActivity extends AppCompatActivity {
private int dataFromMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Get data from MainActivity
dataFromMain = getIntent().getIntExtra(MainActivity.MY_VALUE_KEY, -1);
Log.d("MyTag", "SECOND ACTIVITY. Data retrieved: " + dataFromMain);
// Do something with data
dataFromMain *= 2;
findViewById(R.id.buttonGoToMain).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
goToMain();
}
});
}
private void goToMain() {
Intent returnIntent = new Intent();
Log.d("MyTag", "SECOND ACTIVITY. Data send back: " + dataFromMain);
returnIntent.putExtra(MainActivity.MY_VALUE_KEY, dataFromMain);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}
日志如下所示:
D/MyTag: MAIN ACTIVITY. On create, myValue = 1
D/MyTag: SECOND ACTIVITY. Data retrieved: 1
D/MyTag: SECOND ACTIVITY. Data send back: 2
D/MyTag: MAIN ACTIVITY. Result, myValue = 2
英文:
Maybe You can try to send this array from MainActivity
to SecondActivty
and in Your SecondActivity
when You change that array in the way You want You can send it back and to MainActivity
? This is sample code, I am sending int
( named myValue
) from MainActivity
to SecondActivity
and change it there. And then send it back to MainActivity
MainActivity:
public class MainActivity extends AppCompatActivity
{
private static final int LAUNCH_SECOND_ACTIVITY = 1;
public static final String MY_VALUE_KEY = "MY_VALUE_KEY";
private int myValue = 1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MyTag", "MAIN ACTIVITY. On create, myValue = " + myValue);
findViewById(R.id.buttonGoToSecond).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
startSecondActivity();
}
});
}
private void startSecondActivity()
{
Intent i = new Intent(this, SecondActivity.class);
i.putExtra(MY_VALUE_KEY, myValue);
startActivityForResult(i, LAUNCH_SECOND_ACTIVITY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == LAUNCH_SECOND_ACTIVITY)
{
if (resultCode == Activity.RESULT_OK)
{
myValue = data.getIntExtra(MY_VALUE_KEY, -1);
Log.d("MyTag", "MAIN ACTIVITY. Result, myValue = " + myValue);
}
else if (resultCode == Activity.RESULT_CANCELED)
{
Log.d("MyTag", "No result");
}
}
}
}
SecondActivity:
public class SecondActivity extends AppCompatActivity
{
private int dataFromMain;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Get data from MainActivity
dataFromMain = getIntent().getIntExtra(MainActivity.MY_VALUE_KEY, -1);
Log.d("MyTag", "SECOND ACTIVITY. Data retrieved: " + dataFromMain);
//Do something with data
dataFromMain *= 2;
findViewById(R.id.buttonGoToMain).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
goToMain();
}
});
}
private void goToMain()
{
Intent returnIntent = new Intent();
Log.d("MyTag", "SECOND ACTIVITY. Data send back: " + dataFromMain);
returnIntent.putExtra(MainActivity.MY_VALUE_KEY, dataFromMain);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
}
And the logs look like this:
D/MyTag: MAIN ACTIVITY. On create, myValue = 1
D/MyTag: SECOND ACTIVITY. Data retrieved: 1
D/MyTag: SECOND ACTIVITY. Data send back: 2
D/MyTag: MAIN ACTIVITY. Result, myValue = 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论