0

I am trying to add dynamic textview to listview items.textviews can be of 1-2 or more than that depending on data I have succeed in adding the textview but problem is textviews are repeated on scroll.

I am creating new object of textview each time in loop.I am aware of the problem that android tries to reuse existing view but i have to add new view every time.

Here is my code in custom adapter:

public class ViewHolder {
    TextView text1;
    LinearLayout linearLayout;
    TextView t;
    TextView t1;
}

getView method

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.two_item_icon_text, null);
        holder = new ViewHolder();
        holder.text1 = (TextView) convertView.findViewById(R.id.text1);

        convertView.setTag(holder);
    } else {

        holder = (ViewHolder) convertView.getTag();

    }

    holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.lin_lay_dynamic);
    holder.text1.setText("" + DATA1[position]);
    String tmp, dateparsed;
    dateparsed = DATA1[position].substring(0, DATA1[position].indexOf(":"));
    for (int x = 0; x < calendareventholder1.size(); x++) {

        objHolder = (CalendarEventHolder) calendareventholder1.get(x);

        if (objHolder.opendate.equals(displaydate[current])) {

            tmp = objHolder.dtstarttime.toString().substring(0, objHolder.dtstarttime.toString().indexOf(":"));

            if (Integer.parseInt(tmp) >= Integer.parseInt(dateparsed) && Integer.parseInt(tmp) < Integer.parseInt(dateparsed) + 1) {
                holder.t = new TextView(convertView.getContext());
                holder.t.setText(":-d ");
                holder.t.setOnClickListener(this);

                if (Common.isChildSessionAlerted(String.valueOf(objHolder.id), getApplicationContext(), object1)) {

                    holder.t.setText(holder.t.getText() + objHolder.dtstarttime + " " + objHolder.dtendtime + " :-a");
                } else {
                    holder.t.setText(holder.t.getText() + objHolder.dtstarttime + " " + objHolder.dtendtime);
                }
                holder.t.setTag(objHolder.id);
                holder.t.setTextSize(Common.getPreferenceInt(getApplicationContext(), Common.PREF_FONT_SIZE, 10));
                holder.t.setTextColor(Color.BLACK);
                holder.t.setText(getSmiledText(ScheduleActivity.this,
                holder.t.getText().toString()));
                holder.linearLayout.addView(holder.t);
                holder.t1 = new TextView(convertView.getContext());
                holder.t1.setOnClickListener(this);
                holder.t1.setText(objHolder.title);
                holder.t1.setTag(objHolder.id);

                holder.t1.setTextSize(Common.getPreferenceInt(getApplicationContext(), Common.PREF_FONT_SIZE, 10));
                holder.t1.setTextColor(Color.BLACK);
                holder.linearLayout.addView(holder.t1);
            }
        }
    }
    return convertView;
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Tony Chhabada
  • 412
  • 1
  • 7
  • 18

1 Answers1

1

Its a little hard to really understand what is in holder since it isn't super descriptive, but if I am reading your question right, the reason you are getting repeated items is because you are just adding Views and never deleting what was there before. ListView recycles views, meaning what was there previously stays there. As it scrolls it actually keeps reusing views rather than creating new ones. I won't go into detail because there are a bunch of question on here with similar problems. But at the top you should do

getView() {
    /*previous stuff, and holder.linearLayout must have been set!*/
    if(holder.linearLayout.getChildCount() > 0)
        holder.linearLayout.removeAllViews();

This way any views already there will be removed. One last thing, you should also check out this. Immensely helpful in understanding why you have to do what I posted.

Andy
  • 10,553
  • 21
  • 75
  • 125
  • No problem. One quick thing, you should probably do a check to know whether to call `removeAllViews` or not. No point in calling it when there are no children in the `LinearLayout`. I'll update that. – Andy Dec 21 '12 at 06:51
  • Thanks in millions andy, You saved my day. – Tony Chhabada Dec 21 '12 at 06:55