I am working on an app, for which I have implemented a formula. I implemented it using a separate class called MyViewModel (not to be confused with android's viewmodel please). This class has a method which has the formula. Now, that formula receives the data from a DBHelper (sqlite database), in my TaskSummary class. However, the listview isnt visible in the activity at all. What might be the issue here? Please guide me.
Code for DBHelper Class :
package com.example.taskmasterv3;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "Subtaskdata.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create Table Subtaskdata(subtaskname TEXT primary key, prihigh INTEGER, primed INTEGER, prilow INTEGER, timemore INTEGER, timemed INTEGER, timeless INTEGER, totalTime INTEGER, time INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop Table if exists Userdetails");
}
public Boolean insertSubtaskData(String subtaskName, int prihigh, int primed, int prilow, int timemore, int timemed, int timeless, int totalTime, int time){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("subtaskname", subtaskName);
contentValues.put("priHigh", prihigh);
contentValues.put("priMed", primed);
contentValues.put("priLow", prilow);
contentValues.put("timeMore", timemore);
contentValues.put("timeMed", timemed);
contentValues.put("timeLess", timeless);
contentValues.put("totalTime", totalTime);
contentValues.put("time", time);
long result = db.insert("Subtaskdata", null, contentValues);
if (result < 0){
return false;
}
else
{
return true;
} }
public Boolean updateSubtaskData(String subtaskName, int prihigh, int primed, int prilow, int timemore, int timemed, int timeless,int totalTime, int time){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("subtaskname", subtaskName);
contentValues.put("prihigh", prihigh);
contentValues.put("primed", primed);
contentValues.put("prilow", prilow);
contentValues.put("timemore", timemore);
contentValues.put("timemed", timemed);
contentValues.put("timeless", timeless);
contentValues.put("totalTime", totalTime);
contentValues.put("time", time);
Cursor cursor = db.rawQuery("Select * from Subtaskdata where subtaskname = ?", new String[]{subtaskName});
if (cursor.getCount()> 0){
long result = db.update("Subtaskdata", contentValues, "name=?", new String[]{subtaskName});
if (result== -1){
return false;
}
else
{
return true;
} }
else
{
return false;
}}
public Boolean deletedata(String subtaskName){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("Select * from Subtaskdata where subtaskname = ?", new String[]{subtaskName});
if (cursor.getCount()> 0){
long result = db.delete("Subtaskdata","subtaskname=?", new String[]{subtaskName});
if (result == -1){
return false;
}
else
{
return true;
} }
else
{
return false;
}}
public Cursor getdata (){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("Select * from Subtaskdata", null);
return cursor;
}
public void deleteAll(){
SQLiteDatabase db = getWritableDatabase();
db.delete("Subtaskdata", null, null);
db.execSQL("delete from "+ "Subtaskdata");
db.close();
}
}
Code for MyViewModel Class (containing formula) :
package com.example.taskmasterv3;
import android.content.Context;
import android.database.Cursor;
import androidx.lifecycle.ViewModel;
import java.util.ArrayList;
public class MyViewModel {
int timeInt;
int totalTime;
int timeConstant;
DBHelper dbHelper;
public int timeIntFinder(ArrayList<subtask> list,Context context) {
dbHelper = new DBHelper(context);
Cursor cursor = dbHelper.getdata();
for(int i = 0; i < list.size(); i++){
timeInt += list.get(i).getTime();
}
while (cursor.moveToNext()){
totalTime = list.get(0).getTotaltime();
timeConstant = totalTime / timeInt;
}
return timeConstant;
}
}
Code for TaskSummary Class :
package com.example.taskmasterv3;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class TaskSummary extends AppCompatActivity {
ListView lvTaskList;
TextView tvBreak, tvBreakAfterEvery, txt1, txt2, text1, hmm;
TextView break_duration_mins;
ArrayList<SubtaskPartTwo> subtaskList = new ArrayList<>();
ArrayList<subtask> listForTime = new ArrayList<>();
//String subtname;
//String pri;
//String time;
int working_hours;
int working_minutes;
int totalTime;
DBHelper dbHelper;
int timeInts;
int timeConstant;
int answer;
int timetmetc;
boolean looponeran = false;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.tasksummary, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
dbHelper.deleteAll();
if (item.getItemId() == R.id.restore) {
Intent inti = new Intent(this, TaskInfo.class);
startActivity(inti);
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_summary);
timetmetc = getIntent().getIntExtra("time", 1);
MyViewModel myViewModel = new MyViewModel();
lvTaskList = findViewById(R.id.lvTaskList);
tvBreak = findViewById(R.id.tvBreak);
tvBreakAfterEvery = findViewById(R.id.tvBreakAfterEvery);
txt1 = findViewById(R.id.txt1);
txt2 = findViewById(R.id.txt2);
break_duration_mins = findViewById(R.id.break_duration_mins);
text1 = findViewById(R.id.text1);
hmm = findViewById(R.id.hmm);
dbHelper = new DBHelper(this);
working_hours = getIntent().getIntExtra("working_hours", 1);
working_minutes = getIntent().getIntExtra("working_minutes", 0);
timeConstant = getIntent().getIntExtra("timeconstant", 1);
int alol = working_hours * 60;
int blol = working_minutes;
totalTime = alol + blol;
double without_break_hours = getIntent().getIntExtra("without_break_hours", 1);
double without_break_minutes = getIntent().getIntExtra("without_break_minutes", 0);
double break_duration = getIntent().getIntExtra("break_duration", 20);
String a = working_hours + " h";
txt1.setText(a);
String b = working_minutes + " m";
break_duration_mins.setText(b);
String c = break_duration + " m";
txt2.setText(c);
//Mathematics
double g = working_hours * 100;
double h = g + working_minutes;
double i = h + break_duration;
double j = i / 60;
double p = (int) j;
double q = j - p;
double r = q * 60;
without_break_hours = p;
without_break_minutes = r;
String d = without_break_hours + " h";
String e = without_break_minutes + " m";
text1.setText(d);
hmm.setText(e);
Cursor res = dbHelper.getdata();
while (res.moveToNext()){
looponeran = true;
subtask subtask = new subtask("not req", false, false, false, false, false, false, timetmetc, totalTime);
listForTime.add(subtask);
}
MyViewModel myViewModel1 = new MyViewModel();
myViewModel1.timeIntFinder(listForTime, this);
// possible error : both the while loops run together causing null data being passed to database hence it isnt showing.
// find a way to make the above while loop run first, and only after that the 2nd one should run or something..
SubtaskDetailAdapter adapter = new SubtaskDetailAdapter(this, subtaskList);
lvTaskList.setAdapter(adapter);
while (res.moveToNext() && looponeran){
looponeran = false;
SubtaskPartTwo subtaskPartTwo = new SubtaskPartTwo(res.getString(0), res.getInt(1), res.getInt(2), res.getInt(3), res.getInt(4), res.getInt(5), res.getInt(6), 0, res.getInt(8), totalTime, timeConstant);
//subtaskList.add(subtaskPartTwo);
adapter.addANewSubTask(subtaskPartTwo);
}
}
}