[英]Android - How to send an ArrayList without using Intent?
我有一個Activity
和一個Adapter Class
。 在我的Adapter
class 中,我有一個ArrayList
,我想在Activity
class 中使用它。 並且Activity
class 的回收器Recycler view
附加到adapter
class 所以,我不能使用intent
發送它。 那么,還有其他方法可以訪問ArrayList
嗎?
在我看來,你有三種方法可以做到:
您可以按如下方式創建適配器 -
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
}
}
現在你可以從你的適配器獲取列表如下-
recycleViewAdapter.getStringList();
快樂編碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.