英文:
Android - How to send an ArrayList without using Intent?
问题
我有一个 Activity
和一个 Adapter
类。在我的 Adapter
类中,我有一个 ArrayList
,我想在 Activity
类中使用它。并且 Activity
类的 Recycler view
附加到 Adapter
类,所以我不能使用 intent
来发送它。所以,有没有其他方法可以访问这个 ArrayList
?
英文:
I have an Activity
and an Adapter Class
. In my Adapter
class, I have an ArrayList
that I want to use in the Activity
class. And Recycler view
of Activity
class is attached to the adapter
class So, I cannot use intent
to send it. So, Is there any other way I can access the ArrayList
?
答案1
得分: 1
- 就我的观点而言,你有三种方法可以做到这一点:
- 将你的 ArrayList 移动到 ViewModel,然后将它传递给你的适配器或在活动中使用它。
- 将 ArrayList 放在活动中,然后将它传递给适配器。
- 在你的适配器中创建一个 getter 方法,并在活动中使用它。
英文:
In my opinion, you have three ways to do it:
- move your ArrayList to ViewModel, then pass it to your adapter or use it in the activity.
- place ArrayList just in the activity and pass it to the adapter.
- create a getter in your adapter and use it in the activity.
答案2
得分: 1
你可以按照以下方式创建适配器 -
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.CustomViewModel> {
private Context context;
private List<String> stringList;
public RecycleViewAdapter(Context context, List<String> stringList) {
this.context = context;
this.stringList = stringList;
}
@NonNull
@Override
public CustomViewModel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new CustomViewModel(LayoutInflater.from(context).inflate(R.layout.recycle_view_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull CustomViewModel holder, int position) {
//放置你的绑定代码
}
@Override
public int getItemCount() {
if (stringList!= null)
return stringList.size();
else
return 0;
}
//适配器的getter方法返回字符串列表
public List<String> getStringList() {
return stringList;
}
public class CustomViewModel extends RecyclerView.ViewHolder {
//这里放你的内部类代码
}
}
现在你可以像下面这样从你的适配器中获取 List
recycleViewAdapter.getStringList();
愉快编码!
英文:
You can create your adapter as follow -
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.CustomViewModel> {
private Context context;
private List<String> stringList;
public RecycleViewAdapter(Context context, List<String> stringList) {
this.context = context;
this.stringList = stringList;
}
@NonNull
@Override
public CustomViewModel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new CustomViewModel(LayoutInflater.from(context).inflate(R.layout.recycle_view_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull CustomViewModel holder, int position) {
//put your code of binding of code
}
@Override
public int getItemCount() {
if (stringList!= null)
return stringList.size();
else
return 0;
}
//getter method of Adpater to return string
public List<String> getStringList() {
return stringList;
}
public class CustomViewModel extends RecyclerView.ViewHolder {
//your inner class code here
}
}
Now you can get List<String> from your adapter as follows-
recycleViewAdapter.getStringList();
Happy coding !
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论