主な活動コード私は「(スコア(名前、スコア)VALUES。INSERT INTO "(myDB.execSQLを" このアンドロイドスタジオアプリケーションでリーダーボードを作成する方法
public void method() {
name = LoginActivity.name;
score = GameView.valueCurrent;
ContentValues values = new ContentValues();
values.put("name", name);
values.put("score", score);
}
を挿入してはいけないしたい
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;
}
}
複数のものがあります。
このデータベースコードを使用して現在のリーダーボードを表示するにはどうすればよいですか?
私はその使い方を知らない。
を再生googleApiClient/Googleに接続していることを確認してください? –