Java 如何在使用ListView时删除SQLite中的重复项
当使用ListView从SQLite获取数据时,我一直有重复的项,我用来自两个不同表中两个不同列的数据填充两个TextView。当我使用每个列和表中的单个数据时,它们按计划工作,但同时存在重复项 从database.javaJava 如何在使用ListView时删除SQLite中的重复项,java,android,sqlite,android-studio,Java,Android,Sqlite,Android Studio,当使用ListView从SQLite获取数据时,我一直有重复的项,我用来自两个不同表中两个不同列的数据填充两个TextView。当我使用每个列和表中的单个数据时,它们按计划工作,但同时存在重复项 从database.java public Cursor getQuestionAndAnswer(){ SQLiteDatabase sqLiteDatabase = this.getReadableDatabase(); return sqLiteDatabase.r
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);
}
}
将适配器绑定到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]
我建议您对数据库记录使用
ID
,并根据ID获取它的unique
,但是,请记住,您不能多次添加相同的数据,在这种情况下,如果数据存在于DB
中,您可以对其进行比较,然后再不添加它。如果看不到数据库表模式,这或多或少只是sudo代码,但我认为您可能只需要修改SQL语句
// give me ALL columns, from both tables
select * from Question_Table, Answer_Table
我觉得你好像在尝试匹配问题的答案
// give me all columns from this table matched up with this ID from that table.
SELECT * FROM question_table JOIN answer_table ON question_table.id = answer_table.questionID
为了快速复习SQL,Khan Acadamy提供了一个很好的交互式学习途径:
请您详细解释“为数据库记录使用ID并使其唯一”的含义。请检查此项