![](/img/trans.png)
[英]best way to insert data into android sqlite database using large json data coming from server using cordova
[英]Storing data coming from the server in form of JSON onto Android Sqlite database
我從未在SQlite android數據庫上工作過。 因此,如果可能的話,在這個問題上,我將不勝感激。
我有一項服務,該服務以JSON
形式提供了狀態名稱列表,我需要將其解析並存儲到android中的數據庫中,並將其顯示在ListView
。 目前,我們已經能夠成功解析JSON
數據,然后借助以下代碼將其直接顯示在ListView
上:
public class OpenMeetingsListActivity extends ListActivity
{
private Context context;
private static String url = "http://myurl.com/mycountry/statelist.php";
private static final String TAG_POSTS = "posts";
private static final String TAG_SN = "sn";
JSONArray posts = null;
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
setContentView(R.layout.main);
TextView t=(TextView)findViewById(R.id.textView_header);
t.setText("Select Your State");
new ProgressTask(OpenMeetingsListActivity.this).execute();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String stateSelected = ((TextView) v.findViewById(R.id.sn)).getText().toString();
String url="http://myurl.com/mycountry/citylist.php?stname="+URLEncoder.encode(stateSelected);
Intent intent=new Intent(OpenMeetingsListActivity.this,OpenMeetingsListActivity_Items.class);
intent.putExtra("url", url);
intent.putExtra("stateSelected",stateSelected);
startActivity(intent);
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
OpenMeetingsListActivity op;
public ProgressTask(ListActivity activity) {
Log.i("1", "Called");
context = activity;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(context, jsonlist,
R.layout.list_openmeeting_item, new String[] { TAG_SN }, new int[] {
R.id.sn });
setListAdapter(adapter);
// selecting single ListView item
lv = getListView();
}
protected Boolean doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
posts = json.getJSONArray(TAG_POSTS);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
String sn = c.getString(TAG_SN);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_SN, sn);
jsonlist.add(map);
} }catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
現在,我需要在此代碼中進行的更改或需要添加到此代碼中的內容是,我需要存儲來自鏈接的JSON數據
首先到android中的數據庫。 然后,我需要從數據庫中獲取該數據並將其顯示到android中的ListView
上。
完成后,我需要再次解析一個URL,例如
http://myurl.com/mycountry/citylist.php?stname= “” + URLEncoder.encode(stateSelected)
在下一類中顯示相應州(在上一類中選擇的州)的相應城市。 上面的url以JSON
形式給出了城市名稱,需要再次對其進行解析並將其存儲到數據庫中並顯示在ListView
。
最后,當人們在ListView中單擊一個城市的名稱時,它會重定向到第三個也是最后一個類,在下面形成此URL
http://myurl.com/mycountry/openmeet.php?reg= “ + state +”&cityvalid =“ + city
使用前兩個類提供的信息。 該網址提供了一些JSON
數據,這些數據又需要解析,存儲並顯示在ListView
。
因此,我的應用程序這里具有三個類:1.在第一類中,狀態名稱顯示在視圖上。 2.在第二類中,取決於在上一類中選擇了哪個州,相應的城市名稱會顯示在視圖上。 3.在第三類中,所選州和所選城市的組合URL提供與該特定城市相關的信息詳細信息。
所有數據都來自服務器 。
通過將直接來自服務器的數據顯示到ListView
上,我已經能夠實現此功能。 現在,我需要先將其存儲到數據庫中,然后將其顯示在所有三個類的ListView
。
任何幫助表示贊賞。
protected Boolean doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
posts = json.getJSONArray(TAG_POSTS);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
String sn = c.getString(TAG_SN);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_SN, sn);
jsonlist.add(map);
//Just Put your database insert query here
ContentValues contentValues = new ContentValues();
contentValues.put(TAG_SN, sn);
database.insert(TABLE_NAME, null, contentValues);
} }catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
祝好運...
通過將直接來自服務器的數據顯示到ListView上,我已經能夠實現此功能。 現在,我需要先將其存儲到數據庫中,然后將其顯示在所有三個類的
ListView
。
恕我直言,您只需要知道android sqlite CRUD操作的工作方式,以下是您可以采取的一些步驟來解決問題:
json
數據中的名稱相關聯的值將是列中的數據集 。 ListView
,您必須從sqlite獲取(讀取)數據,然后使用Asynctask
您可以再次填充它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.