Arraylist:通过单击按钮向项目添加文本

时间:2016-12-06 18:25:01

标签: android button arraylist

我有一个带有文本字段和按钮的Arrayadpter。每次我按下按钮时,文本字段应该添加1.问题是当我滚出屏幕时我丢失了输入。我知道我应该使用CheckForUpdateListener或类似的东西,但我不知道如何实现它。谢谢你的帮助:

public class startseitenAdapter extends ArrayAdapter<getraenkeObjekt> {

Context context;
int layoutResourceId;
int anzahl;
ArrayList<getraenkeObjekt> data = new ArrayList<getraenkeObjekt>();

public startseitenAdapter(Context context, int layoutResourceId, ArrayList<getraenkeObjekt> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    UserHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId, parent, false);
        if (position % 2 == 1) {
            convertView.setBackgroundColor(Color.DKGRAY);
        } else {
            convertView.setBackgroundColor(Color.BLACK);
        }

        holder = new UserHolder();
        holder.getraenkename = (TextView)  

 holder.getraenkename = (TextView) convertView.findViewById(R.id.textView1);
    holder.plus = (Button) convertView.findViewById(R.id.button);
    holder.anzahl = (TextView) convertView.findViewById(R.id.textView);


        convertView.setTag(holder);

    } else {
        holder = (UserHolder) convertView.getTag();
    }


    final UserHolder finalHolder = holder;
    holder.plus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int summand = 1;
            Log.d("Adapter", "plus " + summand + " Position " + position);
            MySQLiteHelper m = new MySQLiteHelper(context);
            final int globid = position;
            int anzahl = m.wertAenderung(globid, summand);
            finalHolder.anzahl.setText(anzahl + " ");
           }
    });




    getraenkeObjekt drink = data.get(position);

    holder.getraenkename.setText(drink.getname());
    holder.anzahl.setText(drink.getanzahl()+" ");


    return convertView;
}


static class UserHolder {
    TextView getraenkename;
    TextView anzahl;
    Button plus;
}

1 个答案:

答案 0 :(得分:0)

您应该使用ArrayList或数组来保存上次更新的值...无论何时向下或向上滚动,您的视图对象都会覆盖旧值并且不会存储在任何位置的新值。你必须将它们存储在数组列表或类似的东西中。

我们假设您正在点击第4行按钮对象。你的第4行textview应该被更改所以放ArrayList.set(4,"updatedvalue");,当你向下或向上滚动时从数组列表中获取值,其中包含位置......作为holder.textview.setText(ArrayList.get(position))

这将做你需要的......

希望它有所帮助!