Wukingkong Sun
Wukingkong Sun

Reputation: 17

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

Upvotes: 0

Views: 49

Answers (2)

Bracadabra
Bracadabra

Reputation: 3659

You can override toString of Question class or subclass ArrayAdapter and override getView functions to set necessary text, something like this:

@Override
public @NonNull View getView(int position, @Nullable View convertView,
            @NonNull ViewGroup parent) {
    final TextView view = (TextView)createViewFromResource(mInflater, position, convertView, parent, mResource);
    view.setText(questions.get(position).getQuestion());

    return view;
}

Upvotes: 0

Ezio
Ezio

Reputation: 2985

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.

Upvotes: 1

Related Questions