簡體   English   中英

使用原始查詢或在cursorAdapter中對數據進行排序

[英]Sort data with raw query or in cursorAdapter

在我的程序中,我使用CursorAdapter對顯示數據執行了原始查詢,但是我有一個字段,其值可能為0、1、2、3,...。但是有些例程該值存在多次,但我將顯示一次。 我該怎么做? 在游標Adapter中還是在查詢中?

我的查詢

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);
}

謝謝你的回答

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

在DESC或ASC之間選擇

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM