简体   繁体   English

如何从数据库填充微调器并进行ORDER BY

[英]How to fill a spinner from a database and ORDER BY

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 谢谢

You are using a HashSet which does not guarantee the order of insertion. 您使用的HashSet不能保证插入顺序。 Use a list or a sorted set... 使用列表或排序集...

HashSet不保持插入顺序,请尝试使用LinkedHashSet。

Collections.sort(list, new YourComparator));

            class YourComparator implements Comparator<String>{
                .
                .
                .

See this link } } 看到此链接 }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM