我无法通过 MySQL、PHP、Volley 按点的降序获取 Android Studio 上的玩家列表
Posted
技术标签:
【中文标题】我无法通过 MySQL、PHP、Volley 按点的降序获取 Android Studio 上的玩家列表【英文标题】:I cannot get a list of players on Android Studio via MySQL, PHP, Volley in descending order of points 【发布时间】:2021-05-29 16:42:09 【问题描述】:我正在尝试使用 RecyclerView 在 android Studio 上显示按分数、照片数量和最高分排名的玩家列表。发生的情况是,我得到一个包含相同用户名、分数、照片和高分的列表,其大小与用户名总数相同
我希望我的 Android 屏幕显示类似的内容... mysql table (via XAMPP)
这是我的 php 代码 - 对我来说似乎很好,因为我已经通过编写 localhost 名称对其进行了测试
<?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;
【问题讨论】:
【参考方案1】:在player_object
中,从每个类变量中删除static
。它们将采用最后设置的值,因为它们与您创建的对象无关。
【讨论】:
以上是关于我无法通过 MySQL、PHP、Volley 按点的降序获取 Android Studio 上的玩家列表的主要内容,如果未能解决你的问题,请参考以下文章
Android Volley:通过用户指定的 id 从 MySQL 数据库中选择数据
从MySql DB检索PDF到android(使用Volley)