我正在尝试反转ListView,以便首先显示最新的项目。我已经看到了修改getItem()方法的结果,但是这需要我向下滚动并向上滚动以查看新项目。有没有办法让项目出现在列表的顶部而不需要滚动?
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;
}
}
答案 0 :(得分:1)
您可以在创建适配器之前对Comment
列表进行排序。通过这种方式,它们已经按照您希望它们的顺序排列。我不知道Comment
对象包含哪些变量可以让您知道它何时被修改,但假设它是一个日期,您可以像这样对列表进行排序:
Collections.sort(commentsList, new Comparator<Comment>() {
public int compare(Comment c1, Comment c2) {
return c1.getDate().compareTo(c2.getDate());
}
});
您也可以使用Collections.reverse(commentList)
答案 1 :(得分:0)
调用<?php
if(isset($_POST['query'])) {
//connect to database
include 'connect.php';
//retrieve the query
$query = $_POST['query'];
//search database for all similar items
$sql = mysql_query("SELECT * FROM (
SELECT customer_id, customer_name, address, street, phone_1, phone_2, phone_3, phone_4, CONCAT(customer_name, ' ', address, ' ', street, ' ', phone_1, ' ', phone_2, ' ', phone_3, ' ', phone_4) as `mysearch`
FROM customers) base
WHERE `mysearch` LIKE '%{$query}%'
ORDER BY customer_name ASC, street ASC, address ASC
");
$array = array();
while ($row = mysql_fetch_assoc($sql)) {
$array[] = $row['customer_name'] . " " . $row['address'] . " " . $row['street'] . " " . $row['phone_1']. " " . $row['phone_2']. " " . $row['phone_4']. " " . $row['phone_3']." id#" . $row['customer_id'];
}
//return JSON array
echo json_encode($array);
}
?>
应该更新列表。
答案 2 :(得分:0)
我意识到.add()方法实际上是在特定索引处插入项目。如果我总是向索引(0)添加新项目,那么这些项目将自然地以相反的顺序出现。
我认为Google会更直观地使用Android代码并允许使用insert()方法,但index(o)中的add()方法可以达到目的:
mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
setImage(dataSnapshot);
setQuestion(dataSnapshot);
createInitialCommentIDArray(dataSnapshot);
mNumberOfCommentsAtPoll = (int) dataSnapshot.child(COMMENTS_LABEL).getChildrenCount();
for (int i = 0; i < mNumberOfCommentsAtPoll; i++) {
String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("COMMENT").getValue();
Log.v("COMMENT_ID", "The comment ID is " + commentID);
String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("USER_ID").getValue();
Log.v("USER_ID", "The user ID is " + userID);
mCommentArrayList.add(0 , new Comments(mUserAvatar, userID, commentID));
mCommentAdapter.notifyDataSetChanged();
}
}