ArnavvG
ArnavvG

Reputation: 3

Data repeating on restarting App in Android Database

I am facing this problem whenever i run the app 1st time data in database remain single time but when i close the App and restart again data goes twice(means two same row in table).Similarly for 3rd, 4th time and so on. How do i get rid of this problem? I even put datas.clear in DataList.java but don't whether i have add the datas.clear() line in correct place or not. PLz help if there is any other problem in my code.

MainActivity.java code

public class MainActivity extends AppCompatActivity {

Button listButton, addButton;
DatabaseHelper df;
private final static String TAG = "TestActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    df = new DatabaseHelper(this);

     addButton = (Button) findViewById(R.id.addbutton);
    uploadList();
}

public void uploadList(){
      DatabaseHelper df=new DatabaseHelper(this);
            df.open();
            try{
                InputStream im=getResources().getAssets().open("testdata.csv");
                BufferedReader br=new BufferedReader(new InputStreamReader(im));
                String data=br.readLine();
                while(data != null){
                    String t[]=data.split(",");
                    Product p=new Product();
                    p.setFirst(t[0]);
                    p.setSec(t[1]);
                    p.setThird(t[2]);
                    df.insert(p);
                    data=br.readLine();
                }
            }catch(Exception e){

            }

}
}

DatabaseHelper.java code

public class DatabaseHelper extends SQLiteOpenHelper{

private static final String FIRST="Name";
private static final String SECOND="Issn";
private static final String THIRD="ImpactFactor";

private static final String DATABASE="journal2016";
private static final String TABLENAME="journal";

private static final int VERSION=1;

SQLiteDatabase sd;

public void open(){
    sd=getWritableDatabase();
}
public void close(){
    sd.close();
}

public DatabaseHelper(Context context) {
    super(context, DATABASE, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLENAME );
    sqLiteDatabase.execSQL("CREATE TABLE " + TABLENAME + " ( NAME  TEXT, ISSN  TEXT, IMPACTFACTOR  REAL)");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLENAME );
}



public long insert(Product p){
    ContentValues cv=new ContentValues();
    cv.put(FIRST, p.getFirst());
    cv.put(SECOND, p.getSec());
    cv.put(THIRD, p.getThird());
    return sd.insertWithOnConflict(TABLENAME, null, cv,SQLiteDatabase.CONFLICT_REPLACE);

}

public List<Product> getAllProduct(){
    ArrayList<Product> list=new ArrayList<Product>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c=db.rawQuery("SELECT * FROM " + TABLENAME, null);
    while(c.moveToNext()){
        Product p=new Product();
        p.setFirst(c.getString(0));
        p.setSec(c.getString(1));
        p.setThird(c.getString(2));
        list.add(p);
    }
    db.close();
    return list;
}
}

DataList.java code

public class DataList extends Activity{

List<Product> datas = new ArrayList<Product>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);
    datas.clear();
    DatabaseHelper d=new DatabaseHelper(this);
    d.open();
    datas  = d.getAllProduct();
    ListView lv=(ListView)findViewById(R.id.listView1);
    lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, datas));
}

}

Product.java

public class Product {
private String first;
private String second;
private String third;
public String getFirst() {
    return first;
}
public void setFirst(String first) {
    this.first = first;
}
public String getSec() {
    return second;
}
public void setSec(String sec) {
    this.second = sec;
}
public String getThird() {
    return third;
}
public void setThird(String third) {
    this.third = third;
}

@Override
public String toString() {

    return first + second + third;
}

}

Upvotes: 0

Views: 67

Answers (1)

Saurabh Badola
Saurabh Badola

Reputation: 148

Remove this line from your onCreate() method:

df = new DatabaseHelper(this);

as no need of it because you are create object of your DatabaseHelper class inside uploadList() method. And also you are calling uploadList() method inside onCreate() thats why every time you launch the app, the onCreate() method executes and you uploadList() also execute. Try to put its calling statement in an onClickListener so it happens when you click a button or your choice of stuff.

Upvotes: 1

Related Questions