Save data from Json object to Database (SQLite)

后端 未结 1 1507
醉酒成梦
醉酒成梦 2020-12-22 06:57

I am newbie in Android, so I can not estimate how do I save data in database(SQLite) that were got from httprequest through Json.
Here is my code:

               


        
相关标签:
1条回答
  • 2020-12-22 07:37

    DataBean.java class create for getter and setter method

        public class DataBean {
    
            private String item_type = null;
            private String change_number = null;
    
            public String getItem_type() {
                return item_type;
            }
            public void setItem_type(String itemType) {
                item_type = itemType;
            }
            public String getChange_number() {
                return change_number;
            }
            public void setChange_number(String changeNumber) {
                change_number = changeNumber;
            }
    
    
        }
    

    in your Activity class..set value in object and insert into database

            private DatabaseHelper mDbHelper;
            DataBean dataBean;
            private Cursor mNotesCursor;
            private NewsCursorAdapter adapter = null;
    
                mDbHelper = new DatabaseHelper(this);
                ArrayList<DataBean> liststck = new ArrayList<DataBean>();
    
    
    
            for(int i=0;i<data4.length();i++){                      
    
                   JSONObject e = data4.getJSONObject(i);
    
                     dataBean = new DataBean();
    
                   dataBean.setChange_number(e.getString("eqid"));
                   dataBean.setItem_type(e.getString("magnitude"));
    
                       liststck.add(dataBean);
    
                   }  
    
    
                mDbHelper.deleteRecords();
                for (DataBean dataBean : liststck) {
                    mDbHelper.insertdata(dataBean);
                }
                mNotesCursor = mDbHelper.retrievedata();
                startManagingCursor(mNotesCursor);
    
                adapter.changeCursor(mNotesCursor);
                adapter.notifyDataSetChanged();
    

    you must create NCursorAdapter class for dispay result(like custom listview). here data.xml file is your custom layout. which content two textview.... "key_no" and "key_item" your database table column name...

        public class NCursorAdapter extends CursorAdapter {
    
            private Cursor mCursor;
            private Context mContext;
            private final LayoutInflater mInflater;
    
    
            public NCursorAdapter(Context context, Cursor c) {
                super(context, c);
                // TODO Auto-generated constructor stub
                mInflater = LayoutInflater.from(context);
                mContext = context;
            }
    
            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                // TODO Auto-generated method stub
                TextView title = (TextView) view.findViewById(R.id.title);
                title.setText(cursor.getString(cursor.getColumnIndex("key_no")));
    
                TextView date = (TextView) view.findViewById(R.id.date);
                date.setText(cursor.getString(cursor.getColumnIndex("key_item")));
    
            }
    
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                // TODO Auto-generated method stub
                final View view = mInflater.inflate(R.layout.data, parent, false);
    
                return view;
            }
    
        }
    

    in your database class insert data and retrieve data

    public Long insertdata(DataBean dataBean) {
            ContentValues contentValues = new ContentValues();
            contentValues.put(KEY_NO, dataBean.getChange_number());
            contentValues.put(KEY_ITEM, dataBean.getItem_type());
            return sqliteDatabase.insert(DATABASE_TABLE, null, contentValues);
        }
    
    
    public Cursor retrievedata() {
                return sqliteDatabase.query(DATABASE_TABLE, new String[] { KEY_ROWID,KEY_NO, KEY_ITEM }, null,null);
    
            }
    

    I hope it is useful for your application.

    0 讨论(0)
提交回复
热议问题