【发布时间】:2020-02-24 17:04:01
【问题描述】:
我正在构建一个 Android 应用程序,该应用程序的用户 cmets 页面可能包含多达 1,000 个 cmets。尝试一次获取所有 cmets 并将其加载到 recyclerView 中并不是一个好习惯。所以,我想要一种方法,可以分批加载 cmets,一次可能 10 个,当用户滚动 recyclerView 时,它应该再加载 10 个等。
问题是我使用 Volley 将 cmets 获取为 JSON 对象,我认为 Volley 没有批处理。我也不确定是否会在我的 PHP 文件上完成一些分页。我在 Youtube 上观看了几个教程,并在 Stack Overflow 上查看了答案,包括 This 和 This 但他们并没有真正解决我的问题。
这是我的 PHP 代码:
<?php
define('DB_HOST','*******');
define('DB_USER','********');
define('DB_PASS','********');
define('DB_NAME','***********');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()){
die('Unable to connect to database '.mysqli_connect_error());
}
$conn->set_charset("utf8mb4");
$storyID = $_GET["storyid"];
$stmt = $conn->prepare("SELECT comments_table.comment_id, comments_table.username, comments_table.comment, comments_table.date_time, comments_table.story_id, comments_table.imageURL, comments_table.number_of_likes, users.title, comments_table.is_reply, comments_table.reply_username, comments_table.reply_comment, comments_table.reply_id FROM comments_table INNER JOIN users ON comments_table.username = users.username WHERE comments_table.story_id = '$storyID'");
$stmt->execute();
$stmt->bind_result($comment_id, $username, $comment, $date_time, $story_id, $imageURL, $number_of_likes, $title, $is_reply, $reply_username, $reply_comment, $reply_id);
$comments = array();
while($stmt->fetch()){
$temp = array();
$temp['comment_id'] = $comment_id;
$temp['username'] = $username;
$temp['comment'] = $comment;
$temp['date_time'] = $date_time;
$temp['story_id'] = $story_id;
$temp['imageURL'] = $imageURL;
$temp['number_of_likes'] = $number_of_likes;
$temp['title'] = $title;
$temp['is_reply'] = $is_reply;
$temp['reply_username'] = $reply_username;
$temp['reply_comment'] = $reply_comment;
$temp['reply_id'] = $reply_id;
array_push($comments, $temp);
}
echo json_encode($comments);
这是我在使用 Volley 检索 cmets 时使用的代码 sn-p:
private void loadComments() {
progressBar.setVisibility(View.VISIBLE);
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_COMMENTS + String.valueOf(storyID),
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 comment = array.getJSONObject(i);
if (comment.getInt("story_id") == storyID) {
commentList.add(new GetComments(
comment.getInt("comment_id"),
comment.getString("username"),
comment.getString("comment"),
comment.getInt("story_id"),
comment.getString("imageURL"),
comment.getString("date_time"),
comment.getInt("number_of_likes"),
comment.getString("title"),
comment.getInt("is_reply"),
comment.getInt("reply_id"),
comment.getString("reply_username"),
comment.getString("reply_comment")
));
}
}
if (commentList.isEmpty()){
noCommentTextView.setVisibility(View.VISIBLE);
}
//creating adapter object and setting it to recyclerview
adapterJSON = new CommentAdapter(getApplicationContext(), commentList, Comment.this, rootView, storyID);
recyclerView.setAdapter(adapterJSON);
} catch (JSONException e) {
progressBar.setVisibility(View.GONE);
Toast.makeText(Comment.this,"Error loading comments: Check internet connection...",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressBar.setVisibility(View.GONE);
Toast.makeText(Comment.this,"Error loading comments: Check internet connection...",Toast.LENGTH_LONG).show();
}
});
final RequestQueue requestQueue = Volley.newRequestQueue(Comment.this);
requestQueue.add(stringRequest);
requestQueue.start();
requestQueue.addRequestFinishedListener(new RequestQueue.RequestFinishedListener<Object>() {
@Override
public void onRequestFinished(Request<Object> request) {
progressBar.setVisibility(View.GONE);
}
});
}
这些是我的 recyclerView 和 layoutManager 实现:
recyclerView = findViewById(R.id.recyclerView);
linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
任何有关如何实现此目标或提供正确方法的解决方案都将受到高度赞赏。 谢谢。
【问题讨论】:
标签: php android android-studio