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

Pass list of object from fragment to recyclerViewAdapter

我正在使用 SQLite 数据库做一个购物清单应用程序,我使用了 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) {

            }
        });
    }
}

您得到的是 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
}