我正在尝试在名为Leaderboard.xml的屏幕上填充列表视图我已经在各种不同的站点周围,似乎没有人显示这样做的明确方式(大多数似乎需要某种形式的用户输入)。
我有一个数据库助手类,如下所示:
package com.example.deepseadiver;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper {
private static final String DATABASE_NAME = "ScoreDatabase";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "Highscores";
private OpenHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context dbContext;
private static final String DATABASE_CREATE =
"CREATE TABLE " + TABLE_NAME + " (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"score TEXT);";
public DatabaseHelper(Context ctx) {
this.dbContext = ctx;
}
public DatabaseHelper open() throws SQLException {
mDbHelper = new OpenHelper(dbContext);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public boolean createUser(String name, String score) {
ContentValues initialValues = new ContentValues();
initialValues.put("name", name);
initialValues.put("score", score);
return mDb.insert(TABLE_NAME, null, initialValues) > 0;
}
public boolean updateUsers(long rowId, String name, String score) {
ContentValues args = new ContentValues();
args.put("name", name);
args.put("score", score);
return mDb.update(TABLE_NAME, args, "_id=" + rowId, null) > 0;
}
public void deleteAll() {
mDb.delete(TABLE_NAME, null, null);
}
public void deleteRecord(long rowID) {
mDb.delete(TABLE_NAME, "_rowId=" + rowID, null);
}
public Cursor getdata(String table)
{
return mDb.rawQuery("Select * from "+table+"", null);
}
public Cursor fetchAll(SQLiteDatabase db, String table){
Cursor cursor = db.rawQuery("Select * from "+table+"", null);
return cursor;
}
public ArrayList<String[]> fetchScore(String name) throws SQLException {
ArrayList<String[]> myArray = new ArrayList<String[]>();
int pointer = 0;
Cursor mCursor = mDb.query(TABLE_NAME, new String[] {"_id", "name",
"score"}, "name LIKE '%" + name + "%'", null,
null, null, "score desc");
int NameColumn = mCursor.getColumnIndex("name");
int ScoreColumn = mCursor.getColumnIndex("score");
if (mCursor != null){
if (mCursor.moveToFirst()){
do {
myArray.add(new String[3]);
myArray.get(pointer)[0] = mCursor.getString(NameColumn);
myArray.get(pointer)[1] = mCursor.getString(ScoreColumn);
pointer++;
} while (mCursor.moveToNext());
} else {
myArray.add(new String[3]);
myArray.get(pointer)[0] = "NO RESULTS";
myArray.get(pointer)[1] = "";
}
}
return myArray;
}
public ArrayList<String[]> selectAll() {
ArrayList<String[]> results = new ArrayList<String[]>();
int counter = 0;
Cursor cursor = this.mDb.query(TABLE_NAME, new String[] { "id", "name", "score"}, null, null, null, null, "name desc");
if (cursor.moveToFirst()) {
do {
results.add(new String[2]);
results.get(counter)[0] = cursor.getString(0).toString();
results.get(counter)[1] = cursor.getString(1).toString();
results.get(counter)[2] = cursor.getString(2).toString();
counter++;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return results;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
和leaderboard.java:
package com.example.deepseadiver;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
public class Leaderboard extends Activity {
RelativeLayout LeaderExit;
String table = "Highscores";
DatabaseHelper dbh;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leaderboard);
dbh = new DatabaseHelper(this);
dbh.open();
LeaderExit = (RelativeLayout) findViewById(R.id.Exit);
LeaderExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Leaderboard.this.finish();
}
});
}
}
我尝试填充的列表视图的id为lvItems,它位于leaderboard.xml屏幕上。
感谢您的帮助