I'm trying to fetch data from sqlite to set them into a spinner. The user creates rows of travel dates and set them into the database (COLUMN_DATE_DEPART). In the first spinner I'd like to get the list of the years existing in the COLUMN_DATE_DEPART. In the second one, I'd like to get the list of months of the year selected in the first spinner.
The problem is as followed: In the first spinner i get the years as expected. In the second spinner i get the months but in double so that if the user created two travels on the 1st and the 31st of January, I'll have the month January twice rather than once.
My question is: What did i miss ?
Find attached my code.
Fragment Activity Java
List<String> an = helper.ListeAnneeTrajet(et_iduser);
final ArrayAdapter<String> adapterA= new ArrayAdapter<>(this.getActivity(), R.layout.spinner_item, an);
adapterA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
annee.setAdapter(adapterA);
annee.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
List<String> mo = helper.ListeMoisTrajet(et_iduser, annee.getSelectedItem().toString().trim());
ArrayAdapter<String> adapterB = new ArrayAdapter<>(getActivity(), R.layout.spinner_item, mo);
adapterA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mois.setAdapter(adapterB);
mois.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
DatabaseHelper
public List<String> ListeAnneeTrajet(String et_iduser) {
List<String> an = new ArrayList<>();
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME3 + " WHERE (" + COLUMN_ID_CHAUFFEUR + " = '" + et_iduser + "' AND " + COLUMN_DATE_DEPART + " >= DATE('NOW')) GROUP BY SUBSTR(" + COLUMN_DATE_DEPART + ",1,4) ORDER BY " + COLUMN_DATE_DEPART + " ASC";
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
an.add(cursor.getString(cursor.getColumnIndex(COLUMN_DATE_DEPART)).substring(0,4));
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return an;
}
public List<String> ListeMoisTrajet(String et_iduser, String et_annee) {
List<String> mo = new ArrayList<>();
String selectQuery = "SELECT DISTINCT * FROM " + TABLE_NAME3 + " WHERE (" + COLUMN_ID_CHAUFFEUR + " = '" + et_iduser + "' AND " + COLUMN_DATE_DEPART + " >= DATE('NOW') AND SUBSTR(" + COLUMN_DATE_DEPART + ",1,4) = '" + et_annee + "') GROUP BY SUBSTR(" + COLUMN_DATE_DEPART + ",6,7) ORDER BY " + COLUMN_DATE_DEPART + " ASC";
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
mo.add(cursor.getString(cursor.getColumnIndex(COLUMN_DATE_DEPART)).substring(5,7));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return mo;
}