Question

i have a listview in my project that links to database and shows its context from database but my problem is whenever my application runs it records goes twice (ex.2records first run, 4records with same context,...) and i do not know that wat is problem

this is my database class:

  new File(DIR_DATABASE).mkdirs();
dataBase = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/information.sqlite", null);
dataBase.execSQL("CREATE  TABLE  IF NOT EXISTS information (" +
"information_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE ," +
"information_title TEXT)");
dataBase.execSQL("INSERT INTO information  (information_title)  VALUES ('قسمت اول')");
dataBase.execSQL("INSERT INTO information  (information_title) VALUES ('قسمت دوم')");

and its my main class that shows listview:

 ListView lstC findViewById(R.id.lstContent);
    adapter = new AdapterNote(title);
    lstContent.setAdapter(adapter);
    readFromDataBase();
    adapter.notifyDataSetChanged();
}

private void readFromDataBase() {
    Cursor cursor = G.dataBase.rawQuery("SELECT * FROM information", null);
    while (cursor.moveToNext()) {
        StructNote detail = new StructNote();
        detail.title = cursor.getString(cursor.getColumnIndex("information_title"));
        title.add(detail);
    }
    cursor.close();
    adapter.notifyDataSetChanged();
}
Was it helpful?

Solution

You need to understand SQLiteOpenHelper for this. This is workflow issue, so it would be better you should go through some tutorial. This is a nice tutorial where you can learn the concept.

In very short i am listing few points that may be useful for you:

  1. In your application you create a subclass of the SQLiteOpenHelper class.SQLiteOpenHelper is a helper class to manage database creation and version management. In subclass override, onCreate() and onUpgrade().

    public class MySQLiteHelper extends SQLiteOpenHelper {
       public void onCreate(SQLiteDatabase database) {
         // create database command.
       }
    
       @Override
       public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         // upgrade database command here.
       }
    

    }

  2. Create a DAO class that will manage the interaction with the database. Your CRUD methods will go here. See DAO pattern for details. This class will centralize the access to database, hence your activity, fragment will interact with this class to perform operation. Direct access to database won't be allowed.

    public class ModelDataSource {
         // needed to perform operation on database
         private SQLiteDatabase database;
        //needed to retrieve database object
         private MySQLiteHelper dbHelper;
    
         public boolean insertModel(Model model) {
        // perform insert operation on database
         }    
     }
    
  3. In your activity, you can interact with DAO to perform some action.

    public class YourActivity extends Activity {
     private ModelDataSource datasource;
    
     @Override
      public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
    
       datasource = new ModelDataSource(this);
       datasource.open();
    
       boolean insertion_status = datasource.insert(modelobject);
      }
    }
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top