1

I am trying to add elements dynamically in the listview on button click with the help of custom array adapter. Whenever I add new element it changes the entire elements to default data. I have a seekbar in the listview if I modify its value its getting reflected to dataset but when I add new element again in UI it get reset to default values.

public class TextAdapter extends ArrayAdapter<TextDetails> {

private List<TextDetails> dataSet;
Context mContext;
private int lastPosition = -1;

public TextAdapter(List<TextDetails> td, Context applicationContext) {
    super(applicationContext,R.layout.list_edit, td);
    this.dataSet=td;
    this.mContext=applicationContext;
}

private static class ViewHolder {
    TextView txtName;
    CrystalRangeSeekbar sk;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    final TextDetails data = getItem(position);

    // Check if an existing view is being reused, otherwise inflate the view
    final ViewHolder viewHolder; // view lookup cache stored in tag

    final View result;

    if (convertView == null) {

        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.list_edit, parent, false);
        viewHolder.txtName = (TextView) convertView.findViewById(R.id.tv);
        viewHolder.sk = (CrystalRangeSeekbar) convertView.findViewById(R.id.skr);
        result=convertView;
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result=convertView;
    }

    lastPosition = position;

    viewHolder.sk.setOnRangeSeekbarChangeListener(new OnRangeSeekbarChangeListener() {
        @Override
        public void valueChanged(Number minValue, Number maxValue) {



        }
    });
    final String[] n = new String[1];
    final long[] mn = new long[1];
    final long[] mx = new long[1];
    viewHolder.sk.setOnRangeSeekbarFinalValueListener(new     OnRangeSeekbarFinalValueListener() {
        @Override
        public void finalValue(Number minValue, Number maxValue) {
             n[0] = (String) viewHolder.txtName.getText();
              mn[0] = (long) viewHolder.sk.getSelectedMinValue()/1000;
             mx[0] = (long) viewHolder.sk.getSelectedMaxValue()/1000;

            data.setMaxvalue(mx[0]);
            data.setMinvalue(mn[0]);
            data.setNam(n[0]);

            dataSet.set(position,data);


        }
    });

    viewHolder.txtName.setText(dataSet.get(position).getName());
    viewHolder.sk.setMinValue(dataSet.get(position).getMin());
    viewHolder.sk.setMaxValue(dataSet.get(position).getMax());
    // Return the completed view to render on screen
        return convertView;
    }
}

Main activity:

addnew.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            td.add(new TextDetails("Sample",0,duration));
            adapter=new TextAdapter(td,getApplicationContext());
                    list.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

            addBubble();

        }
    });

Datamodel:

public class TextDetails implements Serializable {

String name;
long minvalue;
long maxvalue;

public TextDetails(String name, long minvalue, long maxvalue ) {
    this.name=name;
    this.minvalue=minvalue;
    this.maxvalue=maxvalue;


}

public String getName() {
    return name;
}

public long getMin() {
    return minvalue;
}

public long getMax() {
    return maxvalue;
}

public void setNam(String name) {
    this.name=name;
}

public void setMinvalue(long minvalue) {

    this.minvalue=minvalue;
}

public void setMaxvalue(long maxvalue) {
    this.maxvalue=maxvalue;
}



}
  • follow this example: http://stackoverflow.com/questions/41026692/updating-listview-without-duplication/41027195?noredirect=1#comment69298147_41027195 – CodeDaily Dec 09 '16 at 01:52

1 Answers1

0

You shouldn't set a new adapter on every click. You should add data to the existing adapter, then call notifyDataSetChanged() on it. Only change the adapter if you're changing everything about the list and want to reset it to the top, for example if you're using the same list to display two different data sets.

Steve Liddle
  • 3,685
  • 3
  • 27
  • 39
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks a lot @Gabe Sechan! That worked for me but when I change the value of the seekbar that's not updated in the dataset! Can you please check where I am doing wrong in custom adapter? – Suresh Kumar S Dec 09 '16 at 02:29