newdeveloper newdeveloper - 4 months ago 11
Android Question

duplicating data when fetch from database in android

hey guys i am working on database first i am fetching data from json and insert in database after when network is not available then display value from database their is going good but when fetching data from database values is duplicating

code is activity

NetworkUtils utils = new NetworkUtils(getApplicationContext());
if(utils.isConnectingToInternet())
{
getSpinnerData();
}
else
{
List<VideoJson> videoList = db.getAllVideos();
Log.e("List is","=> "+videoList);
adapter = new VideoAdapter(videoList, this);
recyclerView.setAdapter(adapter);
}
}


and inserting value in database

private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
VideoJson model = new VideoJson();
JSONObject json = null;
try {
json = array.getJSONObject(i);
String vedio_name = json.getString(Config.TAG_VIDEO_TEXT);
String vedio_url = json.getString(Config.TAG_VIDEO_URL);
String vedio_img = json.getString(Config.TAG_VIDEO_IMAGE);
model.setName_video(vedio_name);
model.setImageUrl_video(vedio_url);
model.setVedio_thambnail(vedio_img);
Log.e("name","is =>"+vedio_name);
Log.e("url","is =>"+vedio_url);
Log.e("image","is =>"+vedio_img);
db.addVideos(model);
Log.e("database","value is"+db);
} catch (JSONException e) {
e.printStackTrace();
}


database is

public class VideoDataBase extends SQLiteOpenHelper {

private static final int DB_VERSION = 4;
private static final String DB_NAME = "appname";
private static final String TABLE_VIDEOS= "videos";

private static final String KEY_VIDEO_ID = "video_id";
private static final String KEY_VIDEO_NAME = "video_id_text";
private static final String KEY_VIDEO_URL ="video_id_url";
private static final String KEY_VIDEO_THUMBNAIL ="video_thumb";



private static final String CREATE_TABLE_VIDEO = "CREATE TABLE "
+ TABLE_VIDEOS
+ " ( "
+ KEY_VIDEO_ID + " INTEGER PRIMARY KEY, "
+ KEY_VIDEO_NAME + " TEXT, "
+ KEY_VIDEO_URL + " TEXT, "
+ KEY_VIDEO_THUMBNAIL + " TEXT) ";
public VideoDataBase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_VIDEO);
Log.e("table name","==>"+CREATE_TABLE_VIDEO);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TextDataBase.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS "+TABLE_VIDEOS);
onCreate(db);
}

public void addVideos(VideoJson model){
SQLiteDatabase db = this.getWritableDatabase();
try{
ContentValues values = new ContentValues();
values.put(KEY_VIDEO_NAME, model.getName_video());
values.put(KEY_VIDEO_URL, model.getImageUrl_video());
values.put(KEY_VIDEO_THUMBNAIL, model.getVedio_thambnail());
db.insert(TABLE_VIDEOS, null, values);
Log.e("this db","value is"+values);
db.close();
}catch (Exception e){
Log.e("problem",e+"");
}
}

public List<VideoJson> getAllVideos() {
SQLiteDatabase db = this.getReadableDatabase();
List<VideoJson> videoList = null;
try{
videoList = new ArrayList<VideoJson>();
String QUERY = "SELECT * FROM "+TABLE_VIDEOS;
Cursor cursor = db.rawQuery(QUERY, null);
if(!cursor.isLast())
{
while (cursor.moveToNext())
{
VideoJson videolist = new VideoJson();
videolist.setVideo_id(cursor.getInt(0));
videolist.setName_video(cursor.getString(1));
videolist.setImageUrl_video(cursor.getString(2));
videolist.setVedio_thambnail(cursor.getString(3));

videoList.add(videolist);
}
}
db.close();
}catch (Exception e){
Log.e("error",e+"");
}
return videoList;
}

Answer

Try to delete the db Table before inserting.

private void parseData(JSONArray array) {

 db.deleteTABLE_VIDEOS();

for (int i = 0; i < array.length(); i++) {
    VideoJson model = new VideoJson();
    JSONObject json = null;
    try {
        json = array.getJSONObject(i);
        String vedio_name = json.getString(Config.TAG_VIDEO_TEXT);
        String vedio_url = json.getString(Config.TAG_VIDEO_URL);
        String vedio_img = json.getString(Config.TAG_VIDEO_IMAGE);
        model.setName_video(vedio_name);
        model.setImageUrl_video(vedio_url);
        model.setVedio_thambnail(vedio_img);
        Log.e("name","is =>"+vedio_name);
        Log.e("url","is =>"+vedio_url);
        Log.e("image","is =>"+vedio_img);
        db.addVideos(model);
        Log.e("database","value is"+db);
    } catch (JSONException e) {
        e.printStackTrace();
     }
Comments