M
M
max_vr2016-07-05 18:41:06
Java
max_vr, 2016-07-05 18:41:06

How to make adapter take data from ArrayList in reverse order?

There is an adapter that takes data from an ArrayList. You need to either make the adapter take objects from the arraylist in reverse order, or sort the ListView itself in a similar way. Thanks in advance

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
bot8, 2016-07-05
@max_vr

ArrayList<String> List = new ArrayList<>(); // Исходный ArrayList
        ArrayList<String> newList = new ArrayList<>(); 
        List.add("Первый");  // Заполнил List
        List.add("Второй");
        List.add("Третий");
        
        for (int i=List.size()-1; i>=0; i--) // Перевернул List и засунул 
            newList.add(List.get(i));        // в newList
      
        for(String str : newList)
            System.out.println(str);

or so
public class MyAdapter extends ArrayAdapter<String> {
        private List<String> list;

    public MyAdapter(Context context, int resource, List<String> list) {
        super(context, resource, list);
        this.list=list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public String getItem(int position) {
        return list.get((position-list.size())*-1-1);
    }
}

O
one pavel, 2016-07-05
@onepavel

Collections.reverse(arrayList);

D
Dmitry Alexandrov, 2016-07-05
@jamakasi666

Derivate from ArrayList and override get method? Or add a new getReverse method? I didn’t quite catch your problem, the difficulty is that you can’t change the ArrayList or the adapter?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question