猿问

如何修复 sqlite 在 android 中多次插入最后一个 EditText 值

我正在尝试在 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();

            }

        }


    });


}


湖上湖
浏览 132回答 2
2回答

小唯快跑啊

您在执行插入操作的循环中使用相同的 EditText,这是您以编程方式创建的 3 个中的最后一个。代替:EditText editText;使用数组存储 3 个 EditText:EditText[] editText;并将代码更改为:LinearLayout ll;EditText[] editText;TextView textView;String room;Button save;private DBHelper dbHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_room_name);&nbsp; &nbsp; final SharedPreferences mSharedPreferences =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PreferenceManager.getDefaultSharedPreferences(getBaseContext());&nbsp; &nbsp; String room_number = (mSharedPreferences.getString("room_number",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ""));&nbsp; &nbsp; ll = findViewById(R.id.ll);&nbsp; &nbsp; final int num = Integer.valueOf(room_number);&nbsp; &nbsp; dbHelper = new DBHelper(this);&nbsp; &nbsp; editText = new EditText[num];&nbsp;&nbsp; &nbsp; for (int i = 0; i < num; i++) {&nbsp; &nbsp; &nbsp; &nbsp; editText[i] = new EditText(this);&nbsp; &nbsp; &nbsp; &nbsp; editText[i].setHint("Room " + (i + 1) + " Name");&nbsp; &nbsp; &nbsp; &nbsp; textView = new TextView(this);&nbsp; &nbsp; &nbsp; &nbsp; textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);&nbsp; &nbsp; &nbsp; &nbsp; ll.addView(editText[i]);&nbsp; &nbsp; &nbsp; &nbsp; ll.addView(textView);&nbsp; &nbsp; }&nbsp; &nbsp; save = new Button(this);&nbsp; &nbsp; save.setText("save");&nbsp; &nbsp; ll.addView(save);&nbsp; &nbsp; save.setOnClickListener(v -> {&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < num; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dbHelper.insertRoom(editText[j].getText().toString())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(), "done",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(), "No", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}

不负相思意

你的做法不正确。您应该为每个 EditText 使用 Tag 并通过它的 Tag 获取每个 EditText。LinearLayout ll;&nbsp; EditText editText;&nbsp; &nbsp;TextView textView;&nbsp; &nbsp;String room;&nbsp; &nbsp; Button save;&nbsp; &nbsp; &nbsp; private DBHelper dbHelper;&nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp;protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp;super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_room_name);&nbsp; &nbsp; &nbsp;final SharedPreferences mSharedPreferences =&nbsp;&nbsp; &nbsp; PreferenceManager.getDefaultSharedPreferences(getBaseContext());&nbsp; &nbsp; &nbsp; &nbsp; String room_number = (mSharedPreferences.getString("room_number",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;""));&nbsp; &nbsp; &nbsp; ll = findViewById(R.id.ll);&nbsp; &nbsp; &nbsp; final int num = Integer.valueOf(room_number);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbHelper = new DBHelper(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int i = 0; i < num; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editText = new EditText(this);&nbsp; &nbsp; &nbsp; &nbsp; editText.setHint("Room " + (i + 1) + " Name");&nbsp; &nbsp; &nbsp; &nbsp; //set tag for edit text&nbsp; &nbsp; &nbsp; &nbsp;editText.setTag(i);&nbsp; &nbsp; textView = new TextView(this);&nbsp; &nbsp; textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);&nbsp; &nbsp; ll.addView(editText);&nbsp; &nbsp; ll.addView(textView);}save = new Button(this);save.setText("save");ll.addView(save);save.setOnClickListener(v ->{&nbsp; &nbsp; for (int j = 0; j < num; j++){&nbsp; &nbsp; &nbsp; &nbsp; if(dbHelper.insertRoom(editText.findViewWithTag(j).getText().toString())){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(), "done",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(), "No", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }});&nbsp;}
随时随地看视频慕课网APP

相关分类

Java
我要回答