英文:
How to get ArrayList from second Activity and show it in a Recycler view in the Main Activity
问题
我有一个主活动,启动一个第二个活动,用户在第二个活动中选择国家。在第二个活动中,当用户选择国家并按下“添加”按钮时,我想关闭第二个活动,并在主活动中的RecyclerView中显示所选国家。我无法在主活动中检索到所选国家的ArrayList。有什么想法吗?谢谢。
主活动
1: https://i.stack.imgur.com/xMmL8.png
第二个活动
2: https://i.stack.imgur.com/lVMYS.jpg
主活动代码:
public class MainActivity extends AppCompatActivity {
private Button addCountry;
private RecyclerView countrySelectedRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbarId);
toolbar.setTitle("Main Activity");
setSupportActionBar(toolbar);
countrySelectedRecyclerView = findViewById(R.id.idCountrySelectedRecyclerView);
addCountry = findViewById(R.id.addCountryId);
selectedCountryAdapter adapter = new selectedCountryAdapter();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
countrySelectedRecyclerView.setLayoutManager(layoutManager);
countrySelectedRecyclerView.setHasFixedSize(true);
countrySelectedRecyclerView.setAdapter(adapter);
addCountry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ChooseCountry.class);
startActivity(intent);
}
});
}
}
第二个活动代码:
public class ChooseCountry extends AppCompatActivity implements CountryListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_country);
Toolbar toolbar = findViewById(R.id.toolbarId);
toolbar.setTitle("Second Activity");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
RecyclerView countryRecyclerView = findViewById(R.id.recyclerViewCoutryId);
List<Countries> countriesList = new ArrayList<>();
// 添加国家到列表
// ...
final selectionCountryAdapter selectCountryAdapter = new selectionCountryAdapter(countriesList, this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
countryRecyclerView.setLayoutManager(layoutManager);
countryRecyclerView.setHasFixedSize(true);
countryRecyclerView.setAdapter(selectCountryAdapter);
// 获取所选国家的ArrayList
List<Countries> newSelectedCountries = selectCountryAdapter.getSelectedCountries();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_add, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.idMenuAdd:
// 执行所需的代码
finish();
break;
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}
请注意,要在第二个活动中获得所选国家的ArrayList,您可以使用selectCountryAdapter.getSelectedCountries()
方法,该方法应该返回已选择的国家列表。
英文:
I have a Main Activity that start a Second Activity where countries are selected. In the Second Activity, when the user selects the countries and presses “Add”, I want to close the Second Activity and show the selected countries in a RecyclerView in the Main Activity. I’m not able to retrieve arraylist with selected countries in Main Activity. Any idea? Thank you.
Main Activity
1: https://i.stack.imgur.com/xMmL8.png
Second Activity
2: https://i.stack.imgur.com/lVMYS.jpg
Main Activity Code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
public class MainActivity extends AppCompatActivity {
private Button addCountry;
private RecyclerView countrySelectedRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbarId);
toolbar.setTitle("Main Activity");
setSupportActionBar(toolbar);
countrySelectedRecyclerView = findViewById(R.id.idCountrySelectedRecyclerView);
addCountry = findViewById(R.id.addCountryId);
selectedCountryAdapter adapter = new selectedCountryAdapter();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
countrySelectedRecyclerView.setLayoutManager(layoutManager);
countrySelectedRecyclerView.setHasFixedSize(true);
countrySelectedRecyclerView.setAdapter(adapter);
addCountry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ChooseCountry.class);
startActivity(intent);
}
});
}
}
<!-- end snippet -->
Second Activity Code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
public class ChooseCountry extends AppCompatActivity implements CountryListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_country);
Toolbar toolbar = findViewById(R.id.toolbarId);
toolbar.setTitle("Second Activity");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
RecyclerView countryRecyclerView = findViewById(R.id.recyclerViewCoutryId);
List<Countries> countriesList = new ArrayList<>();
Countries unitedStates = new Countries();
unitedStates.image = R.drawable.united_states;
unitedStates.unselected = R.drawable.icon_unselected;
unitedStates.country = "United States";
countriesList.add(unitedStates);
Countries italy = new Countries();
italy.image = R.drawable.italy;
italy.unselected = R.drawable.icon_unselected;
italy.country = "Italy";
countriesList.add(italy);
Countries canada = new Countries();
canada.image = R.drawable.canada;
canada.unselected = R.drawable.icon_unselected;
canada.country = "Canada";
countriesList.add(canada);
Countries spain = new Countries();
spain.image = R.drawable.spain;
spain.unselected = R.drawable.icon_unselected;
spain.country = "Spain";
countriesList.add(spain);
final selectionCountryAdapter selectCountryAdapter = new selectionCountryAdapter(countriesList, this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
countryRecyclerView.setLayoutManager(layoutManager);
countryRecyclerView.setHasFixedSize(true);
countryRecyclerView.setAdapter(selectCountryAdapter);
//Arraylist with the selected countries
List<Countries> newSelectedCountries = selectCountryAdapter.getSelectedCountries();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_add, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.idMenuAdd:
//Required code
finish();
break;
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
<!-- end snippet -->
答案1
得分: 0
以下是翻译好的内容:
有几种方法可以实现您所寻求的结果。
如果您想继续使用两个活动(Activity),则将选择保存在 Room 数据库 中,并使用 LiveData 和查询来观察数据。
如果您想要一个更简单的解决方案,创建两个 片段(Fragment):
- 将您第一个活动(Activity)中与视图相关的所有代码放入第一个片段中。
- 将您第二个活动(Activity)中与视图相关的代码放入第二个片段中。
然后创建一个 ViewModel,您的两个片段将都可以访问它。在您的 optionsItemSelected()
中,只需将 ViewModel 中的一个变量设置为所选国家的列表。
您还可以将国家列表的初始化代码移动到 ViewModel 中,然后可以从第二个片段的 onViewCreated()
回调中调用它。
移除第二个活动(Activity),因为它完全可以被一个片段替代。您可以从第一个活动(Activity)内部访问 supportFragmentManager
,然后调用 .beginTransaction()
,接着调用 .replace()
和 .commit()
来使用片段。
另一个好处是您不需要两次初始化您的工具栏(Toolbar)。
英文:
There are a few ways to achieve the result you are looking for.
If you want to continue using two activities, then save the selection in a Room database, and observe the Data using LiveData and a Query.
If you would like an easier solution, create two fragments:
- Put all your first Activity's View related code in the first fragment.
- Put your second Activity's View related code in the second fragment.
Then create a ViewModel which both of your fragments will have access to. In your optionsItemSelected()
just set a variable in the viewModel to a list of selected countries.
You can also move your country list initialization code into the ViewModel, which you can call from the second fragment's onViewCreated()
callback.
Remove the second Activity because it can be replaced entirely by a Fragment. You can access supportFragmentManager
from inside the first Activity, and then call .beginTransaction()
followed by .replace()
and .commit()
to use Fragments.
Another benefit is that you don't have to initialize your Toolbar twice.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论