我正在尝试在 android 的 sqlite 数据库中插入 3 个 Edittext 值,插入功能运行良好,但它插入了最后一个 edittext 值 3 次。我插入 (john, smith, anna) 和 sqlite 插入 (anna, anna, anna) 这是我的代码
LinearLayout ll;
EditText editText;
TextView textView;
String room;
Button save;
private DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_name);
final SharedPreferences mSharedPreferences =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String room_number = (mSharedPreferences.getString("room_number",
""));
ll = findViewById(R.id.ll);
final int num = Integer.valueOf(room_number);
dbHelper = new DBHelper(this);
for (int i = 0; i < num; i++) {
editText = new EditText(this);
editText.setHint("Room " + (i + 1) + " Name");
textView = new TextView(this);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
ll.addView(editText);
ll.addView(textView);
}
save = new Button(this);
save.setText("save");
ll.addView(save);
save.setOnClickListener(v ->{
for (int j = 0; j < num; j++){
if(dbHelper.insertRoom(editText.getText().toString())){
Toast.makeText(getApplicationContext(), "done",
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "No", Toast.LENGTH_LONG).show();
}
}
});
}
小唯快跑啊
不负相思意
相关分类