FirebaseRecycleAdapter不会显示Firebase数据库中的数据

时间:2018-06-07 03:01:29

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我完成了显示数据的程序。数据将显示是否单击了按钮。此计划正在使用FirebaseFirebaseRecyclerAdapter。但它并没有在我的RecyclerView中显示数据。我不知道出了什么问题。

我认为功能KamusViewHolder存在问题,但我仍然不知道错误是什么。

JawaActivity.java

public class JawaActivity extends AppCompatActivity {

    private EditText mSearchField;
    private ImageButton mSearchButton;
    private RecyclerView mResultList;

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

        mKamusDatabase = FirebaseDatabase.getInstance().getReference("Employees");


        mSearchField = (EditText) findViewById(R.id.searchField);
        mSearchButton = (ImageButton) findViewById(R.id.searchButton);

        mResultList = (RecyclerView) findViewById(R.id.resultList);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));

        mSearchButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view){

                String searchText = mSearchField.getText().toString();

                firebaseKamusSearch(searchText);
            }

        });

    }

    private void firebaseKamusSearch(String searchText) {

        Toast.makeText(JawaActivity.this, "Mulai Mencari", Toast.LENGTH_LONG).show();

        Query query = mKamusDatabase.orderByChild("Employees").startAt(searchText).endAt(searchText);

        FirebaseRecyclerOptions<Kamus> options = new FirebaseRecyclerOptions.Builder<Kamus>().setQuery(query, Kamus.class).build();

        FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Kamus, KamusViewHolder>(options) {
            @Override
            public KamusViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                // Create a new instance of the ViewHolder, in this case we are using a custom
                // layout called R.layout.message for each item
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.teks_jawa, parent, false);

                return new KamusViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(KamusViewHolder holder, int position, Kamus model) {

                holder.Indonesia.setText(model.getIndonesia());
                holder.Kramainggil.setText(model.getKramainggil());
                holder.Kramaalus.setText(model.getKramaalus());
                holder.Ngoko.setText(model.getNgoko());
            }
        };

        mResultList.setAdapter(adapter);
    }

    public class KamusViewHolder extends RecyclerView.ViewHolder{

        View mView;
        TextView Indonesia;
        TextView Kramainggil;
        TextView Kramaalus;
        TextView Ngoko;
        public KamusViewHolder(View itemView)
        {
            super(itemView);
            mView = itemView;
            Indonesia = (TextView) mView.findViewById(R.id.Indonesia);
            Kramainggil = (TextView) mView.findViewById(R.id.Kramainggil);
            Kramaalus = (TextView) mView.findViewById(R.id.Kramaalus);
            Ngoko = (TextView) mView.findViewById(R.id.Ngoko);
        }

    }

}

Kamus.Java

public class Kamus {

public String indonesia;
public String kramaalus;
public String kramainggil;
public String ngoko;

public Kamus(){

}

public Kamus(String indonesia, String kramaalus, String kramainggil, String ngoko) {
    this.indonesia = indonesia;
    this.kramaalus = kramaalus;
    this.kramainggil = kramainggil;
    this.ngoko = ngoko;
}

public String getIndonesia() {
    return indonesia;
}

public void setIndonesia(String indonesia) {
    this.indonesia = indonesia;
}

public String getKramaalus() {
    return kramaalus;
}

public void setKramaalus(String kramaalus) {
    this.kramaalus = kramaalus;
}

public String getKramainggil() {
    return kramainggil;
}

public void setKramainggil(String kramainggil) {
    this.kramainggil = kramainggil;
}

public String getNgoko() {
    return ngoko;
}

public void setNgoko(String ngoko) {
    this.ngoko = ngoko;
}

}

2 个答案:

答案 0 :(得分:0)

请这样做

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

以及你的onStop

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}

持有者和绑定看起来没问题,您需要开始修改适配器以进行更改

答案 1 :(得分:0)

将这些添加到您的FirebaseRecyclerAdapter:

            @Override
            public void onDataChanged() {
                // If there is no data, show a view that invites the user to add a message.
            }

同样适用于您的活动onResume()onPause(),请确保您分别yourAdapter.startListening();yourAdapter.stopListening();