android Firebase实时数据库查询带有Start()的combinig orderbychild()

时间:2018-11-13 20:46:04

标签: java android firebase

我的数据库看起来像这样

enter image description here

我想做的是使用以下代码加载前6个项目,然后加载下5个项目

 DatabaseReference noticeRefu = FirebaseDatabase.getInstance().getReference().child("noticeboard").child("posts");
            Query noticeQuery = noticeRefu.orderByChild("timestamp").startAt("-1542128676980").limitToFirst(15);
 if(!LastPostKey.equals(Lastmostkey))
            {
                noticeDatas.clear();
                noticeQuery.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                        NoticeData n = dataSnapshot.getValue(NoticeData.class);
                        noticeDatas.add(n);
                        Log.d("texting","this is working inside"+ noticeDatas.size());
                        noticeAdapter.notifyDataSetChanged();
                        LastPostKey = dataSnapshot.getKey();
                        Log.d("texting","current Key after Change: "+ LastPostKey);
                    }

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

                    }

                    @Override
                    public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                    }

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

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });
            }

            else
            {
                Toast.makeText(DashBoardActivity.this,"No more notice to load",Toast.LENGTH_LONG).show();
            }

但是当我单击loadmoredata按钮时没有任何显示

这是完整的代码

package com.nadim.csedashboard;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.messaging.FirebaseMessaging;

import com.nadim.csedashboard.adapters.NoticeAdapter;
import com.nadim.csedashboard.dataset.NoticeData;

import java.util.ArrayList;
import java.util.List;

public class DashBoardActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    String username = "";
    String LastPostKey = "", Lastmostkey = "";

    List<NoticeData> noticeDatas =  new ArrayList<>();
    NoticeAdapter noticeAdapter;
    final int item = 5;


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






        new InternetCheck(new InternetCheck.Consumer() {
            @Override
            public void accept(Boolean internet) {
                if(internet)
                {
                    FirebaseMessaging.getInstance().subscribeToTopic("notice").addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(getApplicationContext(),"subs to notice Success",Toast.LENGTH_LONG).show();
                        }
                    });

                    Toast.makeText(DashBoardActivity.this,"Internet Found", Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(DashBoardActivity.this,"Internet not Found", Toast.LENGTH_LONG).show();
                }

            }
        });



        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

        if (user != null) {
             username = "SignOut ("+user.getEmail()+")";
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ListView listViewnotice = findViewById(R.id.listView_notice);

        DatabaseReference noticeRef = FirebaseDatabase.getInstance().getReference().child("noticeboard").child("posts");

        Query noticeQuery = noticeRef.orderByChild("timestamp").limitToFirst(6);

        noticeQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                NoticeData n = dataSnapshot.getValue(NoticeData.class);
                noticeDatas.add(n);
                noticeAdapter.notifyDataSetChanged();
                LastPostKey = dataSnapshot.getKey();

                //Toast.makeText(DashBoardActivity.this,"Current Key:"+LastPostKey,Toast.LENGTH_LONG).show();
                Log.d("texting","current Key: "+ LastPostKey);
            }

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

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


        Query lastKey = noticeRef.orderByKey().limitToFirst(1);

        lastKey.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                Lastmostkey = dataSnapshot.getKey();

                Log.d("texting","Last Key: "+ Lastmostkey);


            }

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

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });


        noticeAdapter = new NoticeAdapter(this,noticeDatas);
        listViewnotice.setAdapter(noticeAdapter);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View loadMore = inflater.inflate(R.layout.more,  null);

        listViewnotice.addFooterView(loadMore);

        Button loadmoredata = loadMore.findViewById(R.id.button_loadmore);

        loadmoredata.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("texting","this but is working inside"+ noticeDatas.size());

                Log.d("texting","current Key after click: "+ LastPostKey);
                Log.d("texting","last Key after click: "+ Lastmostkey);

                DatabaseReference noticeRefu = FirebaseDatabase.getInstance().getReference().child("noticeboard").child("posts");
                Query noticeQuery = noticeRefu.orderByChild("timestamp").startAt("-1542128676980").limitToFirst(noticeDatas.size() + 5 );

                if(!LastPostKey.equals(Lastmostkey))
                {

                    noticeQuery.addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                            NoticeData n = dataSnapshot.getValue(NoticeData.class);
                            noticeDatas.add(n);
                            Log.d("texting","this is working inside"+ noticeDatas.size());
                            noticeAdapter.notifyDataSetChanged();
                            LastPostKey = dataSnapshot.getKey();
                            Log.d("texting","current Key after Change: "+ LastPostKey);
                        }

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

                        }

                        @Override
                        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                        }

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

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });
                }

                else
                {
                    Toast.makeText(DashBoardActivity.this,"No more notice to load",Toast.LENGTH_LONG).show();
                }



            }
        });


        listViewnotice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(DashBoardActivity.this,NoticeActivity.class);
                intent.putExtra("dlimage",noticeDatas.get(i).getNotice_picture());
                intent.putExtra("ntitle",noticeDatas.get(i).getNoticetitle());
                startActivity(intent);
            }
        });



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent svc = new Intent(DashBoardActivity.this, ViewClass.class);
                svc.putExtra("batchname","8thBatch");
                //startService(svc);
                startActivity(svc);
                /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();*/
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            finishAffinity();
            System.exit(0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.dash_board, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        menu.findItem(R.id.action_logout).setTitle(username);
        return super.onPrepareOptionsMenu(menu);


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(DashBoardActivity.this,UserProfile.class);
            startActivity(intent);
            return true;
        }
        if (id == R.id.action_logout) {
            FirebaseAuth.getInstance().signOut();
            Intent intent = new Intent(DashBoardActivity.this,LoginActivity.class);
            startActivity(intent);
            Toast.makeText(DashBoardActivity.this, "Signed Out.",
                    Toast.LENGTH_LONG).show();
            return true;
        }
        if (id == R.id.action_back)
        {
            onBackPressed();

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {

            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


}

我该怎么办?有什么方法可以加载前6个项目,然后加载接下来的5个项目?使用时间戳子

0 个答案:

没有答案