Save data from Json object to Database (SQLite)
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: final ArrayList<HashMap<String, String>> mylist4 = new ArrayList<HashMap<String, String>>();
try{
JSONObject jObj = new JSONObject(rfiItems);
JSONArray data4 = jObj.getJSONArray("data");
//data4 = json4.getJSONArray("data");
//Toast.makeText(getApplicationContext(), data4.toString(), Toast.LENGTH_LONG).show();
for(int i=0;i<data4.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = data4.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("rfi_data1", "" + e.getString("item_type"));
map.put("rfi_data2", "" + e.getString("change_number"));
map.put("rfi_data3", "" + e.getString("to_vendor"));
map.put("rfi_data4", "" + e.getString("status"));
开发者_开发问答 map.put("rfi_data5", "" + e.getString("title"));
map.put("rfi_data6", "" + e.getString("change_date"));
map.put("rfi_data7", "" + e.getString("responded_date"));
mylist4.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter4 = new SimpleAdapter(this, mylist4 , R.layout.item_list4,
new String[] { "rfi_data1", "rfi_data2","rfi_data3", "rfi_data4","rfi_data5","rfi_data6","rfi_data7"},
new int[] { R.id.rfi_item_type, R.id.rfi_change_no,R.id.rfi_to_vendor,R.id.rfi_status,R.id.rfi_title,R.id.rfi_change_date,R.id.rfi_responded_date });
setListAdapter(adapter4);
Any help will really be appreciated. THANKS
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.
精彩评论