0

I'm trying to fill a spinner from my database, but when I get the data does not want them sorted in order by year even indicating this in the query.

This is how I get the data

public Set<String> getAllData(int getData,String[] args, String qry) {
    Set<String> set = new HashSet<String>();

    String selectQuery = qry ;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery , args);

    if (cursor.moveToFirst()) {
        do {
            set.add(cursor.getString(getData));
        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    return set;
}

and this is the query

        String myQry = "SELECT * FROM Car ORDER BY year";

and loading the spinner

private void loadSpinner(Spinner mySpinner, int getData, String[] args, String qry) {

    // here i used Set Because Set doesn't allow duplicates.
    Set<String> set = db.getAllData(getData, args, qry);
    List<String> list = new ArrayList<String>(set);

    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    mySpinner.setAdapter(adapter);
    mySpinner.setWillNotDraw(false);
}

Thanks

ibussa
  • 18
  • 3

3 Answers3

0

You are using a HashSet which does not guarantee the order of insertion. Use a list or a sorted set...

benjamin.d
  • 2,801
  • 3
  • 23
  • 35
0

HashSet doesn't keep insertion order, try a LinkedHashSet.

Samuel
  • 2,106
  • 1
  • 17
  • 27
0
Collections.sort(list, new YourComparator));

            class YourComparator implements Comparator<String>{
                .
                .
                .

See this link } }

Community
  • 1
  • 1
Wajdi Hh
  • 785
  • 3
  • 9