gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:00:10 25 4
gpt4 key购买 nike

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

感谢您的回答

最佳答案

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

选择 DESC 或 ASC

关于java - 使用原始查询或在cursorAdapter 中对数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36878319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com