从Firebase检索数据后需要反转ListView

时间:2018-10-15 09:36:16

标签: java android firebase firebase-realtime-database

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

这是我的活动,其名称为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" />

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

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" />

请帮助

2 个答案:

答案 0 :(得分:1)

我会尝试在requestInfo前面添加RequestList。这可能会解决您的问题,而不是反转所有列表。

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. 
});

希望这对您有帮助!

答案 1 :(得分:0)

您可以将Collection用于反向,例如:-Collection.reverse(RequestList);

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);
       Collection.reverse(RequestList);
        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) {

    }
});mpElements);