Reputation: 1424
So I' currently making my way through syncing a SQLite Database to Firebase Database.
Got to a point wher I don't really understand whats happening in my code :P
Data is successfully added to SQLite and since I logged this whole process, I know it is only added once, also I set the Text to UNIQUE When I want to display it in a ListView, it displays every entry duplicated, like this:
So in my Log, it says "adding true to array" when "true" should be a specific name like Drawing and Painting, but displays Drawing and Painting anyway. Confusing! I even tried to handle this with exceptions that prevent the ArrayList from duplicate entrys...
Any Help is highly appreciated! :)
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);
Upvotes: 0
Views: 124
Reputation: 57103
Your issue is with the following lines of code:-
sublistData.add(data.getString(0));
Log.d(TAG, "ADDING " + sublistData.add(data.getString(0)) + " to ARRAY");
The first line adds an element with the data and is correct. However, the second line by repeating the first line adds a 2nd element to the array with the same value. So for each iteration of the loop 2 entries are added.
The fix is to not repeat using sublistData.add(data.getString(0))
. You should probably use Log.d(TAG, "ADDING " + data.getString(0) + " to ARRAY");
for the second line.
Upvotes: 1