使用原始查询或在cursorAdapter中对数据进行排序

时间:2016-04-27 00:44:03

标签: java android sqlite

在我的程序中,我使用CursorAdapter对显示数据执行原始查询,但是我有一个字段可能有值0,1,2,3,....但是某些值存在多次,但我会显示一次。我怎么能这样做?在游标适配器或查询?

我的查询

public Cursor seeGame(){
    open();

    Cursor todoCursor = bdd.rawQuery("SELECT  * FROM "+TABLE_GAME, null);
    return todoCursor;
}

我的CursorAdapter

public class cursorForListGame extends CursorAdapter {
private Context c;
public cursorForListGame(Context context, Cursor cursor,int flag) {

    super(context, cursor,0);
    c = context;
}

// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_game, parent, false);
}

// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {

        // Find fields to populate in inflated template
        TextView tvidgame = (TextView) view.findViewById(R.id.idgame);
        TextView tvnbtour = (TextView) view.findViewById(R.id.nbtour);
        TextView tvj1 = (TextView) view.findViewById(R.id.j1);
        TextView tvj2 = (TextView) view.findViewById(R.id.j2);
        TextView tvj3 = (TextView) view.findViewById(R.id.j3);
        TextView tvj4 = (TextView) view.findViewById(R.id.j4);
        TextView tvj5 = (TextView) view.findViewById(R.id.j5);

        //Au cas ou il n'y est pas de joueur 4 et 5
        tvj4.setText("");
        tvj5.setText("");

        // Extract properties from cursor
        int idgame = cursor.getInt(cursor.getColumnIndexOrThrow("_id_game"));
        int nbtour = cursor.getInt(cursor.getColumnIndexOrThrow("Numéro_du_tour"));
        int nb_joueurs = cursor.getInt(cursor.getColumnIndexOrThrow("Nb_joueurs"));

        String j1_id = cursor.getString(cursor.getColumnIndexOrThrow("j1"));
        Joueur j1 = Data.bdd.get_joueur_by_id(Integer.parseInt(j1_id));

        String j2_id = cursor.getString(cursor.getColumnIndexOrThrow("j2"));
        Joueur j2 = Data.bdd.get_joueur_by_id(Integer.parseInt(j2_id));

        String j3_id = cursor.getString(cursor.getColumnIndexOrThrow("j3"));
        Joueur j3 = Data.bdd.get_joueur_by_id(Integer.parseInt(j3_id));

        if (nb_joueurs >= 4) {
            String j4_id = cursor.getString(cursor.getColumnIndexOrThrow("j4"));
            Joueur j4 = Data.bdd.get_joueur_by_id(Integer.parseInt(j4_id));
            tvj4.setText(String.valueOf(j4.getNom()));
        }
        if (nb_joueurs == 5) {
            String j5_id = cursor.getString(cursor.getColumnIndexOrThrow("j5"));
            Joueur j5 = Data.bdd.get_joueur_by_id(Integer.parseInt(j5_id));
            tvj5.setText(String.valueOf(j5.getNom()));
        }
        // Populate fields with extracted properties
        tvidgame.setText(String.valueOf(idgame));
        tvnbtour.setText(String.valueOf(nbtour));
        tvj1.setText(String.valueOf(j1.getNom()));
        tvj2.setText(String.valueOf(j2.getNom()));
        tvj3.setText(String.valueOf(j3.getNom()));
}

我的主要功能

 private void display_games() {

   Cursor todoCursor = Data.bdd.seeGame();
    // Find ListView to populate
    ListView lvItems = (ListView) findViewById(R.id.listView2);
    // Setup cursor adapter using cursor from last step
    cursorForListGame todoAdapter = new cursorForListGame(this, todoCursor,0);
    // Attach cursor adapter to the ListView
    lvItems.setAdapter(todoAdapter);
}

感谢您的回答

1 个答案:

答案 0 :(得分:1)

Cursor todoCursor = bdd.rawQuery("SELECT  * FROM "+TABLE_GAME+" ORDER BY your_field DESC|ASC, null);

在DESC或ASC之间选择