我需要发送一个值到第二个活动(与recyclerview)和充分它。例如,在main activity中有一个recyclerview,它可以工作。我想在第二个活动中修改这个动作,然后打开第二个活动,我想看到一个填充的回收视图。拜托。帮助我!
这是我的arraylist、方法和调用的第二个活动。
worldList = new ArrayList<>();
worldList.add("cat");//i want to display this word firstly
worldList.add("dog");//it after click
worldList.add("monkey");//after it
worldList.add("bird");//after it
worldList.add("fish");//etc
worldList.add("home");//etc
worldList.add("car");//etc
public void onClickYes(View view) {
display.setText(worldList.get(i));
progress.setText(counter + "");
wordItems.add(new RecyclerItems(worldList.get(i)));
builderAdapter();
if (i < worldList.size()) {
i++;
counter++;
}
else {
Toast.makeText(this, "Слова закончились", Toast.LENGTH_SHORT).show();
i = 0;
}
}
我的第二项活动
public class YesActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
//ArrayList<String> yesWordList;
///////////////////////////////////////////
final ArrayList<RecyclerItems> wordItems = new ArrayList<>();
///////////////////////////////////////////
//private WordDataBase wordDatabaseForYes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yes);
Bundle extras = getIntent().getExtras();
String word = null;
if(extras != null) {
word = extras.getString("key");
}
wordItems.add(new RecyclerItems(word));
Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
recyclerView = findViewById(R.id.rvYes);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerViewAdapter(wordItems);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
1条答案
按热度按时间k10s72fa1#
如果要在两个不同的激活中显示相同的recyclerview(保存数据列表),可以使用公共视图模型。编写代码以从视图模型中获取数据(在视图模型中编写一个getter方法),然后只需编写代码在每个活动中创建视图模型的示例并调用该方法。
这是一个有用的系列文章