如何在android studio应用程序中创建LeaderBoard

时间:2016-09-10 18:26:50

标签: android database sqlite

主要活动代码

我想插入此

public void method() {
        name = LoginActivity.name;
        score = GameView.valueCurrent;
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("score", score);
    }

并且不要“myDB.execSQL(”INSERT INTO分数(名称,分数)VALUES('Marie','4');“);”

public class MainActivity extends AppCompatActivity {

private List<Items> itemsList = new ArrayList<Items>();
private ListView listView;
private CustomListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SQLiteDatabase myDB = null;

    try {

        //Create a Database if doesnt exist otherwise Open It

        myDB = this.openOrCreateDatabase("leaderboard", MODE_PRIVATE, null);

        //Create table in database if it doesnt exist allready

        myDB.execSQL("CREATE TABLE IF NOT EXISTS scores (name TEXT, score TEXT);");

        //Select all rows from the table

        Cursor cursor = myDB.rawQuery("SELECT * FROM scores", null);

        //If there are no rows (data) then insert some in the table

        if (cursor != null) {
            if (cursor.getCount() == 0) {

/ ***在这种情况下,我想要放置正在播放我的应用程序的用户的值

                myDB.execSQL("INSERT INTO scores (name, score) VALUES ('Andy', '7');");
                myDB.execSQL("INSERT INTO scores (name, score) VALUES ('Marie', '4');");
                myDB.execSQL("INSERT INTO scores (name, score) VALUES ('George', '1');");

** //

            }

        }


    } catch (Exception e) {

    } finally {

        //Initialize and create a new adapter with layout named list found in activity_main layout

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListAdapter(this, itemsList);
        listView.setAdapter(adapter);

        Cursor cursor = myDB.rawQuery("SELECT * FROM scores", null);

            if (cursor.moveToFirst()) {

                //read all rows from the database and add to the Items array

                while (!cursor.isAfterLast()) {

                    Items items = new Items();

                    items.setName(cursor.getString(0));
                    items.setScore(cursor.getString(1));

                    itemsList.add(items);
                    cursor.moveToNext();


                }
            }

        //All done, so notify the adapter to populate the list using the Items Array

        adapter.notifyDataSetChanged();
        }

    }

}

CustomListAdapter代码

public class CustomListAdapter extends BaseAdapter {

private  Context mContext;
private LayoutInflater inflater;
private List<Items> itemsItems;



public CustomListAdapter(Context context, List<Items> itemsItems) {
    this.mContext = context;
    this.itemsItems = itemsItems;

}

@Override
public int getCount() {
    return itemsItems.size();
}

@Override
public Object getItem(int location) {
    return itemsItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View scoreView, ViewGroup parent) {
    ViewHolder holder;
    if (inflater == null) {
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    if (scoreView == null) {

        scoreView = inflater.inflate(R.layout.list_row, parent, false);
        holder = new ViewHolder();
        holder.name = (TextView) scoreView.findViewById(R.id.name);
        holder.score = (TextView) scoreView.findViewById(R.id.score);

        scoreView.setTag(holder);

    } else {
        holder = (ViewHolder) scoreView.getTag();
    }

    final Items m = itemsItems.get(position);
    holder.name.setText(m.getName());
    holder.score.setText(m.getScore());

    return scoreView;
}

static class ViewHolder {

    TextView name;
    TextView score;

}

}

物品代码

public class Items {

private String name, score;

public Items() {
}

public Items(String name, String score) {

    this.name = name;
    this.score = score;

}

public String getName() {
    return name;
}

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

public String getScore() {
    return score;
}

public void setScore(String score) {
    this.score = score;
}

}

还有更多的东西。

如何使用此数据库代码显示当前排行榜?

我不知道如何使用它。

1 个答案:

答案 0 :(得分:0)

使用Google Play:https://developers.google.com/games/services/android/leaderboards

在开发者控制台中创建Google Play游戏

1)从这里添加BaseGameUtils SDK https://github.com/playgameservices/android-basic-samples

2)在app gradle中:  dependencies { compile 'compile project(':BaseGameUtils')' }

3)更新分数:Games.Leaderboards.submitScore(mGoogleApiClient, LEADERBOARD_ID, 1337);

4) 显示排行榜:startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient, LEADERBOARD_ID), REQUEST_LEADERBOARD);

确保您已成功连接到googleApiClient / Google Play游戏服务