我无法通过 MySQL、PHP 获得 Android Studio 上的玩家列表,排球按分数降序排列
I cannot get a list of players on Android Studio via MySQL, PHP, Volley in descending order of points
我试图在 Android Studio 中使用 RecyclerView 显示按分数、照片数量和他们的最高分排名的玩家列表。发生的事情是我得到了一个列表,其中包含相同的用户名、积分、照片和高分,大小为用户名总数
我希望我的 Android 屏幕显示这样的内容...
MySQL table (via XAMPP)
这是我的 PHP 代码 - 对我来说似乎没问题,因为我已经通过写入本地主机名
对其进行了测试
<?php
require_once 'connect.php';
$stmt = $conn->prepare("SELECT username, points, photos, highscore FROM users ORDER BY points DESC, photos ASC, highscore DESC;");
$stmt->execute();
$stmt->bind_result($username, $points, $photos, $highscore);
$players = array();
while($stmt->fetch()){
$temp = array();
$temp['username'] = $username;
$temp['points'] = $points;
$temp['photos'] = $photos;
$temp['highscore'] = $highscore;
array_push($players, $temp);
}
$stmt->close();
echo json_encode($players);
?>
PHP test via localhost address
尝试在 Android 上显示时,I get the following output...
这是我的 Android:
代码
rankings.java
public class rankings extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager manager;
private RecyclerView.Adapter mAdapter;
private List<player_object> players;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rankings);
recyclerView = findViewById(R.id.listView);
manager = new GridLayoutManager(rankings.this, 1);
recyclerView.setLayoutManager(manager);
players = new ArrayList<>();
getPlayers();
}
private void getPlayers(){
StringRequest stringRequest = new StringRequest(Request.Method.GET, URLs.URL_RANKING,
new Response.Listener<String>(){
@Override
public void onResponse(String response) {
try{
JSONArray array = new JSONArray(response);
for (int i = 0 ; i<array.length() ; i++){
JSONObject object = array.getJSONObject(i);
String player = object.getString("username");
int points = object.getInt("points");
int photos = object.getInt("photos");
int highscore = object.getInt("highscore");
player_object gamer = new player_object(player, points, photos, highscore);
players.add(gamer);
}
} catch(Exception e){
e.printStackTrace();
}
mAdapter = new RecyclerAdapter(rankings.this, players);
recyclerView.setAdapter(mAdapter);
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Toast.makeText(rankings.this, "internet gone", Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(rankings.this).add(stringRequest);
}
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context context;
private List<player_object> players;
public RecyclerAdapter (Context context, List<player_object> players){
this.context = context;
this.players = players;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.activity_rankings_row,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
player_object player = players.get(position);
holder.rank.setText(Integer.toString(position+1));
holder.gamer.setText(player.getPlayer());
holder.points.setText(Integer.toString(player.getPoints()));
holder.photos.setText(Integer.toString(player.getPhotos()));
holder.highscore.setText(Integer.toString(player.getHighscore()));
}
@Override
public int getItemCount() {
return players.size();
}
/*public*/ class MyViewHolder extends RecyclerView.ViewHolder {
/*private*/ TextView rank;
/*private*/ TextView gamer;
/*private*/ TextView points;
/*private*/ TextView photos;
/*private*/ TextView highscore;
/*private*/ LinearLayout container;
public MyViewHolder (View view){
super(view);
rank = view.findViewById(R.id.textView_rank);
gamer = view.findViewById(R.id.textView_player);
points = view.findViewById(R.id.textView_points);
photos = view.findViewById(R.id.textView_photos);
highscore = view.findViewById(R.id.textView_highscore);
container = view.findViewById(R.id.player_container);
}
}
player_object.java
public class player_object {
private static String gamer;
private static int points;
private static int photos;
private static int highscore;
public player_object(String gamer, int points, int photos, int highscore) {
this.gamer = gamer;
this.points = points;
this.photos = photos;
this.highscore = highscore;
}
public String getPlayer(){
return gamer;
}
public int getPoints(){
return points;
}
public int getPhotos(){
return photos;
}
public int getHighscore(){
return highscore;
}
public void setPlayer(String gamer){
this.gamer = gamer;
}
public void setPoints(int points){
this.points = points;
}
public void setPhotos(int photos){
this.photos = photos;
}
public void setHighscore(int highscore){
this.highscore = highscore;
}
在 player_object
中,从每个 class 变量中删除 static
。它们将采用最后设置的值,因为它们与您创建的对象无关。
我试图在 Android Studio 中使用 RecyclerView 显示按分数、照片数量和他们的最高分排名的玩家列表。发生的事情是我得到了一个列表,其中包含相同的用户名、积分、照片和高分,大小为用户名总数
我希望我的 Android 屏幕显示这样的内容... MySQL table (via XAMPP)
这是我的 PHP 代码 - 对我来说似乎没问题,因为我已经通过写入本地主机名
对其进行了测试<?php
require_once 'connect.php';
$stmt = $conn->prepare("SELECT username, points, photos, highscore FROM users ORDER BY points DESC, photos ASC, highscore DESC;");
$stmt->execute();
$stmt->bind_result($username, $points, $photos, $highscore);
$players = array();
while($stmt->fetch()){
$temp = array();
$temp['username'] = $username;
$temp['points'] = $points;
$temp['photos'] = $photos;
$temp['highscore'] = $highscore;
array_push($players, $temp);
}
$stmt->close();
echo json_encode($players);
?>
PHP test via localhost address
尝试在 Android 上显示时,I get the following output...
这是我的 Android:
代码rankings.java
public class rankings extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.LayoutManager manager;
private RecyclerView.Adapter mAdapter;
private List<player_object> players;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rankings);
recyclerView = findViewById(R.id.listView);
manager = new GridLayoutManager(rankings.this, 1);
recyclerView.setLayoutManager(manager);
players = new ArrayList<>();
getPlayers();
}
private void getPlayers(){
StringRequest stringRequest = new StringRequest(Request.Method.GET, URLs.URL_RANKING,
new Response.Listener<String>(){
@Override
public void onResponse(String response) {
try{
JSONArray array = new JSONArray(response);
for (int i = 0 ; i<array.length() ; i++){
JSONObject object = array.getJSONObject(i);
String player = object.getString("username");
int points = object.getInt("points");
int photos = object.getInt("photos");
int highscore = object.getInt("highscore");
player_object gamer = new player_object(player, points, photos, highscore);
players.add(gamer);
}
} catch(Exception e){
e.printStackTrace();
}
mAdapter = new RecyclerAdapter(rankings.this, players);
recyclerView.setAdapter(mAdapter);
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Toast.makeText(rankings.this, "internet gone", Toast.LENGTH_SHORT).show();
}
});
Volley.newRequestQueue(rankings.this).add(stringRequest);
}
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private Context context;
private List<player_object> players;
public RecyclerAdapter (Context context, List<player_object> players){
this.context = context;
this.players = players;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.activity_rankings_row,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
player_object player = players.get(position);
holder.rank.setText(Integer.toString(position+1));
holder.gamer.setText(player.getPlayer());
holder.points.setText(Integer.toString(player.getPoints()));
holder.photos.setText(Integer.toString(player.getPhotos()));
holder.highscore.setText(Integer.toString(player.getHighscore()));
}
@Override
public int getItemCount() {
return players.size();
}
/*public*/ class MyViewHolder extends RecyclerView.ViewHolder {
/*private*/ TextView rank;
/*private*/ TextView gamer;
/*private*/ TextView points;
/*private*/ TextView photos;
/*private*/ TextView highscore;
/*private*/ LinearLayout container;
public MyViewHolder (View view){
super(view);
rank = view.findViewById(R.id.textView_rank);
gamer = view.findViewById(R.id.textView_player);
points = view.findViewById(R.id.textView_points);
photos = view.findViewById(R.id.textView_photos);
highscore = view.findViewById(R.id.textView_highscore);
container = view.findViewById(R.id.player_container);
}
}
player_object.java
public class player_object {
private static String gamer;
private static int points;
private static int photos;
private static int highscore;
public player_object(String gamer, int points, int photos, int highscore) {
this.gamer = gamer;
this.points = points;
this.photos = photos;
this.highscore = highscore;
}
public String getPlayer(){
return gamer;
}
public int getPoints(){
return points;
}
public int getPhotos(){
return photos;
}
public int getHighscore(){
return highscore;
}
public void setPlayer(String gamer){
this.gamer = gamer;
}
public void setPoints(int points){
this.points = points;
}
public void setPhotos(int photos){
this.photos = photos;
}
public void setHighscore(int highscore){
this.highscore = highscore;
}
在 player_object
中,从每个 class 变量中删除 static
。它们将采用最后设置的值,因为它们与您创建的对象无关。