Venky
Venky

Reputation: 2067

How to transfer Downloaded JSONArray to a Database?

Thanks a lot for the previous response to my query I have the JSONArray in buffer.I might be wrong when i say these things,so do correct me.

public void get_data(String data) {

    try {
        JSONArray data_array=new JSONArray(data);

        for (int i = 0 ; i < data_array.length() ; i++)
        {
            JSONObject obj=new JSONObject(data_array.get(i).toString());
            String[] nameArray;
            String[] distArray;
            String[] latArray;
            nameArray[i] = (data_array.getJSONObject(i).getString("rtrname"));
            distArray[i] = (data_array.getJSONObject(i).getString("ctgname"));
            latArray[i] = (data_array.getJSONObject(i).getString("rtrphone"));
            //lonArray[i] = (data_array.getJSONObject(i).getString("longitude"));


        }

        //adapter.notifyDataSetChanged();

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

Now instead of transferring this data to nameArray[i] and so and so,what should i do to transfer this to a Database(SQLite).Ive already created a DBHlper.java class to deal with it,but im new to this and im missing something.

public boolean insertData(String rtrname, String ctgname,String rtradd1) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues();

    //contentValues.put(COL_2,rtrphoneno);
    contentValues.put(COL_2,ctgname);
    contentValues.put(COL_3, rtrname);
    contentValues.put(COL_4, rtradd1);
    long result = db.insert(TABLE_NAME,null ,contentValues);
    if(result == -1)
        return false;
    else
        return true;

1)What should i do in the main class so that i can get each JSONArray node be transferred to the Column of the table??

Upvotes: 0

Views: 132

Answers (1)

Patel Vicky
Patel Vicky

Reputation: 776

Use Following Way

for (int i = 0 ; i < data_array.length() ; i++)
        {
            JSONObject obj=new JSONObject(data_array.get(i).toString());
            String[] nameArray;
            String[] distArray;
            String[] latArray;
            nameArray[i] = (data_array.getJSONObject(i).getString("rtrname"));
            distArray[i] = (data_array.getJSONObject(i).getString("ctgname"));
            latArray[i] = (data_array.getJSONObject(i).getString("rtrphone"));
            //lonArray[i] = (data_array.getJSONObject(i).getString("longitude"));

            db.insert(nameArray[i],distArray[i],latArray[i]);

        }

//I use this method to copy one by one item in database column.see my source code

 // results is arrayList
for(int ij=0; ij<results.size(); ij++)
{
   //First four is static value
   mb.insertEntry(1,1,1, 1, results.get(ij).item, "1", results.get(ij).price, results.get(ij).price);
}

Upvotes: 0

Related Questions