我是Android的新手,所以我无法估计如何通过Json从httprequest获取的数据库(SQLite)中保存数据。
这是我的代码:
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);
真的很感激任何帮助。致谢
答案 0 :(得分:2)
DataBean.java类为getter和setter方法创建
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;
}
}
在Activity类中。在object中设置值并插入数据库
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();
您必须为调度结果创建NCursorAdapter类(如自定义列表视图)。 这里的data.xml文件是你的自定义布局。哪个内容有两个textview ....“key_no”和“key_item”你的数据库表列名...
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;
}
}
在数据库类中插入数据并检索数据
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);
}
我希望它对您的应用程序有用。