在firebase android中按值搜索时无法显示数据

时间:2018-06-07 11:46:16

标签: android firebase firebase-realtime-database onclicklistener event-listener

我有一个基于firebase的程序android。此程序用于在单击按钮搜索时显示数据。但是数据没有显示出来。

我认为问题出在函数reference.AddValueEventListener上。这是代码

package love.trykamus;

public class MainActivity extends AppCompatActivity {    
    private static final String TAG = "ViewDatabase";
    RecyclerView recyclerview;
    FirebaseDatabase database;
    DatabaseReference reference;
    Kamus kamus;
    List<Kamus> kamusList;
    EditText search;
    Button btn;
    TextView tvNgoko;
    private String key, kramal, kraming,ngoko;

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

        search = (EditText) findViewById(R.id.search);
        btn = (Button) findViewById(R.id.btn);
        tvNgoko = (TextView) findViewById(R.id.ngoko);
        kamus = new Kamus();
        database = FirebaseDatabase.getInstance();
        recyclerview = (RecyclerView) findViewById(R.id.rview);
        reference = database.getReference("employees");


        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                kamusList = new ArrayList<>();
                // StringBuffer stringbuffer = new StringBuffer();
                for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){

                    kamus = dataSnapshot1.getValue(Kamus.class);
                    String indonesia=kamus.getIndonesia();
                    String kramaAlus=kamus.getKramaAlus();
                    String kramaInggil=kamus.getKramaInggil();
                    String ngoko=kamus.getNgoko();
                    kamus.setIndonesia(indonesia);
                    kamus.setKramaAlus(kramaAlus);
                    kamus.setKramaInggil(kramaInggil);
                    kamus.setNgoko(ngoko);

                    kamusList.add(kamus);

                    // Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();

                }

                KamusAdapter recycler = new KamusAdapter(kamusList);
                RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(MainActivity.this);
                recyclerview.setLayoutManager(layoutmanager);
                recyclerview.setItemAnimator( new DefaultItemAnimator());
                recyclerview.setAdapter(recycler);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                //  Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  key = search.getText().toString();
                  kamus.setIndonesia(key);
                if(key == kamus.getIndonesia()){
                    reference.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            for(DataSnapshot ds :dataSnapshot.getChildren()){
                                kamus = ds.getValue(Kamus.class);
                                kamus.setKramaAlus(ds.child(key).getValue(Kamus.class).getKramaAlus());
                                kamus.setKramaInggil(ds.child(key).getValue(Kamus.class).getKramaInggil());
                                kamus.setKramaInggil(ds.child(key).getValue(Kamus.class).getNgoko());

                                tvNgoko.setText(kamus.getNgoko());

                                Log.d(TAG, "showData: name: " + kamus.getKramaAlus());
                                Log.d(TAG, "showData: name: " + kamus.getKramaInggil());
                                Log.d(TAG, "showData: name: " + kamus.getNgoko());
                            }

                        }

                        @Override
                        public void onCancelled(DatabaseError error) {
                            // Failed to read value
                            //  Log.w(TAG, "Failed to read value.", error.toException());
                        }

                    });
                }

            }
        });




    }

    @Override
    public void onStart() {
        super.onStart();
    }
    @Override
    public void onStop() {
        super.onStop();
    }
}

程序的结果是

06-07 15:04:45.484 24544-24544/love.trykamus E/AndroidRuntime: FATAL EXCEPTION: main
Process: love.trykamus, PID: 24544
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String love.trykamus.Kamus.getKramaAlus()' on a null object reference
    at love.trykamus.MainActivity$2$1.onDataChange(MainActivity.java:109)
    at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
    at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
    at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5233)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

这是 的 Kamus.class

package com.rifkyprayanta.kamusbahasa;

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

}

然后这就是firebase struktur

enter image description here

1 个答案:

答案 0 :(得分:0)

替换此代码......

  reference = database.getReference().child("employees");
  with      
  reference = database.getReference("employees");