带有ArrayAdapter和EditText的ListView如何存储已更改的数据

时间:2015-09-05 14:49:33

标签: android listview view android-edittext android-arrayadapter

我有这个问题:

我创建了一个Listview,其中包含由ArrayAdapter组成的自定义EditText,在插入Listview的活动中,有一个用于添加新行的按钮Listview克隆了最后一个。

  

列表的行为是当我单击所有ADD按钮时   对列表视图行所做的更改将丢失。

我弄清楚我需要插入一些代码来将更改保存到ArrayAdapter的对象中,但我不能很好地解决这个问题。

你能请求给我一些建议吗?

谢谢。

ArrayAdapter

public class ExerciseAdapter extends ArrayAdapter<Exercise_p_obj> {

private int resource;
private LayoutInflater inflater;
private ArrayList<Exercise_p_obj> listExercises = new ArrayList<Exercise_p_obj>();

public ExerciseAdapter(Context context, int resourceId, ArrayList<Exercise_p_obj> objects) {
    super(context, resourceId, objects);
    resource = resourceId;
    inflater = LayoutInflater.from(context);
    listExercises = objects;
}

@Override
public View getView(final int position, View v, ViewGroup parent) {

    // Recuperiamo l'oggetti che dobbiamo inserire a questa posizione
    final Exercise_p_obj exercise_p = getItem(position);

    final ViewHolder holder;

    if (v == null) {
        v = inflater.inflate(resource, parent, false);
        holder = new ViewHolder();
        holder.et_reps = (EditText) v.findViewById(R.id.et_reps);
        holder.et_kg   = (EditText) v.findViewById(R.id.et_kg);
        holder.et_rest = (EditText) v.findViewById(R.id.et_rest);
        holder.bt_del  = (Button)   v.findViewById(R.id.bt_del);
        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

    holder.et_reps.setText(String.valueOf(exercise_p.getReps()));
    holder.et_kg.setText(String.valueOf(exercise_p.getWeight()));
    holder.et_rest.setText(String.valueOf(exercise_p.getRest()));

    holder.bt_del.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            listExercises.remove(position);
            notifyDataSetChanged();
        }
    });

    return v;
}

private static class ViewHolder {
 EditText et_reps;
 EditText et_kg;
 EditText et_rest;
 Button   bt_del;}}

主要活动

    public class ExerciseCreate extends AppCompatActivity {
Toolbar toolbar;
ListView listView;
Button btAdd;
Button btDel;
ExerciseAdapter exerciseAdapter;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.createexercise);
    listView = (ListView) findViewById(R.id.exerciselistView);
    btAdd    = (Button) findViewById(R.id.bt_add);
    toolbar  = (Toolbar) findViewById(R.id.app_bar);

    exerciseAdapter = new ExerciseAdapter(this, R.layout.rowexercise,new ArrayList<Exercise_p_obj>());
    exerciseAdapter.add(new Exercise_p_obj(1,1,1,1,1,1));

    listView.setAdapter(exerciseAdapter);
    setSupportActionBar(toolbar);

    btAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final int count = exerciseAdapter.getCount();
            Exercise_p_obj clone = exerciseAdapter.getItem(count - 1);
            exerciseAdapter.add(new Exercise_p_obj(0, clone.getId_exer_h(), clone.getReps(), clone.getSecs(), clone.getWeight(), clone.getRest()));
        }
    });

}}

1 个答案:

答案 0 :(得分:0)

对数据使用单独的列表。如果要添加新项目,首先将其添加到列表中,然后需要调用adapter.notifyDataSetChanged()

public class ExerciseCreate extends AppCompatActivity {
    Toolbar toolbar;
    ListView listView;
    Button btAdd;
    Button btDel;
    ExerciseAdapter exerciseAdapter;
    List<Exercise_p_obj> items = new ArrayList<Exercise_p_obj>();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createexercise);
        listView = (ListView) findViewById(R.id.exerciselistView);
        btAdd    = (Button) findViewById(R.id.bt_add);
        toolbar  = (Toolbar) findViewById(R.id.app_bar);

        items.add(new Exercise_p_obj(1,1,1,1,1,1));
        exerciseAdapter = new ExerciseAdapter(this, R.layout.rowexercise,items);
        listView.setAdapter(exerciseAdapter);
        setSupportActionBar(toolbar);

        btAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final int count = items.size();
                Exercise_p_obj clone = items.get(count - 1);
                items.add(new Exercise_p_obj(0, clone.getId_exer_h(), clone.getReps(), clone.getSecs(), clone.getWeight(), clone.getRest()));
                exerciseAdapter.notifyDataSetChanged();
            }
        });
    }
}