如何从第二个活动在MainActivity中编辑ArrayList

huangapple go评论108阅读模式
英文:

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 发送一个名为 myValueint 数组到 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

huangapple
  • 本文由 发表于 2020年9月27日 16:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64086472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定