在UI中使用自定义适配器未正确更新列表视图

时间:2016-12-09 01:45:40

标签: android listview

我试图在按钮单击的情况下在listview中动态添加元素,并使用自定义数组适配器。每当我添加新元素时,它都会将整个元素更改为默认数据。我在listview中有一个搜索栏,如果我将其值修改为反映到数据集,但是当我在UI中再次添加新元素时,它会重置为默认值。

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;
    }
}

主要活动:

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();

        }
    });

数据模型:

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;
}



}

1 个答案:

答案 0 :(得分:0)

每次点击都不应设置新的adapter。您应该将数据添加到现有适配器,然后在其上调用notifyDataSetChanged()。如果您要更改列表的所有内容并希望将其重置为顶部,则仅更改adapter,例如,如果您使用相同的列表来显示两个不同的数据集。