【问题标题】:making a leader-board that displays top 10 players?制作一个显示前 10 名玩家的排行榜?
【发布时间】:2015-03-01 02:31:26
【问题描述】:

我创建的排行榜有问题,该排行榜用于显示得分最高的前 10 名玩家,但它以相反的顺序显示最低的第一名,我试图更改选择语句和版本数据库,没有工作任何人有任何想法如何解决这个问题? 这是排行榜活动:

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

我在数据库中的get函数:

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

Leader Class 设置和获取数据:

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

这是排行榜的屏幕截图:

【问题讨论】:

  • 试试cursor = mDb.rawQuery("SELECT TOP(10) * FROM Game ORDER by score DESC ", null); 另外,如果应用仍然崩溃,请发布错误日志,以便我们查看问题所在
  • 您的错误是说fname 列在表questions 中不存在。那是正确的列名吗?这是正确的表名吗?
  • 我犯了一个错误,表常量是 Game_TABLE 仍然给出光标错误
  • 它表示您正在尝试阅读row 0, col -1。您无法访问负索引列。
  • 谢谢已经解决了,是的,表名旁边的列参数有错误

标签: java android listview sqlite leaderboard


【解决方案1】:

您的 SQLite 查询不正确。试试下面的方法:

cursor = mDb.rawQuery("SELECT * FROM Game ORDER BY score DESC LIMIT 10",
            null);

【讨论】:

  • 没有工作,应用程序崩溃 RunTime Exception Unable to resume activity
【解决方案2】:

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

【讨论】:

  • 我是否必须将所有列常量放入列参数中?
  • 不,但如果你愿意,你可以。我认为在您的情况下,这是您想要的名称和分数。
  • 它还给出了运行时异常,无法恢复活动!!
  • 您实际上在 onResume 中做了很多工作。我认为它不是做这么长的工作的最佳场所。在 List contacts = db.getAllContactsFromAndroid() 之后添加 db.close();并提供更新。
  • 使用这个解决了问题: mDb.query(GAME_TABLE, new String[] { G_PHOTO, G_FIRSTNAME, G_LASTNAME, G_USERNAME, G_EMAIL, G_UID, G_SCORE }, null, null, null, null,G_SCORE+" DESC");非常感谢
猜你喜欢
  • 2020-06-15
  • 2019-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-11
相关资源
最近更新 更多