【问题标题】:How to insert two arralist into SQLite at sametime?如何将两个数组列表同时插入 SQLite?
【发布时间】:2017-08-02 17:00:27
【问题描述】:

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

我只想喜欢:

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

    }

在第二次活动中从数据库中获取值:

 while(data.moveToNext()) {

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

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

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

我的自定义适配器:

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

【问题讨论】:

  • 你想用 for 循环做什么?还要检查一下写得好不好,中途可以改正。
  • 我正在尝试将所有 arraylist 记录到数据库中。如果你问我为什么要设置 null ?因为我的数据库助手无法应用。我想将 selectedlist 设置为第 1 列,将 selectedlistarama 设置为第 2 列。
  • 如果我理解正确你的问题,请查看我的答案。

标签: android sqlite android-contentprovider


【解决方案1】:

假设:

selectedlist 是 COL_2(名称)的 String 的 ArrayList。

selectedlistaramaString 的 COL_3(数字)的 ArrayList。

注意:如果您真的想保留数字,请使用 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);
}

【讨论】:

  • 是的,您的回答是正确的。感谢您的帮助。另外我想为其他用户改进我的问题。检查您的书面内容是什么意思?您的意思是您的英语语法不好还是您的代码不好.你有什么推荐的?
  • 对不起,我指的是代码。我认为 for 外观有一个 { 在错误的行上打开。
  • 没问题。良好的编码。
猜你喜欢
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多