Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse List Order in Array Adapter

How do I modify this code so that it adds each new object to the top of the list instead of the bottom? I would like the latest object to be added to the very top of the list so you see older objects as you scroll lower down on 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;

}

}
like image 263
user3002680 Avatar asked May 12 '15 03:05

user3002680


1 Answers

If you want to display list in reverse order newest item on top then just reverse your list.Java collection class provide a reverse method which reverse all items in a list.See below code -

Collections.reverse(aList);

Above code reverse list item and store result in same list.

Hope it will help you.

like image 173
Ravi Bhandari Avatar answered Sep 23 '22 06:09

Ravi Bhandari