我想将文本消息发送给数据库中存在的数字。 当在imageView上点击时,应该发送味精。 以下代码位于一个称为Tab33的片段中。 我只是共享ImageView的onClick方法
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.imageViewPayments:
Boolean b = checkPermission();
if(b==true) {
DBRef.child("Members").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String phn = dataSnapshot.child("memberPhone").getValue(String.class);
String no = "+91 ";
String cat = no.concat(phn);
sms.sendTextMessage(cat, null, "Hey there! We know you enjoy Canteen Food but nobody likes to be in debt and that too for food. Please" +
" pay your Canteen Bill to the earliest and stay a Happy Tummy!", null, null);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Toast.makeText(getContext(),"Message Sent Successfully",Toast.LENGTH_LONG).show();
}else{
Snackbar.make(getView(), "Permission to send message NOT granted!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
return;
case R.id.imageViewToday:
return;
case R.id.imageViewCoupon:
return;
case R.id.imageViewComposeMessage:
Snackbar.make(getView(), "Compose Message!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
return;
}
}
private boolean checkPermission()
{
String permission = Manifest.permission.SEND_SMS;
int res = getContext().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
其中DBRef= FirebaseDatabase.getInstance().getReference();
和sms = SmsManager.getDefault();
Toast出现,然后应用崩溃。
答案 0 :(得分:0)
当您使用ValueEventListener
加载节点时,您在onDataChange
中获得的快照将包含所有匹配的子节点。要访问各个用户节点,您将需要遍历dataSnapshot.getChildren()
。
类似的东西:
DBRef.child("Members").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String phn = childSnapshot.child("memberPhone").getValue(String.class);
System.out.println(phn)
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore errors!
}