【发布时间】:2017-12-18 22:46:59
【问题描述】:
所以我目前正在将 SQLite 数据库同步到 Firebase 数据库。
到了我不太明白我的代码中发生了什么的地步:P
数据已成功添加到 SQLite,由于我记录了整个过程,我知道它只添加了一次,我也将 Text 设置为 UNIQUE 当我想在 ListView 中显示它时, 它显示每个重复的条目,如下所示:
所以在我的日志中,当“true”应该是一个特定的名称(如绘图和绘画)时,它会显示“将真实添加到数组”,但无论如何都会显示绘图和绘画。令人困惑! 我什至试图用防止 ArrayList 重复条目的异常来处理这个问题......
非常感谢任何帮助! :)
selectedName = getIntent().getStringExtra("name");
// get data and append to a list
Cursor data = databaseHelper.getFirstSubData(selectedName);
//----------------Snipped of DatabseHelper--------------//
public Cursor getFirstSubData(String mainCat){
// SELECT SubCats FROM first_sub_table WHERE 'mainCat' = main cat
SQLiteDatabase database = this.getWritableDatabase();
String query1 = "SELECT " + FIRST_SUB_COL3 + " FROM " + COURSES_FIRST_SUB_TABLE
+ " WHERE " + FIRST_SUB_COL2 + " = '" + mainCat + "'";
Log.d(TAG, "SELECT " + FIRST_SUB_COL3 + " FROM " + COURSES_FIRST_SUB_TABLE
+ " WHERE " + FIRST_SUB_COL2 + " = '" + mainCat + "'");
Cursor data = database.rawQuery(query1, null);
return data;
}
// ---------------------End of snippet------------------//
ArrayList<String> sublistData = new ArrayList<>();
Log.d(TAG, "Cursor is on Position: " + data.getPosition());
while (data.moveToNext()) {
Log.d(TAG, "Inside the while loop");
Log.d(TAG, "Cursor is on Position: " + data.getPosition());
// get the value from the database in column 0 then add it to the array list
sublistData.add(data.getString(0));
Log.d(TAG, "ADDING " + sublistData.add(data.getString(0)) + " to ARRAY");
}
String size = Integer.toString(sublistData.size());
Log.d(TAG, "SIZE OF ARRAY IN SUB CAT IS " + size);
final ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, sublistData);
lvSubItems.setAdapter(adapter);
【问题讨论】:
-
尝试从
Log.d...行中删除sublistData.add(data.getString(0)),因为这将添加第二个因此重复的元素,而不是获取第一个元素的值。 -
天啊!非常感谢您的帮助!...如果您复制和粘贴内容并工作 12 小时,就会发生这种情况。
标签: android sqlite listview android-studio android-sqlite