首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android studio应用程序中创建LeaderBoard

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

Stack Overflow用户
提问于 2016-09-11 02:26:50
回答 1查看 4.4K关注 0票数 0

主要活动代码

我想插入这个

代码语言:javascript
复制
public void method() {
        name = LoginActivity.name;
        score = GameView.valueCurrent;
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("score", score);
    }

And dont“myDB.execSQL(”插入到分数(姓名,分数)值('Marie','4');");“

代码语言:javascript
复制
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) {

/***在本例中,我希望将正在使用我的应用程序的用户的值

代码语言:javascript
复制
                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');");

**//

代码语言:javascript
复制
            }

        }


    } 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代码

代码语言:javascript
复制
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;

}

}

物料编码

代码语言:javascript
复制
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;
}

}

还有几件事。

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

我不知道如何在上面使用。

EN

回答 1

Stack Overflow用户

发布于 2016-09-11 02:42:37

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

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

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

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游戏服务

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39429272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档