【问题标题】:how to store json data in sqlite and display in another activity?如何将 json 数据存储在 sqlite 中并显示在另一个活动中?
【发布时间】:2014-09-01 09:22:57
【问题描述】:

我试过..with this link 但它对我不起作用..

【问题讨论】:

  • 为什么需要 SQLite ???您可以简单地将 json 响应存储在您的应用程序中,并在整个应用程序中使用它。
  • 我必须将它存储在数据库中..!!
  • Shweta,很容易做到,这里不需要SQLite,你可以把它保存到你的应用中
  • 有要求吗?
  • @ShwetaGupta ya 如有必要,您可以将其存储起来。

标签: android json eclipse sqlite


【解决方案1】:
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

        }

    }
}

【讨论】:

  • 如果你仍然遇到问题,你可以通过 jeetendra.gupta21@gmail.com 给我发邮件,我会发给你完整的源代码,你可以参考
猜你喜欢
  • 1970-01-01
  • 2018-07-28
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多