Reverse list order in array

How can I change this code so that it adds every new object to the top of the list instead of the bottom? I would like the last object to be added to the very top of the list, so you see older objects when you scroll the bottom of the list.

public class StatusAdapter extends ArrayAdapter {
protected Context mContext;
protected List<ParseObject> mStatus;

public StatusAdapter(Context context, List<ParseObject> status) {
    super(context, R.layout.homepage, status);
    mContext = context;
    mStatus = status;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.homepage, null);
        holder = new ViewHolder();
        holder.usernameHomepage = (TextView) convertView
                .findViewById(R.id.usernameHP);
        holder.statusHomepage = (TextView) convertView
                .findViewById(R.id.statusHP);

        convertView.setTag(holder);
    } else {

        holder = (ViewHolder) convertView.getTag();

    }

    ParseObject statusObject = (ParseObject)mStatus.get(position);

    // title
    String username = statusObject.getString("newUser") + ":";
    holder.usernameHomepage.setText(username);

    // content
    String status = statusObject.getString("newStatus");
    holder.statusHomepage.setText(status);

    return convertView;
}

public static class ViewHolder {
    TextView usernameHomepage;
    TextView statusHomepage;

}

}
+4
source share
4 answers

If you want to display the list in the reverse order, the new item is at the top, and then just cancel your list. The java collection class provides an inverse method that undoes all elements in the list. See below code -

Collections.reverse(aList);

The above item is a reverse list of code and the result of saving in the same list.

Hope this helps you.

+12
source

List void add(int index, E element)

( ). ( ) ( ).

, add(0,item), , . , , .

+2

, , , .

public static void sort (List<T> list, Comparator<? super T> comparator)

Ref

+1

, , , .

private List<ParseObject> reverseListOrder(List<ParseObject> status)
{
    Iterator<ParseObject> it = status.iterator();
    List<ParseObject> destination = new ArrayList<>();
    while (it.hasNext()) {
        destination.add(0, it.next());
        it.remove();
    }
    return destination;
}

StatusAdapter, , StatusAdapter.

:

StatusAdapter adapter = new StatusAdapter(this, R.layout.item, reverseListOrder(listOfStatus));
listView.setAdapter(adapter);
+1

All Articles