gpt4 book ai didi

java - 将字符串 ArrayList 放入 SQLite 数据库

转载 作者:太空宇宙 更新时间:2023-11-04 12:37:18 28 4
gpt4 key购买 nike

我无法将整个字符串正确放入数组列表中。我假设该错误与我的自定义文本观察器或我循环数组列表的方式有关。这是我的代码:

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

外壳是按钮。因此,当我单击添加按钮时,我尝试将自定义 ListView 添加到我的页面,其中包括三个 TextView 。然后,当我单击“保存”时,我试图让它将每个编辑文本位置的数据保存到数组列表中,以存储到我的数据库中。

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

最佳答案

发生这种情况是因为监听器方法 afterTextChanged 在输入第一个字母后启动,因此它将第一个字母保存到列表中,依此类推直到你最终得到一个字符列表。

解决方案是:不要使用文本观察器,因为您想在用户按下保存按钮时保存数据,因此在按下按钮时将数据添加到练习列表中,如下代码:

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

}

关于java - 将字符串 ArrayList 放入 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37193705/

28 4 0