将对象列表从片段传递到 recyclerView 适配器

Posted

技术标签:

【中文标题】将对象列表从片段传递到 recyclerView 适配器【英文标题】:Pass list of object from fragment to recyclerViewAdapter 【发布时间】:2022-01-01 14:45:36 【问题描述】:

我正在使用 SQLitedatabase 做一个购物清单应用程序,我使用 2 个虚拟数据来测试数据库和列表,但似乎列表没有正确传递给 recyclerViewAdapter,因为当我在适配器中 list.size() 为 0使用 System.out.println 检查列表大小是否等于 2。下面是我的代码:

FragmentHome.java:

public class FragmentHome extends Fragment 

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;

private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;

public static DatabaseHandler databaseHandler;
public static List<ShoppingItem> shoppingItemList=new ArrayList<ShoppingItem>();

public FragmentHome() 
    // Required empty public constructor


public static FragmentHome newInstance(String param1, String param2) 
    FragmentHome fragment = new FragmentHome();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;


@Override
public void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    if (getArguments() != null) 
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) 
    // Inflate the layout for this fragment
    View v=inflater.inflate(R.layout.fragment_home, container, false);

    databaseHandler=new DatabaseHandler(getContext(),null,null,1);
    populateDB();
    populateitemList();

    recyclerView=v.findViewById(R.id.recyclerView);
    recyclerViewAdapter=new RecyclerViewAdapter(shoppingItemList,getActivity());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(recyclerViewAdapter);

    DividerItemDecoration divider=new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
    recyclerView.addItemDecoration(divider);

    return v;


private void populateDB() 
    //testing
    databaseHandler.addItem(new ShoppingItem(1,"bread","testing1","1","Small",1));
    databaseHandler.addItem(new ShoppingItem(2,"rice","testing2","2","Medium",0));


private void populateitemList() 
    if (shoppingItemList!=null)
        shoppingItemList.clear();
    
    shoppingItemList=databaseHandler.getAllItem();
    for(int i=0;i<shoppingItemList.size();i++)
        System.out.println(shoppingItemList.get(i).getName());
    

RecyclerViewAdapter.java:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> 

List<ShoppingItem> shoppingItemList;
private Context context;

public RecyclerViewAdapter(List<ShoppingItem> shoppingItemList, Context context) 
    this.shoppingItemList = shoppingItemList;
    this.context = context;


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
    View v=LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout,parent,false);
    return new ViewHolder(v);


@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) 

    ShoppingItem shoppingItem=shoppingItemList.get(position);
    System.out.println("shoppingList in adapter: " + shoppingItemList);

    if(shoppingItem.getUrgent()==1) 
        holder.imageView.setImageResource(R.drawable.urgent);
    
    if(shoppingItem.getUrgent()==0) 
        holder.imageView.setImageResource(R.drawable.buy);
    

    holder.textViewItem.setText(shoppingItem.getName());
    holder.textViewItemQty.setText(shoppingItem.getQuantity());
    holder.textViewItemSize.setText(shoppingItem.getSize());


@Override
public int getItemCount() 
    System.out.println(shoppingItemList.size());
    return shoppingItemList.size();


public class ViewHolder extends RecyclerView.ViewHolder 

    private ImageView imageView;
    private TextView textViewItem,textViewItemQty,textViewItemSize;

    public ViewHolder(@NonNull View itemView) 
        super(itemView);

        imageView=itemView.findViewById(R.id.imageView);
        textViewItem=itemView.findViewById(R.id.textViewItem);
        textViewItemQty=itemView.findViewById(R.id.textViewItemQty);
        textViewItemSize=itemView.findViewById(R.id.textViewItemSize);

        itemView.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

            
        );
    

【问题讨论】:

我检查了,你的代码没问题,你是在 FragmentHome 中获取数据吗? 【参考方案1】:

你得到 0 因为 getItemCount() 在 populateitemList() 之前被调用 以后可以多次调用,也由recycler view自动决定

添加数据后需要调用 notifyDatasetChanged() 来告诉回收站视图自己更新

private void populateitemList() 
    if (shoppingItemList!=null)
        shoppingItemList.clear();
    
    shoppingItemList=databaseHandler.getAllItem();
    for(int i=0;i<shoppingItemList.size();i++)
        System.out.println(shoppingItemList.get(i).getName());
    
    recyclerViewAdapter.notifyDatasetChanged(); //ADD THIS LINE

【讨论】:

以上是关于将对象列表从片段传递到 recyclerView 适配器的主要内容,如果未能解决你的问题,请参考以下文章

如何从 recyclerview 片段传递到另一个 recyclerview 片段

如何将数据从 RecyclerView 传递到 Kotlin 中的 DialogFragment?

将数据从片段列表视图项传递到活动字符串变量 onitemclicklistener

如何在单击 RecyclerView 项目时从一个片段移动到另一个片段

如何从另一个片段访问片段对象

将 JSON 字符串从片段传递到适配器的问题