英文:
I can't retrieve data from Firebase database by user input to TextView
问题
以下是您提供的代码的翻译:
public class TransPenjFragment extends Fragment {
// ... (省略了一些变量的声明)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_trans_penj, container, false);
// ... (省略了一些控件的初始化)
FirebaseApp.initializeApp(getActivity());
firedb = FirebaseDatabase.getInstance();
myRef = firedb.getReference();
btnCek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String namabrg = edtTextnamabrg.getText().toString().trim();
myRef = firedb.getInstance().getReference().child("Barang");
Query check = myRef.orderByChild("nama").equalTo(namabrg);
check.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
BarangDB barang = snapshot.getValue(BarangDB.class);
String namabrgfromDB = snapshot.child(namabrg).child("nama").getValue(String.class);
if(namabrgfromDB.equals(namabrg)){
tvkdbrg.setText(barang.getKode());
tvhrgbrg.setText(barang.getHarga());
}else{
Snackbar.make(btnCek, "Barang yang dimasukkan tidak ada di daftar barang",
Snackbar.LENGTH_LONG).show();
}
}else{
Snackbar.make(btnCek, "Tidak tersedia",
Snackbar.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getActivity(), error.getDetails() + " " + error.getMessage(), Toast.LENGTH_LONG).show();
}
});
String jmlbeli = edtTextjmlbeli.getText().toString().trim();
double jb = Double.parseDouble(jmlbeli);
double hb = Double.parseDouble(tvhrgbrg.getText().toString());
double total = (jb * hb);
totalpj.setText(" " + total);
}
});
}
// ... (省略了其他部分)
}
public class BarangDB implements Serializable {
// ... (省略了变量声明和 getter、setter 方法)
@Override
public String toString() {
return " " + kode + "\n" + " " + nama + "\n" + " " + harga + "\n" + " " + stok;
}
}
日志:
2020-07-25 23:07:30.250 20490-20490/com.example.aplikasiskripsi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aplikasiskripsi, PID: 20490
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.example.aplikasiskripsi.TransPenjFragment$1.onClick(TransPenjFragment.java:120)
at android.view.View.performClick(View.java:6605)
at android.view.View.performClickInternal(View.java:6582)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25897)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
请注意,上述翻译是直接从您提供的内容中翻译的,其中包括了变量名、注释等。如果您需要关于特定代码部分的进一步解释或问题,可以随时询问。
英文:
Anyone help me please. I don't understand what I should to do to solve my problem.
I am not able to retrieve data from Firebase database. I am using Java language in android studio. I want to showing "Kode barang" which in database is "kode" and "Harga Barang" which in database is "harga". All of them will show by user input "Nama barang" which in database is "nama".
If I'm trying to input data in "Nama barang" which is the same as data value in "nama" in database and after that I'm input "jumlah beli" (in english: quantity) and then click "Cek" (english: check"), my app get forced close.
This is fragment class
public class TransPenjFragment extends Fragment {
TextView autotgl;
TextView tvkdbrg;
TextView tvhrgbrg;
TextView totalpj;
EditText edtTextnamabrg;
EditText edtTextjmlbeli;
Button btnCek, btnProses, btnHapus;
private FirebaseDatabase firedb;
private DatabaseReference myRef;
private ArrayList<BarangDB> daftarbarang;
private PenjualanAdapter adapter;
private TabLayout tabLayout;
private ViewPager viewPager;
private ValueEventListener valueEventListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_trans_penj, container, false);
tabLayout = view.findViewById(R.id.tabLayout);
viewPager = view.findViewById(R.id.viewPager);
autotgl = view.findViewById(R.id.autotgl);
tvkdbrg = view.findViewById(R.id.tvkdbrg);
tvhrgbrg = view.findViewById(R.id.tvhrgbrg);
totalpj = view.findViewById(R.id.total);
edtTextnamabrg = view.findViewById(R.id.edtTextnamabrg);
edtTextjmlbeli = view.findViewById(R.id.edtTextjmlbeli);
btnCek = view.findViewById(R.id.btnCek);
btnProses = view.findViewById(R.id.btnProses);
btnHapus = view.findViewById(R.id.btnHapus);
FirebaseApp.initializeApp(getActivity());
firedb = FirebaseDatabase.getInstance();
myRef = firedb.getReference();
btnCek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String namabrg = edtTextnamabrg.getText().toString().trim();
myRef = firedb.getInstance().getReference().child("Barang");
Query check = myRef.orderByChild("nama").equalTo(namabrg);
check.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if(snapshot.exists()){
BarangDB barang = snapshot.getValue(BarangDB.class);
String namabrgfromDB = snapshot.child(namabrg).child("nama").getValue(String.class);
if(namabrgfromDB.equals(namabrg)){
tvkdbrg.setText(barang.getKode());
tvhrgbrg.setText(barang.getHarga());
}else{
Snackbar.make(btnCek, "Barang yang dimasukkan tidak ada di daftar barang",
Snackbar.LENGTH_LONG).show();
}
}else{
Snackbar.make(btnCek, "Tidak tersedia",
Snackbar.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getActivity(), error.getDetails() + " "
+ error.getMessage(), Toast.LENGTH_LONG).show();
}
});
String jmlbeli = edtTextjmlbeli.getText().toString().trim();
double jb =Double.parseDouble(jmlbeli);
double hb = Double.parseDouble(tvhrgbrg.getText().toString());
double total = (jb*hb);
totalpj.setText(" "+total);
}
});
edtTextnamabrg
is editText
to input "Nama barang" from user to be the same as "nama" in database.
tvkdbrg
is textView
to retrieve data "Kode Barang" or "kode" from database.
tvhrgbrg
is textView
to retrieve data "Harga barang" or "harga" from database.
This is my database model
public class BarangDB implements Serializable {
private String kode;
private String nama;
private String harga;
private String stok;
private String key;
public BarangDB(){ }
public BarangDB (String kode, String nama, String harga, String stok){
this.kode = kode;
this.nama = nama;
this.harga = harga;
this.stok = stok;
}
public String getKey() { return key;}
public void setKey(String key) { this.key = key; }
public String getKode() { return kode;}
public void setKode(String kode) { this.kode = kode; }
public String getNama() { return nama; }
public void setNama(String nama) { this.nama = nama; }
public String getHarga() { return harga; }
public void setHarga(String harga) { this.harga = harga; }
public String getStok() { return stok; }
public void setStok(String stok) { this.stok = stok; }
@Override
public String toString() {
return " "+kode+"\n" + " "+nama+"\n" + " "+harga+ "\n" + " "+stok;
}
}
Logcat
2020-07-25 23:07:30.250 20490-20490/com.example.aplikasiskripsi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aplikasiskripsi, PID: 20490
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at com.example.aplikasiskripsi.TransPenjFragment$1.onClick(TransPenjFragment.java:120)
at android.view.View.performClick(View.java:6605)
at android.view.View.performClickInternal(View.java:6582)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25897)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
答案1
得分: 0
你正在尝试提取位于 Barang
键下的具有键值对 nama: {namabrg的值}
的子项。请注意,snapshot
中的结果与您的 BarangDB
类结构不匹配。snapshot
将返回以下结构:
这意味着您的 BarangDB
类必须拥有另一个类型为 Energen
的变量,它将包含其他变量(mode、harga、nama、stok)。
我看到您部分地理解了这一点,几乎做对了。
您需要更改两行代码。从这个:
BarangDB barang = snapshot.getValue(BarangDB.class);
String namabrgfromDB = snapshot.child(namabrg).child("nama").getValue(String.class);
改为这个:
BarangDB barang = snapshot.child(namabrg).getValue(BarangDB.class);
String namabrgfromDB = barang.getNama();
这应该就足够了。
英文:
You are trying to extract a child under Barang
key which has key-value pair nama: {value of namabrg}
. Note that the result in snapshot
does not match your BarangDB
class structure. snapshot
will return you the next structure:
It means that your BarangDB
class must hold another variable of type Energen
, which will hold other variables (mode, harga, nama, stok).
And I see you partially understand that and almost got it right.
You need to change two lines. From this:
BarangDB barang = snapshot.getValue(BarangDB.class);
String namabrgfromDB = snapshot.child(namabrg).child("nama").getValue(String.class);
To this:
BarangDB barang = snapshot.child(namabrg).getValue(BarangDB.class);
String namabrgfromDB = barang.getNama();
And it should be enough.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论