我正在将资产文件夹中的json数据解析为房间数据库,并使用onCreate
显示数据库中的数据。我没有这样做的问题,但问题来自于我打开应用程序 {
"sample":[
{
"guide_topic": "toccata_fugue",
"guide_details": "We are conducting a research on how"
},
{
"guide_topic": "toccata_fugue",
"guide_details": "Johann Sebastian Bach"
},
{
"guide_topic": "toccata_fugue",
"guide_details": "Johann Sebastian Bach"
}
]}
任何时候屏幕上的数据都被复制,尝试在共享首选项中创建一个标志,以指示数据是否已保存为第一个时间并在保存另一个数据之前检查它,还试图在离开活动后删除表的数据,但似乎onDestroy和onStop没有调用所有努力证明abortive我需要一个逻辑来处理这个以防止数据重复或任何链接可以提供帮助,在发布之前已经在线搜索过。
我的json数据
package com.africanego.righthajj.ui;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.africanego.righthajj.R;
import com.africanego.righthajj.adapters.HajjGuideAdapter;
import com.africanego.righthajj.db.DbDatabase;
import com.africanego.righthajj.models.HajjGuideModel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Created by ${sodiqOladeni} on 3/30/2018.
*/
public class HajjGuide extends AppCompatActivity {
private RecyclerView mRecyclerView;
private DbDatabase mDbDatabase;
private static final String DATABASE_FLAG = "data_already_exist";
private HajjGuideModel hajjGuideModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hajj_guide);
mRecyclerView = findViewById(R.id.rv_hajj_guide);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
SharedPreferences checkSharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
String dataPresentToken = checkSharedPreferences.getString("data_present_token", "");
// if (dataPresentToken.isEmpty() || !dataPresentToken.matches(DATABASE_FLAG)) {
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray sampleArray = obj.getJSONArray("sample");
for (int i = 0; i < sampleArray.length(); i++) {
JSONObject readGuide = sampleArray.getJSONObject(i);
String guideTopic = readGuide.getString("guide_topic");
String guideDetails = readGuide.getString("guide_details");
hajjGuideModel = new HajjGuideModel();
hajjGuideModel.setGuideTopics(guideTopic);
hajjGuideModel.setGuideDetails(guideDetails);
}
} catch (JSONException e) {
e.printStackTrace();
}
long insertRow = MainActivity.mDbDatabase.hajjGuideDao().insertHajjGuides(hajjGuideModel);
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("data_present_token", DATABASE_FLAG);
editor.apply();
List<HajjGuideModel> newData = MainActivity.mDbDatabase.hajjGuideDao().loadGuidesTopics();
mRecyclerView.setAdapter(new HajjGuideAdapter(this, newData));
// }else {
// List<HajjGuideModel> newData = MainActivity.mDbDatabase.hajjGuideDao().loadGuidesTopics();
// mRecyclerView.setAdapter(new HajjGuideAdapter(this, newData));
// }
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getApplicationContext().getAssets().open("hajjguide.righthajj.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
@Override
protected void onDestroy() {
super.onDestroy();
MainActivity.mDbDatabase.hajjGuideDao().deleteHajjGuides();
}
@Override
protected void onStop() {
super.onStop();
MainActivity.mDbDatabase.hajjGuideDao().deleteHajjGuides();
}
@Override
protected void onPause() {
super.onPause();
MainActivity.mDbDatabase.hajjGuideDao().deleteHajjGuides();
}
}
MainActicity
set @s = CONCAT('select distinct ? , ? , ? , ?;');
PREPARE stmt FROM @s;
EXECUTE stmt USING @frm_date, @to_date, fc_frm_date, fc_to_date;
DEALLOCATE PREPARE stmt;
评论如果您需要更多信息以澄清,我的意思是复制数据保持三个,它将成为6,9,12 ......正如我正在开展活动。