从Firebase检索数据后需要反转ListView [英] Need to reverse the listview after retriving data from firebase

查看:24
本文介绍了从Firebase检索数据后需要反转ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Android应用程序.在我的应用程序中,当人们单击按钮时,它会转到requestlist活动并显示所有名称和描述.每个数据都存储在Firebase中.firebase依次一个接一个地依次存储数据.这就是为什么当我从firebase中获取数据时,列表会顺序显示所有内容.但是我想在最后显示最后一个数据.我该怎么做.

I have a created an android app.In my app when people click on the button it goes to requestlist activity and displays all the name and description. Every data is stored in the firebase. firebase store data serially one by one below one another.Thats why when I fetch data from firebase the list shows everything serially.. But i want to show last data at top. How I can do that.

这是我的活动,其名称为requestList

Here is my activity its names requestList

RequestList.java

String description;
ArrayList<String> RequestList;
ListView listView;
ArrayAdapter<String> arrayAdapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_request_list);


    RequestList = new ArrayList<>();

    listView = (ListView) findViewById(R.id.list_request);
    arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.list, RequestList);

    listView.setAdapter(arrayAdapter);





    DatabaseReference myRef = database.getReference("Request");



    myRef.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            RequestName Request = dataSnapshot.getValue(RequestName.class);
            String requestInfo = "Name : "+ Request.requestname + "   \nDescription : " +  Request.description;
            RequestList.add(requestInfo);
            arrayAdapter.notifyDataSetChanged();
        }



        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

这是布局

activity_request_list



<ListView
    android:id="@+id/list_request"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:divider="@color/red"
    android:dividerHeight="2dip" />

我已使用自定义布局进行显示

I have used a custom layout for display

list_item

<TextView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:textStyle="bold"
    android:layout_marginLeft="15dp"
    android:textColor="@color/White" />

请帮助

推荐答案

我会尝试添加 requestInfo 作为 RequestList 的第一个元素.这可能会解决您的问题,而不是反转所有列表.

I would try to add the requestInfo as the first element of the RequestList. This might solve your problem rather than reversing all the list.

myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        RequestName request = dataSnapshot.getValue(RequestName.class);
        String requestInfo = "Name : "+ Request.requestname + "   \nDescription : " +  Request.description;
        RequestList.add(0, requestInfo);
        arrayAdapter.notifyDataSetChanged();
    }

    // Other methods are removed for simplicity. 
});

希望这对您有所帮助!

这篇关于从Firebase检索数据后需要反转ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