【问题标题】:Sort data with raw query or in cursorAdapter使用原始查询或在 cursorAdapter 中对数据进行排序
【发布时间】:2016-04-27 09:31:39
【问题描述】:

在我的程序中,我使用 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);
}

感谢您的回答

【问题讨论】:

  • 在查询中。你会在这里找到一个解决方案 [select distinct value in android sqlite][1] [1]: stackoverflow.com/questions/11219361/…
  • @GeraldoFreitas 是的,但是当我执行“SELECT * FROM TABLE”时如何使用?
  • 这样你就不能用一个独特的查询来解决它。但是为什么你需要一个 Select * 来代替选择单独的字段呢?
  • @GeraldoFreitas,是的,我很愚蠢,你说得对!谢谢

标签: java android sqlite


【解决方案1】:
Cursor todoCursor = bdd.rawQuery("SELECT  * FROM "+TABLE_GAME+" ORDER BY your_field DESC|ASC, null);

在 DESC 或 ASC 之间选择

【讨论】:

  • 所以你的朋友就像@Geraldo Freitas 说link
猜你喜欢
  • 1970-01-01
  • 2018-05-03
  • 2011-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多