游戏排名界面就一个listview
同时我为每个item自定义了view
ranklist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rankList" >
</ListView>
</LinearLayout>
自定义的itemview(两个TextView)
rankitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/txtUser"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="20sp"
android:singleLine="true"
android:gravity="right|center" />
<TextView
android:id="@+id/txtScore"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:textSize="20sp"
android:singleLine="true"
android:gravity="left|center" />
</LinearLayout>
RankListActivity.java
package com.example.gamedemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import db.Ranking;
import db.User;
public class RankListActivity extends Activity {
private ArrayList<User> userList = new ArrayList<User>();
private ListView rankList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ranklist);
//从数据库中读取数据
Ranking r = new Ranking(this);
userList = r.getData();
rankList = (ListView) findViewById(R.id.rankList);
ArrayAdapterDemo adapter = new ArrayAdapterDemo(RankListActivity.this,
0, 0, userList);
rankList.setAdapter(adapter);
}
/**
* 重写返回键
* (non-Javadoc)
* @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
setResult(RESULT_OK);
finish();
return true;
} else
return super.onKeyDown(keyCode, event);
}
/**
* 自定义adapter。需要用到listview的优化
*/
public class ArrayAdapterDemo extends ArrayAdapter<User> {
private Wrapper wrapper;
public ArrayAdapterDemo(Context context, int resource,
int textViewResourceId, List<User> objects) {
super(context, 0, 0, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.rankitem,
null);
wrapper = new Wrapper();
wrapper.tvName = (TextView) convertView
.findViewById(R.id.txtUser);
wrapper.tvScore = (TextView) convertView
.findViewById(R.id.txtScore);
convertView.setTag(wrapper);
} else {
wrapper = (Wrapper) convertView.getTag();
}
wrapper.tvName.setText(userList.get(position).getUserName());
wrapper.tvScore
.setText("得分" + userList.get(position).getUserScore());
return convertView;
}
}
class Wrapper { // 持有者模式
private TextView tvName, tvScore;
}
}
实现也不难。接下来我贴出数据库代码和User类
User.java
package db;
public class User {
private String userName;
private int userScore;
public User(String userName, int userScore) {
this.userName = userName;
this.userScore = userScore;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getUserScore() {
return userScore;
}
public void setUserScore(int userScore) {
this.userScore = userScore;
}
}
数据库
DBOpenHelper.java
package db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
//创建数据库、数据表
public class DBOpenHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
private static final String DBNAME = "game.db";
public DBOpenHelper(Context context) {
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table ranking( username varchar(50), userscore integer)");
}
@Override
// 以便数据库版本更新
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
Ranking.java
package db;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Ranking {
private DBOpenHelper helper;
private SQLiteDatabase db;
public Ranking(Context context) {
helper = new DBOpenHelper(context);
db = helper.getWritableDatabase();
}
public ArrayList<User> getData() {
ArrayList<User> list = new ArrayList<User>();
Cursor cursor = db.rawQuery("select * from ranking", null);
while (cursor.moveToNext()) {
list.add(new User(cursor.getString(cursor
.getColumnIndex("username")), cursor.getInt(cursor
.getColumnIndex("userscore"))));
}
return list;
}
public void insert(User user) {
db.execSQL("insert into ranking (username, userscore) values (?,?)",
new Object[] { user.getUserName(), user.getUserScore() });
}
}
下一篇就是最难得了