0

I have source code of an app which displays the list of values as uploaded in the backend server, but the problem is it displays values in opposite order (Things uploaded older is listed first) I want it to be sorted just opposite (By Date).

  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;
}

}

Above code take input from category adapter and displays it as recycler view

4
  • What data are you getting in response? Add response in question. Commented Aug 29, 2018 at 12:41
  • 1
    Replacing "Android Studio" (the IDE, so not a good idea to include in your search terms, the language, Java here, is a better idea) by "Java" from the title text and pasting that in Google returns countless duplicates. Please consider at least typing the title of your question in Google to find duplicates before posting your question. Commented Aug 29, 2018 at 12:49
  • Your code looks very incomplete. Also it is not very well formatted :( Commented Aug 30, 2018 at 6:56
  • Updated again with everything Commented Aug 30, 2018 at 11:23

3 Answers 3

2

Reverse list by Java Collection class

Collections.reverse(arrayToListObject(response));

or reverse RecyclerView items.

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

Edit Example

As you are not doing right thing, here is the way

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

Reason

You reversed another copy of list, and you inserted another list. So first hold list, reverse it, and insert same list in adapter.

Sign up to request clarification or add additional context in comments.

2 Comments

Doesn't work, I have edited the question with full code
Works :) Thanks, I just had to change ArrayList to List as <SingleQuizObject> was a list type
0

First call this

Collections.reverse(arrayToListObject(response));

then

CategoryAdapter mAdapter = new CategoryAdapter(SelectQuizActivity.this, arrayToListObject(response));
                    quizRecyclerView.setAdapter(mAdapter);

Comments

0

java.util.Collections.reverse(arrayToListObject(response))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.