Search code examples
androidlistviewandroid-sqlite

ListView doesn't show the list of items it only shows the package what to do? -Android


I don't know what to do anymore please help, the listview doesn't get the items inside the database it just shows the package something.

//from the database

   public ArrayList<Question> getAllQuestions(){
    ArrayList<Question> questionList = new ArrayList<>();
    db = getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM " + QuestionsTable.TABLE_NAME, null);

    if (c.moveToFirst()){
        do{
            Question question = new Question();
            question.setId(c.getInt(c.getColumnIndex(QuestionsTable._ID)));
            question.setQuestion(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_QUESTION)));
            question.setOption1(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION1)));
            question.setOption2(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION2)));
            question.setOption3(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION3)));
            question.setOption4(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION4)));
            question.setAnswer(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_ANSWER)));
            question.setCategoryID(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_CATEGORY_ID)));
            questionList.add(question);
        } while (c.moveToNext());
    }
    c.close();
    return questionList;
}

//from the view question class

   private void questionList(){
    QuizDbHelper dbHelper = QuizDbHelper.getInstance(this);
    final ArrayList<Question> questionList = dbHelper.getAllQuestions();
    ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,questionList);
    lvQuestions.setAdapter(adapter);
}

and it shows this

It doesn't show what's inside


Solution

  • You are using the default ArrayAdapter which can only show strings. What you are seeing is the result to calling toString() method of the Question class.

    In this case you have to create your own adapter and implement the logic of rendering each row with your data.