gpt4 book ai didi

java - 制作一个显示前 10 名玩家的排行榜?

转载 作者:行者123 更新时间:2023-12-02 05:06:30 25 4
gpt4 key购买 nike

我创建的排行榜有问题,该排行榜是为了显示得分最高的前 10 名玩家,但它以相反的顺序首先显示最低的玩家,我尝试更改选择语句和版本数据库并且不起作用有人知道如何解决这个问题吗?这是排行榜 Activity :

package com.example.tabsnav;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import library.DBhelper;
import library.DBhelper.DatabaseHelper;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class LeaderBoard extends Activity {
ArrayList<Leaders> imageArry = new ArrayList<Leaders>();
ImageAdapter adapter;
EditText etcomment1;
//Button b1;
DBhelper db;
TextView etname1;
ListView dataList;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leader_board);




}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
adapter.clear();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
db = new DBhelper(this);
db.open();


// Reading all comments from database

List<Leaders> contacts = db.getAllContactsFromAndroid();

for (Leaders cn : contacts) {
String log = "ID:" + cn.getID() + " Name: " + cn.getName()
+ " ,Image: " + cn.getImage();

// Writing Contacts to log
Log.d("Result: ", log);
//add contacts data in arrayList
imageArry.add(cn);

}
adapter = new ImageAdapter(this, R.layout.leader_list_adapter,
imageArry);
dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
}}

我在数据库中的获取函数:

// Getting All Contacts From Android
public List<Leaders> getAllContactsFromAndroid() {
List<Leaders> contactList = new ArrayList<Leaders>();
// Select All Query
Cursor cursor=null;
cursor = mDb.rawQuery("SELECT * FROM Game DESC ORDER by score LIMIT 10",
null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Leaders contact = new Leaders();
contact.setName(cursor.getString(cursor.getColumnIndex(G_FIRSTNAME)));
contact.setImage(cursor.getBlob(cursor.getColumnIndex(G_PHOTO)));
contact.setComment(cursor.getInt(cursor.getColumnIndex(G_SCORE)));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}

用于设置和获取数据的领导者类:

package com.example.tabsnav;

public class Leaders {

// private variables
int _id;
String _name;
byte[] _image;
int _comment;

// Empty constructor
public Leaders() {

}
// constructor
public Leaders(int keyId, String name, byte[] image,int comment) {
this._id = keyId;
this._name = name;
this._image = image;
this._comment=comment;

}

// constructor
public Leaders(int keyId, String name, byte[] image) {
this._id = keyId;
this._name = name;
this._image = image;

}

// constructor
public Leaders(String contactID, String name, byte[] image) {
this._name = name;
this._image = image;

}

// constructor
public Leaders(String name, byte[] image) {
this._name = name;
this._image = image;
}


// getting ID
public int getID() {
return this._id;
}

// setting id
public void setID(int keyId) {
this._id = keyId;
}

// getting name
public String getName() {
return this._name;
}

// setting name
public void setName(String name) {
this._name = name;
}

// getting phone number
public byte[] getImage() {
return this._image;
}

// setting phone number
public void setImage(byte[] image) {
this._image = image;
}

// getting comment
public int getComment() {
return this._comment;
}

// setting comment
public void setComment(int comment) {
this._comment = comment;
}
}

这是排行榜的屏幕截图: as you see it displays it in the wrong order

最佳答案

试试这个:mDb.query(DATABASE_TABLE, new String[] { KEY_NAME, KEY_SCORE}, null, null, null, null,KEY_SCORE+"DESC","LIMIT 10");

关于java - 制作一个显示前 10 名玩家的排行榜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27747584/

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