Pass list of object from fragment to recyclerViewAdapter

Published

I’m doing a shopping list app using SQLitedatabase i used 2 dummy data for testing the database and list but it seems like the list is not correctly passed to the recyclerViewAdapter because the list.size() is 0 in adapter when I use System.out.println to check the if the list size is equal to 2. So below are my codes:

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) {

            }
        });
    }
}

Source: Android Questions

Published
Categorized as android, android-recyclerview, fragment, list

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq