1

I searched everywhere but i didnt found any info or tutorial or blogs regarding sqlite with MVVM. how do i create viewmodel for my sqlite methods? i have tried various ways to access it, but gives me error

following is my code for sqlite helper class

public class DbHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;

static final String DATABASE_NAME = "kuncorosqlite.db";

public static final String TABLE_SQLite = "sqlite";

public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_ADDRESS = "address";

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " + TABLE_SQLite + " (" +
            COLUMN_ID + " INTEGER PRIMARY KEY autoincrement, " +
            COLUMN_NAME + " TEXT NOT NULL, " +
            COLUMN_ADDRESS + " TEXT NOT NULL" +
            " )";

    db.execSQL(SQL_CREATE_MOVIE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SQLite);
    onCreate(db);
}

public ArrayList<HashMap<String, String>> getAllData() {
    ArrayList<HashMap<String, String>> wordList;
    wordList = new ArrayList<HashMap<String, String>>();
    String selectQuery = "SELECT * FROM " + TABLE_SQLite;
    SQLiteDatabase database = this.getWritableDatabase();
    Cursor cursor = database.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put(COLUMN_ID, cursor.getString(0));
            map.put(COLUMN_NAME, cursor.getString(1));
            map.put(COLUMN_ADDRESS, cursor.getString(2));
            wordList.add(map);
        } while (cursor.moveToNext());
    }

    Log.e("select sqlite ", "" + wordList);

    database.close();
    return wordList;
}

public void insert(String name, String address) {
    SQLiteDatabase database = this.getWritableDatabase();
    String queryValues = "INSERT INTO " + TABLE_SQLite + " (name, address) " +
            "VALUES ('" + name + "', '" + address + "')";

    Log.e("insert sqlite ", "" + queryValues);
    database.execSQL(queryValues);
    database.close();
}

public void update(int id, String name, String address) {
    SQLiteDatabase database = this.getWritableDatabase();

    String updateQuery = "UPDATE " + TABLE_SQLite + " SET "
            + COLUMN_NAME + "='" + name + "', "
            + COLUMN_ADDRESS + "='" + address + "'"
            + " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
    Log.e("update sqlite ", updateQuery);
    database.execSQL(updateQuery);
    database.close();
}

public void delete(int id) {
    SQLiteDatabase database = this.getWritableDatabase();

    String updateQuery = "DELETE FROM " + TABLE_SQLite + " WHERE " + COLUMN_ID + "=" + "'" + id + "'";
    Log.e("update sqlite ", updateQuery);
    database.execSQL(updateQuery);
    database.close();
}

} viewmodel so far till now what ihave tried :-

public class Viewmodell extends AndroidViewModel {

private DbHelper repository ;
 MutableLiveData<ArrayList<HashMap<String, String>>> allNotesLivedata;
public Viewmodell(@NonNull Application application) {
    super(application);
    repository = new DbHelper(application);
    allNotesLivedata = repository.getAllData();
}
void insert(String name,String Address) {
    repository.insert(name,Address);
}
void update(int id, String name, String address) {
    repository.update(id,name,address);
}
void delete(int id) {
    repository.delete(id);
}
public LiveData<ArrayList<HashMap<String, String>>> getAllNotes() {
    return allNotesLivedata;
}

}

please dont suggest me to use room because it is not my requirement

IRON MAN
  • 191
  • 3
  • 18

1 Answers1

1

The error just means that you are not submitting the compatible values :

In your code , when creating MutableLiveData , you have assigned it

ArrayList<HashMap<String,String>>

but when setting value to the MutableLiveData you are just passing a HashMap . So what you need to do is instead of passing HashMap directly ,you need to create an ArrayList and then to that arrayList you need to pass your HashMap and then set the ArrayList to the MutableLiveData .

You can do it somewhat like this .Below code is for illustration purpose

//Create an arrayList
ArrayList<HashMap> listofMaps = new ArrayList();
//then add all your hashmaps to your list
listofMaps.add(map);
//and then set it to the MutableLiveData
wordList.postValue(listOfMaps);

Or instead of the above process , the second way to solve this issue is , you just need to create the MutableLiveData of the Type : HashMap<String,String>

wordList = new MutableLiveData<HashMap<String,String>>();
Karunesh Palekar
  • 2,190
  • 1
  • 9
  • 18
  • karunesh i have added latest ss please have a look – IRON MAN Aug 27 '21 at 08:32
  • but im getting one issue while fetching list can you help? can i update code? – IRON MAN Aug 27 '21 at 08:48
  • You need to create another question for the same . Updating the code and taking it to another context would just damage the purpose of what you have asked and no one else might get benefit of it . Remember we all are here to help each other as well – Karunesh Palekar Aug 27 '21 at 08:48
  • Just ping the next question link in the comment section for me to know and just restore this post with those ss that you had posted earlier – Karunesh Palekar Aug 27 '21 at 08:51
  • hi can u help me out here -> https://stackoverflow.com/questions/76480901/facing-issue-while-making-dynamic-views-using-relative-layout – IRON MAN Jun 15 '23 at 11:20