【问题标题】:how to get all value in array android and insert into database如何获取数组android中的所有值并插入数据库
【发布时间】:2016-08-25 10:50:04
【问题描述】:

我已经在数组中获取了所有联系人号码(姓名、号码)并显示在列表视图中。我已经知道使用 volley 将单个值插入数据库的方法,但我不知道如何插入数组。

        cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor1);

        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID}; 

        int[] to = {android.R.id.text1, android.R.id.text2}; 

        SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor1, from, to);
        setListAdapter(listadapter);
        lv = getListView();

排球

 @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                //Adding parameters to request
                params.put("fromV", from);

我从 params.put 中得到错误 ..

【问题讨论】:

    标签: android database string insert contact


    【解决方案1】:

    您应该将 String 作为第二个参数传递,但您传递的是 String[];如果您只需要 ArrayList 则可以将 String[] 转换为 List 参见this

    【讨论】:

      【解决方案2】:

      你可以为插入数组做这样的事情

      public void Insert_phone_contact(String [] contact){//Arrary of your contacts
      try{
      
      SQLiteDatabase DB = this.getWritableDatabase();
      ContentValues cv = new ContentValues(); //put your contacts in the content values in the loop
      for(int i=0;i<contact.length;i++){            
          cv.put(CONTACT_NAME, contact[i]);
          DB.insert(TABLE_CONTACTS, null, cv); //Insert each time for loop count            
      }
      DB.close(); // Now close the DB Object
      }
      catch(Exception ex){
      Log.e("Error in phone contact insertion", ex.toString());
      }
      

      【讨论】:

        猜你喜欢
        • 2016-04-10
        • 1970-01-01
        • 2019-03-05
        • 2018-05-17
        • 2016-06-15
        • 2020-05-02
        • 2021-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多