Rishabh Bhardwaj
Rishabh Bhardwaj

Reputation: 207

Populating a linearlayout within cardview using onbindviewholder

I have a linear layout within my cardview which in turn is within a recyclerview. Im trying to populate data into the linear layout through an arraylist dynamically- where the number of elements in every linear layout of the cardview will be of different size. When I do the following, it causes the onBindViewHolder to be called every time I scroll it out of view, adding the elements to the linear layout again and again. Here's the adapter of the recycler view. Kindly let me know how to add these elements in the right way, so that the linear layout doesnt overpopulate.

public class CardRecyclerAdapter extends RecyclerView.Adapter<CardRecyclerAdapter.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<ElementCard> cardElements;
private static MyClickListener myClickListener;
private Context context;

public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView title;
    LinearLayout listView;
    RelativeLayout relativeLayout;

    public DataObjectHolder(View itemView) {
        super(itemView);
        title = (TextView) itemView.findViewById(R.id.card_title);
        listView = (LinearLayout) itemView.findViewById(R.id.card_listview);
        relativeLayout = (RelativeLayout) itemView.findViewById(R.id.card_rellay);
        Log.i(LOG_TAG, "Adding Listener");
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        myClickListener.onItemClick(getAdapterPosition(), v);
    }
}

public void setOnItemClickListener(MyClickListener myClickListener) {
    this.myClickListener = myClickListener;
}

public CardRecyclerAdapter(ArrayList<ElementCard> myDataset, Context context) {
    cardElements = myDataset;
    this.context = context;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.element_card, parent, false);

    DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
    return dataObjectHolder;
}

@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
    holder.title.setText(cardElements.get(position).mTransactions.get(0).tCategory);
    for (Transaction transaction : cardElements.get(position).mTransactions) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.element_listitem, null, false);
        CheckBox checkBox = (CheckBox) view.findViewById(R.id.listitem_checkBox);
        checkBox.setText(transaction.tSentence); // set sentence for now -> change later
        checkBox.setChecked(transaction.tCompleted); // set checkbox completed or not

        holder.listView.addView(view);
    }
}

public void addItem(ElementCard dataObj, int index) {
    cardElements.add(index, dataObj);
    notifyItemInserted(index);
}

public void deleteItem(int index) {
    cardElements.remove(index);
    notifyItemRemoved(index);
}

@Override
public int getItemCount() {
    return cardElements.size();
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

public interface MyClickListener {
    public void onItemClick(int position, View v);
}
}

Upvotes: 3

Views: 649

Answers (1)

fractalwrench
fractalwrench

Reputation: 4076

The view will be recycled when scrolling, so the LinearLayout will still contain the child views added in the previous binding. You can clear all views by using the following code on your LinearLayout (just replace the View id with what you are using within your project):

LinearLayout myLinearLayout = (LinearLayout) cardView.findViewById(R.id.my_linear_layout);
myLinearLayout.removeAllViews();

Upvotes: 2

Related Questions