0

I'm attempting to get the data that is saved in my the SQLite database into a custom ListView in my app. It seems the adaptor is working okay because it inflating the layout, however, there is no data in the views. I'm hoping it is a simple fix and I appreciate any input on this :)

Activity where data will be shown:

public class SavedGameScreen extends AppCompatActivity {

    ListView lv1;
    ArrayList<GameStats> arrayList;
    MyAdaptor myAdaptor;
    DatabaseHelper databaseHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saved_game_screen);
        lv1 = findViewById(R.id.lv1);
        databaseHelper = new DatabaseHelper(this);
        arrayList = new ArrayList<>();
        loadData();
    }

    private void loadData() {
        arrayList = databaseHelper.getAllData();
        myAdaptor = new MyAdaptor(this, arrayList);
        lv1.setAdapter(myAdaptor);
        myAdaptor.notifyDataSetChanged();
    }
}

Database Helper:


    public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "savedGames.db";
    public static final String TABLE_NAME = "savedGamesTable";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "lName";
    public static final String COL_3 = "lScore";
    public static final String COL_4 = "rName";
    public static final String COL_5 = "rScore";
    public static final String COL_6 = "notes";
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("create table "+TABLE_NAME+" (ID INTEGER PRIMARY KEY AUTOINCREMENT, lName TEXT," +
            "lScore INTEGER, rName TEXT, rScore INTEGER, notes TEXT)");
    }

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

    public boolean insertData(String lName, int lScore, String rName, int rScore, String notes) {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, lName);
        contentValues.put(COL_3, lScore);
        contentValues.put(COL_4, rName);
        contentValues.put(COL_5, rScore);
        contentValues.put(COL_6, notes);
       long result = sqLiteDatabase.insert(TABLE_NAME, null, contentValues);
        if(result == -1) {
            return false;
        }
        else{
            return true;
        }

    }

    public ArrayList<GameStats> getAllData() {
        ArrayList<GameStats> arrayList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery("SELECT * FROM savedGamesTable", null);

        while(cursor.moveToNext()){
            int id = cursor.getInt(0);
            String lName = cursor.getString(1);
            int lScore = cursor.getInt(2);
            String rName = cursor.getString(3);
            int rScore = cursor.getInt(4);
            String notes = cursor.getString(5);

            GameStats gameStats = new GameStats(id, lName, lScore, rName, rScore, notes);
            arrayList.add(gameStats);

        }
        return arrayList;
    }


}

myAdaptor:


    public class MyAdaptor extends BaseAdapter {

    Context context;
    ArrayList<GameStats> arrayList;
    public MyAdaptor(Context context, ArrayList<GameStats> arrayList){
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return this.arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.activity_saved_games, null);
            TextView lName = convertView.findViewById(R.id.lName);
            TextView lScore = convertView.findViewById(R.id.lScore);
            TextView rName = convertView.findViewById(R.id.rName);
            TextView rScore = convertView.findViewById(R.id.rScore);
            TextView notes = convertView.findViewById(R.id.notes);

        GameStats gameStats = arrayList.get(position);

        lName.setText(gameStats.getlName());
        lScore.setText(String.valueOf(gameStats.getlScore()));
        rName.setText(gameStats.getrName());
        rScore.setText(String.valueOf(gameStats.getrScore()));
        notes.setText(gameStats.getNotes());
        return convertView;
    }
}

Below is what I am getting when I try to view the data enter image description here

Model class:

public class GameStats {
int id, lScore, rScore;
String lName, rName, notes;

public GameStats(int id, String lName, int lScore, String rName, int rScore, String notes) {

}


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getlScore() {
    return lScore;
}

public void setlScore(int lScore) {
    this.lScore = lScore;
}

public int getrScore() {
    return rScore;
}

public void setrScore(int rScore) {
    this.rScore = rScore;
}

public String getlName() {
    return lName;
}

public void setlName(String lName) {
    this.lName = lName;
}

public String getrName() {
    return rName;
}

public void setrName(String rName) {
    this.rName = rName;
}

public String getNotes() {
    return notes;
}

public void setNotes(String notes) {
    this.notes = notes;
}

}

Dom.F
  • 17
  • 7
  • Did you check data object value available in the adapter class? – Thirumalai May 28 '20 at 17:46
  • @Thirumalai how do you mean? – Dom.F May 28 '20 at 17:52
  • This object value GameStats gameStats = arrayList.get(position); – Thirumalai May 28 '20 at 18:02
  • @Thirumalai how do I check this? – Dom.F May 28 '20 at 18:23
  • just check with debugger or put log there. – Thirumalai May 28 '20 at 18:34
  • @Dom.F check the object value as suggested by @Thirumalai but I think using ListView like this is a bad pattern. It will starts to freeze if the the list goes long. Try using `ViewHolder` pattern or switch to `RecyclerView` .....See https://stackoverflow.com/questions/21501316/what-is-the-benefit-of-viewholder-pattern-in-android and https://stackoverflow.com/questions/26728651/recyclerview-vs-listview – cgb_pandey May 29 '20 at 02:47
  • Thanks for the feedback guys. I will consider using those views. However I have checked the get methods and they are all returning null .. I can see that being an issue to as why no data is being imported.. (I have added the class to the post) – Dom.F May 30 '20 at 10:47
  • @cgb_pandey I have tested if the object is available and it seems to be. However, I have done some more testing and it seems the arraylist is also populated. I have no idea what is going on. Thanks in advance – Dom.F May 30 '20 at 17:36
  • (I have just tested the getlName on the Adaptor and it is coming back null) – Dom.F May 30 '20 at 17:45

0 Answers0