簡體   English   中英

如何從第二個活動中獲取 ArrayList 並將其顯示在主活動的 Recycler 視圖中

[英]How to get ArrayList from second Activity and show it in a Recycler view in the Main Activity

我有一個主要活動,它啟動了第二個活動,其中選擇了國家/地區。 在第二個活動中,當用戶選擇國家並按“添加”時,我想關閉第二個活動並在主活動的 RecyclerView 中顯示選定的國家。 我無法在主要活動中檢索選定國家/地區的數組列表。 任何的想法? 謝謝你。

[主要活動][1] [1]:https://i.stack.imgur.com/xMmL8.png

【第二個活動】【2】【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<>(); 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); }

有幾種方法可以實現您正在尋找的結果。

如果要繼續使用兩個活動,請將選擇保存在Room 數據庫中,並使用 LiveData 和 Query 觀察數據。

如果您想要更簡單的解決方案,請創建兩個片段

  1. 將所有第一個 Activity 的 View 相關代碼放在第一個片段中。
  2. 將第二個 Activity 的 View 相關代碼放在第二個片段中。

然后創建一個您的兩個片段都可以訪問的ViewModel 在您的optionsItemSelected()只需將optionsItemSelected()的變量設置為所選國家/地區的列表。

您還可以將您的國家/地區列表初始化代碼移動到 ViewModel 中,您可以從第二個片段的onViewCreated()回調中調用該代碼。

刪除第二個 Activity,因為它可以完全由 Fragment 替換。 您可以從第一個 Activity 內部訪問supportFragmentManager ,然后調用.beginTransaction() ,然后調用.replace().commit()以使用 Fragment。

另一個好處是您不必兩次初始化工具欄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM