如何在数组android中获取所有值并插入到数据库中

时间:2016-04-30 08:14:48

标签: android database string insert contact

我已经获得了数组中的所有联系号码(名称,号码)并在listview中显示..我已经知道使用齐射将单个值插入数据库但我不知道如何插入数组。

        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中得到错误。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以为插入数组

执行类似的操作
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());
}
相关问题