我试图在片段内的recycleView上显示一些项目。这些项目来自另一个片段。我首先用一个空的arrayList创建recycleView,但是当按下一个按钮时,数据通过接口发送,一个方法填充ArrayList。我整天调试了这个并且工作正常,当我点击按钮记录时,ArrayList的大小已经达到了一个。
我正在阅读我必须在列表更改时通知我的适配器,所以我尝试使用notifyDataSetChange(),notifyItemInserted(dataReceived.size() - 1)但似乎都没有解决。每当我检查adapter.getItemCount()它就会抛出0,即使arraylist说抛出1。
这里是片段里面的代码接收数据:
public class FavoriteFragment extends Fragment {
private OnFragmentInteractionListener mListener;
RecyclerView myRecyclerView;
public ArrayList<RecycleViewItem> dataReceived;
public MyFavoriteAdapter adapter;
public FavoriteFragment() {
}
public void setDataReceived(ArrayList<RecycleViewItem> dataReceived){
this.dataReceived = dataReceived;
Log.d("INTERFACE", "Data received by the FavoriteFragment!!!!" + " " + this.dataReceived.get(0).getCardName());
for(int i = 0; i < dataReceived.size(); i++) {
//THIS LINE RETURNS A 1
Log.d("INTERFACE", "Items inside the dataReceived: " + dataReceived.size());
//NEITHER WORKED
//adapter.notifyItemInserted(dataReceived.size() -1);
//adapter.notifyDataSetChanged();
//THIS LINE WILL KEEP ON RETURNING 0
Log.d("INTERFACE", "Items inside the list inside the adapter: " + adapter.getItemCount());
}
}
public static FavoriteFragment newInstance() {
FavoriteFragment fragment = new FavoriteFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
dataReceived = new ArrayList<>();
adapter = new MyFavoriteAdapter(dataReceived);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_favorite, container, false);
myRecyclerView = (RecyclerView) view.findViewById(R.id.recycleView);
myRecyclerView.setHasFixedSize(true);
LinearLayoutManager myLayoutManager = new LinearLayoutManager(getActivity());
myLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
if (myRecyclerView != null) {
myRecyclerView.setAdapter(adapter);
}
myRecyclerView.setLayoutManager(myLayoutManager);
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
这是适配器:
public class MyFavoriteAdapter extends RecyclerView.Adapter<MyViewHolder>{
private ArrayList<RecycleViewItem> list = new ArrayList<RecycleViewItem>();
InterfaceListItemClickListener sender = null;
public MyFavoriteAdapter(ArrayList<RecycleViewItem> list) {
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item_favorite, parent, false);
MyViewHolder holder = new MyViewHolder(view, sender);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if(list.size() >= 1) {
holder.favoriteImageView.setImageResource(list.get(position).getImageResourceID());
holder.favoriteTextView.setText(list.get(position).getCardName());
}
}
@Override
public int getItemCount() {
return list.size();
}
}
答案 0 :(得分:2)
您的适配器有一个私有变量list
,必须修改它才能使adapter.notifyDataSetChanged()
生效。
这是你的方法:
public void setDataReceived(ArrayList<RecycleViewItem> dataReceived){
this.dataReceived = dataReceived;
...
}
将参数dataReceived
分配给您的片段this.dataReceived
并不会以任何方式影响您的适配器list
。相反,你可以做这样的事情:
public void setDataReceived(ArrayList<RecycleViewItem> dataReceived){
adapter.list.addAll(dataReceived);
...
}
您可能必须公开您的适配器list
变量,或通过getter公开它:
public ArrayList<RecycleViewItem> getList() {
return list
}
甚至可以添加一个可以向其中添加项目的方法:
public void addAll(ArrayList<RecycleViewItem> items) {
list.addAll(items);
}
答案 1 :(得分:0)
public void setDataReceived(ArrayList dataReceived){ this.dataReceived.addAll(dataReceived); .... } 不要重新创建列表