如何同时将两个数组列表插入到 SQLite 中?

How to insert two arralist into SQLite at sametime?

我为我的应用程序创建了一个数据库。我正在尝试插入 3 个值。但我的问题是在尝试插入第二个值时,它们会转到第一列。这个值来自用户的 phone 数字(因此来自内容提供商)。当我尝试启动我的代码时,所有列都设置在第一列,除了或一列。我想将这些值设置为 3 个不同的列. 它显示了 10 个结果,但我选择了 5 个名字。我想显示 5 个结果。其他 5 个结果转到第 2 列。我在第二个 activity 使用自定义列表视图。另一方面,它们可能首先加载列,但我看不到我的数字值我是数据库初学者。

我只想点赞:

ID name    number  oran 
1  Ahmet   5555    50
2  Mehmet  6666    50
3  Veli    3333    50
4  Can     2222    50
5  Zehra   11111   50 
etc.

我的数据库助手:

class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "DatabaseHelper";

    private static final String TABLE_NAME = "people_table";
    private static final String COL1 = "ID";
    private static final String COL2 = "name";
    private static final String COL3 = "number";
    private static final String COL4 = "oran";



     public DatabaseHelper(Context context) {
            super(context, TABLE_NAME, null, 1);
        }
        public void onCreate(SQLiteDatabase db) {
            String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +  COL2 + " TEXT,"+
                     COL3 + " TEXT,"+  COL4 + " TEXT)";
            db.execSQL(createTable);
        }
      // COL2 +" TEXT)0" col3 yerine 2 koyulması gerekiyor.
        @Override
        public void onUpgrade(SQLiteDatabase db, int i, int i1) {
            db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
            onCreate(db);
        }

        public boolean addData(String item , String number   ) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put(COL2, item );
    contentValues.put(COL3 , number);
    contentValues.put(COL4, "50");


            Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);

            long result = db.insert(TABLE_NAME, null, contentValues);

            //if date as inserted incorrectly it will return -1
            if (result == -1) {
                return false;
            } else {
                return true;
            }
        }

当用户点击保存数据按钮时:

 for (String newEntry: selectedlist)
                                AddData(newEntry,null);    {

                            }
                            for (String number : selectedlistarama)
                                AddData(null , number);    {

                            }

增加值:

 private void AddData(String newEntry ,String number) {

        boolean insertData = mDatabaseHelper.addData(newEntry ,number);
        if (insertData) {
            Cursor data = mDatabaseHelper.getData();
            sayı = data.getCount();
            //   toastMessage("Data Successfully Inserted!");
            Toast.makeText(this,String.valueOf(sayı), Toast.LENGTH_SHORT).show();

        } else {
            toastMessage("Something went wrong");
        }

    }

第二次从数据库获取值Activity:

 while(data.moveToNext()) {

            listData.add(data.getString(1));

            listDatanumber.add(data.getString(2));

           listDataoran.add(data.getString(3));
    }

我的 Custom_adapter :

namesbox.setText(listData.get(position));

假设:

selectedlistString 的 ArrayList for COL_2 (name).

selectedlistaramaString 的 ArrayList for COL_3 (number).

注意:如果您真的想保留数字,请使用 ArrayList<Integer> 并存储 int 值。

我还假设两个列表中的项目数相同,因此 selectedlist 的第一项对应于 selectedlistarama 的第一项,依此类推。

然后,调用 addData(String String) 你会这样做:

for(int i=0; i<selectedlist.size(); i++){
    String name = selectedlist.get(i);
    String number = selectedlistarama.get(i);
    addData(name, number);
}