I am displaying 17 CardViews.
I am using RecyclerView to achieve the same.
Each CardView shows a common data (format).
Depending on data received from a JSON file, a card may display some additional rows. . The following image shows 2 CardViews (in focus) displaying the additional data, the rest displaying the common data.
It appears correctly. But then when I scroll down to bottom of the View, the last CardView is not suppose to display any row, but it does copy one from one of the 2 we saw in the above link and displays. [Can post only 2 links a time]
Then I scroll to the top and it copies the rows from both CardViews that were displaying correctly and repeats here for top 2 cards respectively:
What I tried?
- If, else
Many similar issues show a common solution. One of those answers is here. Thing is I don't know what condition to apply and where. This is my code:
@Override
public void onBindViewHolder(DataObjectHolder holder, final int position) {
HashMap<Integer, ShoppingCartSummaryObject> dataItem = new HashMap<Integer, ShoppingCartSummaryObject>();
dataItem = mDataset.get(position);
TextView textViewComment = holder.textViewComment;
ShoppingCartSummaryObject obj = new ShoppingCartSummaryObject();
obj = (ShoppingCartSummaryObject) dataItem.get(position);
holder.textViewPackageType.setText(obj.getPackageType());
holder.textViewPackageCode.setText(obj.getPackageCode());
holder.textViewPackageDesc.setText(obj.getPackageDesc());
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
try {
for (int i = 0; i < obj.getArrayListPart().size(); i++) {
View view2 = LayoutInflater.from(mContext).inflate(R.layout.detailrow, null);
holder.advPartDesc = (TextView) view2.findViewById(R.id.advPartDesc);
holder.advPartnum = (TextView) view2.findViewById(R.id.advPartNum);
cv = holder.cv;
advisoryLayout = holder.advisoryLayout;
empty = holder.empty;
layoutPackageSummary = holder.layoutPackageSummary;
holder.detailRow = (RelativeLayout) view2.findViewById(R.id.detailRow);
rallyLayout = new RelativeLayout(mContext);
rallyLayout.addView(holder.detailRow);
cnt+=1;
rallyLayout.setId(cnt);
holder.layoutpartList.addView(rallyLayout);
if (i == 0) { //for adding the first row below standard data
params.addRule(RelativeLayout.BELOW, empty.getId() );
holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);
rallyLayout.setLayoutParams(params);
} else { //for more than one rows, 2nd row appearing below 1st and 3rd below 2nd and so on
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, cnt - 1);
holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);
rallyLayout.setLayoutParams(params);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
and this is how I tried implementing the answers from the shared link:
if(obj.getArrayListPart().size()>0) { /*added the if() condition*/
if (i == 0) {
params.addRule(RelativeLayout.BELOW, empty.getId());
holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);
rallyLayout.setLayoutParams(params);
} else {
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, cnt - 1);
holder.advPartnum.setText(obj.getArrayListPart().get(i).partCode);
holder.advPartDesc.setText(obj.getArrayListPart().get(i).partDesc);
rallyLayout.setLayoutParams(params);
}
}
else
{
holder.advPartnum.setText("abc");
holder.advPartDesc.setText("abc");
}
}
} catch (Exception e) {
e.printStackTrace();
}
I also added:
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
Nothing worked.
I checked the JSON data, removed one array from the list, and irrespective of the card I remove, it also affects removal of the repeating row values from top/bottom rows (cardviews), depending on the card lying between card with extra details and card on top/bottom. Also tried:
mAdapter.setHasStableIds(true);
but that simply skipped all the additional row data that appears below the common standard.
Kindly help. I have provided whatever I tried.
Here is the source code which you asked for xml
Activity