如何在sqlite中存储json数据并在另一个活动中显示?

时间:2014-09-01 09:22:57

标签: android json eclipse sqlite

我试过...... this link但它对我没用..

1 个答案:

答案 0 :(得分:0)

MyDb db=new MyDb(this);
    db.open();
private static final String ClientName= "ClientName";
     private static final String projectName= "projectName";
ArrayList<HashMap<String, String>> contactList = null;
                 try {
                            JSONObject jsonObj = new JSONObject(yourJsonStringName);

                            // Getting JSON Array node
                           JSONArray contacts = jsonObj.getJSONArray(info);

                            // looping through All Contacts
                            for (int i = 0; i < contacts.length(); i++) {
                                JSONObject c = contacts.getJSONObject(i);

                                // tmp hashmap for single contact
                                HashMap<String, String> contactdata = new HashMap<String, String>();

                                // adding each child node to HashMap key => value
                                contactdata.put(ClientName, c.getString(ClientName));
                                contactdata.put(projectName, c.getString(projectName));
                                // To Insert into Db
                               ContentValues  cv=new ContentValues();
                              cv.put(MyDb.name, c.getString(ClientName).toString());
                              cv.put(MyDb.address, c.getString(projectName).toString());
                               db.insertdata(cv);
                                // adding contact to contact list
                               contactList.add(contactdata);

                                //tv=(TextView)findViewById(R.id.tv4);

                            }
                        } catch (JSONException e) {

                        }

为DB

创建一个单独的类
     public class MyDb 
{
    Sqldb db;
    Context context;
    SQLiteDatabase sdb;
    public static final String DB="DB.db";
    public static final String T_Name="table1";
    public static final String name="name";
    public static final String address="address";
    public static final String Table="create table table1(_id integer primary key,name text,address text)";
    // create construtor and call innner class" Sqldb"
    public MyDb(Context context) 
    {
        this.context=context;
        db=new Sqldb(context, DB, null, 1);
    }
    public void  open() 
    {
        sdb=db.getWritableDatabase();
    }
    // to insert data in table
    public void insertdata(ContentValues cv) 
    {
        sdb.insert(T_Name, null, cv);
    }

    //create one inner class and extend it by SQLITEOPENHELPER and then after add constructor and method
    public class Sqldb extends SQLiteOpenHelper
    {

        public Sqldb(Context context, String name, CursorFactory factory,
                int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(Table);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

    }
}