2016-02-16 40 views
0

在這裏,我需要將值保存到服務器,從JSON響應sqlite的,但不知道如何做到這一點到目前爲止,我曾嘗試是: 這是我的數據庫:如何從微調器json響應保存Valu到sqlite?

import android.content.ContentValues; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

import java.util.ArrayList; 
import java.util.List; 


public class Account_SF_DB extends SQLiteOpenHelper { 
    private static final int DATABASE_VERSION = 4; 
    private static final String DATABASE_NAME = "account.db"; 

    public Account_SF_DB(android.content.Context context) { 

     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_TABLE_ACCOUNT= "CREATE TABLE " + Model_Account.Accunt_Table + "(" 
       + Model_Account.id + " INTEGER PRIMARY KEY AUTOINCREMENT ," 
       + Model_Account.company_groups + " TEXT, " 
       + Model_Account.Parent_company + " TEXT, " 
       + Model_Account.Company_name + " TEXT, " 
       + Model_Account.Company_type + " TEXT ," 
       + Model_Account.Address_line1 + " TEXT ," 
       + Model_Account.Address_line2 +" TEXT ," 
       + Model_Account.Address_line3 + " TEXT ," 
       + Model_Account.State + " TEXT ," 
       + Model_Account.State_id + " INTEGER ," 
       +Model_Account.City + " TEXT ," 
       +Model_Account.CityID + " INTEGER ," 
       +Model_Account.Pincode + " TEXT ," 
       +Model_Account.Landline1 + " TEXT ," 
       +Model_Account.Landline2 + " TEXT ," 
       +Model_Account.Url + " TEXT ," 
       +Model_Account.Email_id +" TEXT ," 
       +Model_Account.Industry + " TEXT ," 
       +Model_Account.IndustryID + " INTEGER ," 
       +Model_Account.Account_manager+ " TEXT ," 
       +Model_Account.Account_managerid+ " INTEGER ," 
       +Model_Account.Region + " TEXT ," 
       +Model_Account.regionid + " INTEGER , " 
       +Model_Account.Muti_location +" TEXT ," 
       +Model_Account.mutilocationid+ " INTEGER" 
       +");"; 

       db.execSQL(CREATE_TABLE_ACCOUNT); 


    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + Model_Account.Accunt_Table); 
     onCreate(db); 
    } 
