【发布时间】:2020-04-26 16:26:15
【问题描述】:
在使用 ListView 从 SQLite 获取数据时,我一直有重复的项目,我正在使用来自两个不同表中两个不同列的数据填充两个 TextView。当我使用每个列和表中的单个数据时,它们按计划工作,但在一起有重复的项目。
来自database.java
public Cursor getQuestionAndAnswer(){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
return sqLiteDatabase.rawQuery("select * from Question_Table, Answer_Table" ,null);
}
来自光标适配器
public class QuestionAndAnswerAdapter extends CursorAdapter {
public QuestionAndAnswerAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.q_a_layout,parent,false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView Question = view.findViewById(R.id.text_question);
String listQuestion = cursor.getString(cursor.getColumnIndexOrThrow("QUESTIONS"));
Question.setText(listQuestion);
TextView Answer = view.findViewById(R.id.text_answer);
String listAnswer = cursor.getString(cursor.getColumnIndexOrThrow("ANSWERS"));
Answer.setText(listAnswer);
}
}
当将 Adapter 绑定到 ListView 时
questionCursor = myDataBaseClass.getQuestionAndAnswer();
if (questionCursor != null) {
QuestionAndAnswerAdapter questionAndAnswerAdapter = new QuestionAndAnswerAdapter(this, questionCursor);
QuestionList.setAdapter(questionAndAnswerAdapter);
}
[![The first text is duplicated, while the second text displays correctly][1]][1]
【问题讨论】:
标签: java android sqlite android-studio