gpt4 book ai didi

java - 如何将Recyclerview从最后一个ID到第一个ID排序?

转载 作者:行者123 更新时间:2023-11-29 18:34:53 25 4
gpt4 key购买 nike

我请求帮助将 Recyclerview 从最后一个 id 排序到第一个 id。

当我在数据库中添加一行时,我想显示在 recyclerview 中。

如何将数据库中的最后一行显示为列表中的第一张卡?

这是我的 php 代码:

<?php 
//Creating a connection
$con = mysqli_connect("--","---","----","-----");

/* change character set to utf8 */
$con->set_charset("utf8");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
/*Get the id of the last visible item in the RecyclerView from the request and store it in a variable. For the first request id will be zero.*/
$id = $_GET["id"];

$sql= "Select * from movies_table where id between ($id+1) and ($id+10)";

$result = mysqli_query($con ,$sql);

while ($row = mysqli_fetch_assoc($result)) {

$array[] = $row;

}
header('Content-Type: text/html; charset=utf-8');

echo json_encode($array, JSON_UNESCAPED_UNICODE );

mysqli_free_result($result);

mysqli_close($con);

?>

这是我的 recyclerview 类:

public class ThreeFragment extends Fragment{

private static final String TAG = ThreeFragment.class.getSimpleName();
private List<Movie> movies;
private RecyclerView recyclerView;
private GridLayoutManager gridLayout;
private MoviesAdapter adapter;


public ThreeFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_three, container, false);

recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
movies = new ArrayList<>();
getMoviesFromDB(0);

gridLayout = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(gridLayout);

adapter = new MoviesAdapter(getActivity(), movies);
recyclerView.setAdapter(adapter);

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

if (gridLayout.findLastCompletelyVisibleItemPosition() == movies.size() - 1) {
getMoviesFromDB(movies.get(movies.size() - 1).getId());
}

}
});
return view;
}




private void getMoviesFromDB(int id) {

AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {



ProgressDialog pdLoading = new ProgressDialog(getActivity());

@Override
protected void onPreExecute() {
super.onPreExecute();

//this method will be running on UI thread
pdLoading.setMessage("\tدر حال بارگذاری...");
pdLoading.setCancelable(false);
pdLoading.show();

}


@Override
protected Void doInBackground(Integer... movieIds) {

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://astaneapp.ir/wp-php/movies.php?id=" + movieIds[0])
.build();
try {
Response response = client.newCall(request).execute();

JSONArray array = new JSONArray(response.body().string());

for (int i = 0; i < array.length(); i++) {

JSONObject object = array.getJSONObject(i);

Movie movie = new Movie(object.getInt("id"), object.getString("movie_name"),
object.getString("movie_image"), object.getString("movie_genre") , object.getString("movie_discription") , object.getString("movie_lat"), object.getString("movie_lon") , object.getString("movie_marker"));

ThreeFragment.this.movies.add(movie);
}


} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
pdLoading.dismiss();
}
};

asyncTask.execute(id);
}
}

我需要发送适配器类代码吗?

最佳答案

你可以这样尝试

@Override
protected void onPostExecute(Void aVoid) {

Collections.sort(ThreeFragment.this.movies, new Comparator<Movie>() {
public int compare(Movie o1, Movie o2) {
Integer firstItem = o1.getId();
Integer secondItem = o2.getId();
return secondItem.compareTo(firstItem);
}
});

adapter.notifyDataSetChanged();
pdLoading.dismiss();
}

关于java - 如何将Recyclerview从最后一个ID到第一个ID排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45387047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com