我想顛倒ListView,以便最先出現的項目。我看到了修改getItem()方法的結果,但是這要求我向下滾動並滾動回來查看新項目。有沒有辦法讓項目出現在列表的頂部而不需要滾動?顛倒ListView的順序和顯示沒有滾動
public class ListAdapter extends ArrayAdapter<Comments> {
Firebase BaseRef = new Firebase(FIREBASE_URL);
Firebase PollsRef = mBaseRef.child(POLLS_LABEL);
Firebase UpdateRef = mPollsRef.child(mCurrentDateString).child(String.valueOf(mPollIndex + 1));
Firebase CommentsRef = mUpdateRef.child(COMMENTS_LABEL);
int pollCommentCount;
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, List<Comments> items) {
super(context, resource, items);
}
@Override
public int getCount() {
CommentsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
pollCommentCount = (int) dataSnapshot.getChildrenCount();
Log.v("POLL_COMMENT_COUNT", "The poll comment count is " + pollCommentCount);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
return pollCommentCount;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.individual_comment, null);
}
Comments p = getItem(position);
if (p != null) {
TextView userID = (TextView) v.findViewById(R.id.user_ID);
TextView userComment = (TextView) v.findViewById(R.id.user_comment);
if (userID != null) {
userID.setText(p.getUserID());
}
if (userComment != null) {
userComment.setText(p.getUserComment());
}
}
return v;
}
}
你能排序'Comments'列表發送給適配器之前?可能用'Collections'? – mattfred
你可以發佈示例代碼嗎? – tccpg288
使用Collections.sort和比較器對您的支持列表進行排序,並使用模型的新近度作爲要比較的字段。然後在您的適配器上調用notifyDataSetChanged來通知ListView它需要刷新自己。 –