我无法正确地将整个字符串放入我的arraylist中。我假设错误与我的自定义textwatcher或我如何循环遍历数组列表有关。这是我的代码:
case R.id.action_add:
Workout workout = new Workout("", "", "", "");
workoutList.add(workout);
ListView exerciseList = (ListView) findViewById(R.id.exerciseListView);
CustomAdapter myAdapter = new CustomAdapter(this, R.layout.custom_list, workoutList);
exerciseList.setAdapter(myAdapter);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public class CustomAdapter extends ArrayAdapter<Workout> {
private int layoutResource;
public CustomAdapter(Context context, int layoutResource, List<Workout> workoutList) {
super(context, layoutResource, workoutList);
this.layoutResource = layoutResource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(layoutResource, null);
}
Workout workout = getItem(position);
if (workout != null) {
final EditText exercise = (EditText) view.findViewById(R.id.exercise);
final EditText reps = (EditText) view.findViewById(R.id.reps);
final EditText sets = (EditText) view.findViewById(R.id.sets);
if (exercise != null){
exercise.addTextChangedListener(new CustomWatcher(exercise));
}
if (reps != null) {
reps.addTextChangedListener(new CustomWatcher(reps));
}
if (sets != null) {
sets.addTextChangedListener(new CustomWatcher(sets));
}
}
return view;
}
}
//Textwatcher class
class CustomWatcher implements TextWatcher {
private View view;
public CustomWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
public void afterTextChanged(Editable editable) {
String text = editable.toString();
switch (view.getId()) {
//exercises
case R.id.exercise:
exercises.add(text);
break;
//reps
case R.id.reps:
reps.add(text);
break;
//sets
case R.id.sets:
sets.add(text);
break;
}
}
}
}
案例是按钮。因此,当我单击添加按钮时,我尝试将自定义列表视图添加到包含三个文本视图的页面。然后,当我单击“保存”时,我试图将其从每个编辑文本位置保存到数组列表中以存储到我的数据库中。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection for menu
switch (item.getItemId()) {
case R.id.action_save:
String title = MyWorkoutsActivity.workouts.get(workoutID);
String ex;
String rp;
String st;
SQLDatabase db = new SQLDatabase(this);
for(int i = 0; i < workoutList.size(); i++){
//exercises, reps, and sets always same number so only one for loop needed
ex = exercises.get(i);
rp = reps.get(i);
st = sets.get(i);
db.addExercise(new Workout(title, ex, rp, st));
}
List<Workout> workouts = db.getAllExercises();
for (Workout wo : workouts) {
String log = "Workout " + wo.getWorkout() + "Exercise " + wo.getExercise() + " ,Reps " + wo.getReps() + " ,Sets " + wo.getSets();
// Writing workouts to log
Log.d("Workout ", log);
}
Intent intent = new Intent(this, MyWorkoutsActivity.class);
startActivity(intent);
return true;
答案 0 :(得分:0)
这种情况正在发生,因为在输入的第一个字母后,监听器方法afterTextChanged被激活,因此它会将第一个字母保存到列表中,依此类推 直到你最终得到一个字符列表。
解决方案是:只是不使用textwatcher,因为你想在用户按下保存按钮时保存数据,所以当按下按钮时将数据添加到练习列表中,如下代码:
public void onClick(View view){
//the exercise and reps and sets are the EditText fields that you have
exercises.add(exercise.getText());
exercises.add(reps.getText());
exercises.add(sets.getText());
}