public void onInsert(Model_Account accountobj){ 
    SQLiteDatabase db=this.getWritableDatabase(); 
    ContentValues contentobj=new ContentValues(); 
    contentobj.put(Model_Account.company_groups,accountobj.getCompany_group()); 
    contentobj.put(Model_Account.Parent_company,accountobj.getParent_company()); 
    contentobj.put(Model_Account.Company_name,accountobj.getCompany_name()); 
    contentobj.put(Model_Account.Company_type,accountobj.getCompany_type()); 
    contentobj.put(Model_Account.Address_line1,accountobj.getAddrss_line1()); 
    contentobj.put(Model_Account.Address_line2,accountobj.getAddrss_line2()); 
    contentobj.put(Model_Account.Address_line3,accountobj.getAddrss_line3()); 
    contentobj.put(Model_Account.State,accountobj.getState()); 
    contentobj.put(Model_Account.State_id,accountobj.getStateid()); 
    contentobj.put(Model_Account.City,accountobj.getCity()); 
    contentobj.put(Model_Account.CityID,accountobj.getCityid()); 
    contentobj.put(Model_Account.Pincode,accountobj.getPincode()); 
    contentobj.put(Model_Account.Landline1,accountobj.getLandline1()); 
    contentobj.put(Model_Account.Landline2,accountobj.getLandline2()); 
    contentobj.put(Model_Account.Url,accountobj.getUrl()); 
    contentobj.put(Model_Account.Email_id,accountobj.getEmailid()); 
    contentobj.put(Model_Account.Industry,accountobj.getIndusty()); 
    contentobj.put(Model_Account.IndustryID,accountobj.getIndustry_id()); 
    contentobj.put(Model_Account.Account_manager,accountobj.getAccount_manager()); 
    contentobj.put(Model_Account.Account_managerid,accountobj.getAcm_id()); 
    contentobj.put(Model_Account.Region,accountobj.getRegion()); 
    contentobj.put(Model_Account.regionid,accountobj.getRegion_id()); 
    contentobj.put(Model_Account.Muti_location,accountobj.getMulti_location()); 
    contentobj.put(Model_Account.mutilocationid,accountobj.getMutilocation_id()); 
    db.insert(Model_Account.Accunt_Table,null,contentobj); 
    db.close(); 

} public int getTaskCount() { 
     int count=0; 
     String countQuery = "SELECT * FROM " + Model_Account.Accunt_Table; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     if(cursor != null && !cursor.isClosed()){ 
      count = cursor.getCount(); 
      cursor.close(); 
     } 
     return count; 

    } 
    public List<Model_Account> list(){ 
     String countQuery = "SELECT * FROM " + Model_Account.Accunt_Table; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     List<Model_Account>listobj=new ArrayList<Model_Account>(); 
     if (cursor.moveToFirst()) { 
      do { 
       Model_Account modelobj=new Model_Account(); 

       modelobj.setCompany_group(cursor.getString(cursor.getColumnIndex(Model_Account.company_groups))); 
       modelobj.setState(cursor.getString(cursor.getColumnIndex(Model_Account.State))); 
       listobj.add(modelobj); 

      }while (cursor.moveToNext()); 
     } 
     return listobj; 

    } 
    public void stateInsert(Model_Account modelobj){ 
     SQLiteDatabase db=this.getWritableDatabase(); 
     ContentValues contentobj=new ContentValues(); 
     contentobj.put(Model_Account.State,modelobj.getName()); 
     db.insert(Model_Account.Accunt_Table, null, contentobj); 
     db.close(); 
    } 
    public List<Model_Account>spinner_state(){ 
     String countQuery = " SELECT * FROM " + Model_Account.Accunt_Table; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     List<Model_Account>listobj=new ArrayList<Model_Account>(); 
     if (cursor.moveToFirst()) { 
      do { 
       Model_Account modelobj=new Model_Account(); 
       modelobj.setState(cursor.getString(cursor.getColumnIndex(Model_Account.State))); 
       listobj.add(modelobj); 

      }while (cursor.moveToNext()); 
     } 
     return listobj; 

    } 
    } 

這就是我需要保存數值:

String Url = "http://xx.x.x.xx/xx/AcoountCreatePageLoad.svc/Account/AccountPageLoad"; 
     JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, Url, new JSONObject(), 
       new Response.Listener<JSONObject>() { 
        @Override 
        public void onResponse(JSONObject response) { 
         String server_response = response.toString(); 
         try { 
          JSONObject json_object = new JSONObject(server_response); 
          JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadResult")); 
          for (int i = 0; i < json_array.length(); i++) { 
           Model_Account model_spinner = new Model_Account(); 
           JSONObject json_arrayJSONObject = json_array.getJSONObject(i); 
           model_spinner.setName(json_arrayJSONObject.getString("Name")); 
           model_spinner.setStateid(json_arrayJSONObject.getInt("PairLinkID")); 
           account_sf_db.stateInsert(model_spinner); 


          } 


         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 

        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(getBaseContext(), error.toString(), Toast.LENGTH_SHORT).show(); 

        } 
       }); 

     //Creating request queue 

     queue.add(jsonObjRequest); 
     List<Model_Account> yogan=account_sf_db.spinner_state(); 
     final ArrayAdapter<Model_Account> spinnerAdapter = new ArrayAdapter<Model_Account>(this, android.R.layout.simple_dropdown_item_1line, yogan); 

     spinner_state.setAdapter(spinnerAdapter); 

但它越來越空值我需要將其保存在單獨的表或在同一個表

回答

0

試試這個

if (cursor.moveToFirst()) { 
     do { 
      Model_Account modelobj=new Model_Account(); 
       modelobj.setCompany_group(cursor.getString(indexValue));//ex:cursor.getString(1) 
       modelobj.setState(cursor.getString(indexValue)); 
      listobj.add(modelobj); 

     }while (cursor.moveToNext()); 
    } 
    return listobj;