gpt4 book ai didi

java - 如何在 Java 中反转数组

转载 作者:行者123 更新时间:2023-12-02 01:57:52 27 4
gpt4 key购买 nike

我有一个应用程序的源代码,它显示在后端服务器中上传的值列表,但问题是它以相反的顺序显示值(首先列出较早上传的内容)我希望它以相反的顺序排序(按日期)。

  public class SelectQuizActivity extends AppCompatActivity {

private static final String TAG = SelectQuizActivity.class.getSimpleName();

private int categoryId;
private String catName;
private String catImage;

private ImageView categoryImage;
private TextView categoryName;

private RecyclerView quizRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_quiz);

categoryId = getIntent().getExtras().getInt(Constants.CATEGORY);
catName = getIntent().getExtras().getString(Constants.CATEGORY_NAME);
catImage = getIntent().getExtras().getString(Constants.CATEGORY_IMAGE);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if(getSupportActionBar() != null){
getSupportActionBar().setElevation(0);
}
setTitle("");

categoryImage = (ImageView)findViewById(R.id.quiz_category_image);
categoryName = (TextView)findViewById(R.id.quiz_category_name);

quizRecyclerView = (RecyclerView)findViewById(R.id.quiz_category);
quizRecyclerView.setLayoutManager(new GridLayoutManager(SelectQuizActivity.this, 3));
quizRecyclerView.setHasFixedSize(true);

if(Helper.isNetworkAvailable(this)){
allSubcategoryInCategory(catName);
}else{
DisplayMessage.displayErrorMessage(this, "No network available");
}

}

private void allSubcategoryInCategory(String name){
Map params = getParams(name);
GsonRequest<SingleQuizObject[]> serverRequest = new GsonRequest<SingleQuizObject[]>(
Request.Method.POST,
Constants.PATH_TO_QUIZ_SUBCATEGORIES,
SingleQuizObject[].class,
params,
createRequestSuccessListener(),
createRequestErrorListener());

((CustomApplication)getApplication()).getNetworkCall().callToRemoteServer(serverRequest);
}

private Map getParams(String name){
Map<String, String> params = new HashMap<String,String>();
params.put(Constants.NAME, name);
return params;
}

private Response.Listener<SingleQuizObject[]> createRequestSuccessListener() {
return new Response.Listener<SingleQuizObject[]>() {
@Override
public void onResponse(SingleQuizObject[] response) {
try {
if(response != null){
categoryName.setText(catName + " subcategories");
String serverImagePath = Constants.PUBLIC_FOLDER + catImage;
RequestOptions requestOptions = new RequestOptions();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
Glide.with(SelectQuizActivity.this).load(serverImagePath).apply(requestOptions.fitCenter().override(80, 80)).into(categoryImage);
ArrayList<SingleQuizObject> list = arrayToListObject(response);
Collections.reverse(list);
CategoryAdapter mAdapter = new CategoryAdapter(SelectQuizActivity.this, arrayToListObject(list));
quizRecyclerView.setAdapter(mAdapter);
} else{
displayErrorMessage(SelectQuizActivity.this, "No subcategory found ");
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
}

private Response.ErrorListener createRequestErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
};
}


private List<SingleQuizObject> arrayToListObject(SingleQuizObject[] response){
List<SingleQuizObject> allCategories = new ArrayList<>();
Collections.addAll(allCategories, response);
return allCategories;
}

}

上面的代码从类别适配器获取输入并将其显示为回收器 View

最佳答案

按 Java Collection 类反向列表

Collections.reverse(arrayToListObject(response));

或反向RecyclerView项目。

LinearLayoutManager lm = new LinearLayoutManager(YourActivity.this);
lm.setReverseLayout(true);
lm.setStackFromEnd(true);

编辑示例

由于你没有做正确的事情,这就是方法

ArrayList<SingleQuizObject> list = arrayToListObject(response);
Collections.reverse(list);
CategoryAdapter mAdapter = new CategoryAdapter(SelectQuizActivity.this, list);

原因

您反转了列表的另一个副本,并插入了另一个列表。因此,首先保留列表,反转它,然后在适配器中插入相同的列表。

关于java - 如何在 Java 中反转数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52077683/

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