addValueEventListener不能与过滤器一起使用或单独使用

时间:2019-02-04 12:32:14

标签: java android firebase firebase-realtime-database

我正在尝试将项目("Noticia")添加到recyclerview,我希望在滚动时将它们从20添加到20,但这是行不通的,我尝试显示所有新闻并且它可以工作当我滚动时,它们又全部添加了,但我会离开去做。调试在mDatabase.child("News")中停止。 AddValueEventListener (new ValueEventListener () 并跳到代码的结尾

package com.example.corre.itab;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
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.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final int TOTAL_ITEM_EACH_LOAD = 4;
    private DatabaseReference mDatabase;
    RecyclerView rv;
    List<Noticia> listaNoticias;
    FirebaseRecyclerAdapter adapter;
    LinearLayoutManager layoutManager;
    String utimaKey = "";
    private int currentPage = 0;
    private AdapterNoticias adapter1;
    private ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv = (RecyclerView) findViewById(R.id.recycleView);
        mProgressBar = (ProgressBar) findViewById(R.id.progressbar);

        mDatabase = FirebaseDatabase.getInstance().getReference();
        listaNoticias = new ArrayList<>();
        adapter1 = new AdapterNoticias(listaNoticias);
        layoutManager = new LinearLayoutManager(getApplicationContext());
        rv.setLayoutManager(layoutManager);
        rv.setAdapter(adapter1);
        rv.addOnScrollListener(new EndlessRecyclerOnScrollListener(layoutManager) {
            @Override
            public void onLoadMore(int current_page) {
                loadMoreData();
            }
        });
        loadData();
    }

    private void loadMoreData() {
        currentPage++;
        loadData();
        mProgressBar.setVisibility(RecyclerView.VISIBLE);
    }

    private void loadData() {
        // example
        // at first load : currentPage = 0 -> we startAt(0 * 10 = 0)
        // at second load (first loadmore) : currentPage = 1 -> we startAt(1 * 10 = 10)

                mDatabase.child("Noticia")
            .limitToFirst(TOTAL_ITEM_EACH_LOAD)
            .startAt(currentPage*TOTAL_ITEM_EACH_LOAD)
            .orderByChild("descripcion").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(!dataSnapshot.hasChildren()){
                            Toast.makeText(MainActivity.this, "No more questions", Toast.LENGTH_SHORT).show();
                            currentPage--;
                        }
                        for (DataSnapshot data : dataSnapshot.getChildren()) {
                            Noticia noticia = data.getValue(Noticia.class);
                            noticia.setId(data.getKey());
                            listaNoticias.add(noticia);
                            adapter1.notifyDataSetChanged();
                            Toast.makeText(MainActivity.this,"Page:"currentPage,Toast.LENGTH_SHORT).show();
                        }
                        mProgressBar.setVisibility(RecyclerView.GONE);
                    }

                    @Override public void onCancelled(DatabaseError databaseError) {
                        mProgressBar.setVisibility(RecyclerView.GONE);
                    }});
    }


}

0 个答案:

没有答案