Rajat Beck
Rajat Beck

Reputation: 1633

programatically add textview inside recyclerview onbindView adapter

On scrolling the RecyclerView the data inside the TextView changes even though the data in the array logtime.get(i).getTime() does not change.it changes only when we place the data inside textview which is generating dynamically.

public void onBindViewHolder(PunchCardViewHolder holder, int position) {

    PunchCardReport punchCardReport = punchCardReports.get(position);
    holder.sNumber.setText(punchCardReport.getmSNumber());
    holder.logDate.setText(punchCardReport.getmLogDate());
    StringTokenizer stringTokenizer = new StringTokenizer(punchCardReport.getmLogTime(), ",");
    List<LogTime> logtime = punchCardReport.getmLogTimeList();
    for (int i = 0; i < logtime.size(); i++) {
        Log.d(TAG, "onBindViewHolder: "+logtime.get(i).getTime());

    }
    LinearLayout.LayoutParams dim = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
     for (int i = 0; i < logtime.size(); i++) {

         TextView textView=new TextView(mContext);
         textView.setLayoutParams(dim);
         textView.setText(logtime.get(i).getTime());//the problem is here when we put data inside textview
         holder.logTime.addView(textView);
    }

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

Upvotes: 1

Views: 3209

Answers (4)

irfan
irfan

Reputation: 1

You just add holder.setIsRecyclable(false); inside method onBindViewHolder in your Adapter

Upvotes: 0

user2261498
user2261498

Reputation: 11

You can add following code in onBindViewHolder.

if(holder.logTime.getChildCount()>0){
    holder.logTime.removeAllViews();
}

for (int i = 0; i < logtime.size(); i++) {

    TextView textView=new TextView(mContext);
    textView.setLayoutParams(dim);
    textView.setText(logtime.get(i).getTime());
    //the problem is here when we put data inside textview
    holder.logTime.addView(textView);
}

Upvotes: 1

Abbas
Abbas

Reputation: 3331

Your design is completely wrong. You are creating new TextViews on every call to onBindViewHolder() which means when you scroll back to a view a fresh list of TextViews are created, so now you have two sets of TextViews one from last call and one from now (Keep scrolling and there will be more extra TextViews attached).

Creating new Views should be done in onCreateViewHolder(). The reason you are getting this behavior could be because RecyclerView uses Scrapped Views, which means that it reuses the Views created earlier. Thus enhancing performance (because no new memory allocations are required). If you are looking for a Nested RecyclerView then do that instead.

Upvotes: 2

Divyesh Patel
Divyesh Patel

Reputation: 2576

Try to Put this line in starting of onbindViewHolder.

holder.setIsRecyclable(false);

Or, you can put your TextView inside XML file. There is no need to create dynamic textview inside Onbinder.

Upvotes: 0

Related Questions