- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的第二个问题,提前致谢。
问题基本上是无法将 qrCode(EditText) 字段的值传递到 myRef DatabaseReference 中,以便该值以(例如)“1-Breakfast”、“2-Breakfast”和等等...
数据库图像:
我尝试通过创建一个方法将值作为 return String.valueOf(qrCodes) 传递,但它使它不断崩溃。我们的应用程序不断崩溃。此外,该值不断以 null 形式传入。
package com.vidmun.dhananjay.qrscanner;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.ChildEventListener;
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.ValueEventListener;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import static android.content.ContentValues.TAG;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button buttonScan;
private Button delete;
private EditText qrCode;
private static final String TAG = MainActivity.class.getSimpleName();
private IntentIntegrator qrScan;
FirebaseDatabase database = FirebaseDatabase.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myRef.addChildEventListener(new MyChildEventListener());
delete = (Button) findViewById(R.id.delete);
buttonScan = (Button) findViewById(R.id.buttonScan);
qrCode = (EditText) findViewById(R.id.qrCode);
qrScan = new IntentIntegrator(this);
buttonScan.setOnClickListener(this);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteValueFromDb();
}
});
}
//Getting the scan results
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
//if qrcode has nothing in it
if (result.getContents() == null) {
Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
} else {
//if qr contains data
try {
//converting the data to json
JSONObject obj = new JSONObject(result.getContents());
} catch (JSONException e) {
e.printStackTrace();
//if control comes here that means the encoded format not matches
//in this case you can display whatever data is available on the qrcode
qrCode.setText(result.getContents());
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onClick(View view) {
//initiating the qr code scan
qrScan.initiateScan();
}
DatabaseReference myRef = database.getReference(String.valueOf(qrCode));
public void deleteValueFromDb(){
myRef.child("0").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
dataSnapshot.getRef().removeValue();
Toast.makeText(getApplicationContext() , "Can Eat", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext() , "Already Eaten", Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
}
class MyChildEventListener implements ChildEventListener {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "childAdded " + dataSnapshot.toString());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "childChanged " + dataSnapshot.toString());
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.i(TAG, "childRemoved " + dataSnapshot.toString());
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log.i(TAG, "childMoved " + dataSnapshot.toString());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.i(TAG, "childMoved " + databaseError.getMessage());
}
}
最佳答案
您似乎正在尝试以字符串形式获取文本字段的值:
private EditText qrCode
调用String.valueOf(qrCode)
只会将EditText
本身转换为字符串,而不是其值。
要从编辑文本中获取值:
qrCode.getText().toString()
例如:
DatabaseReference myRef = database.getReference(qrCode.getText().toString())
关于java - 无法将 result.getContents() 中的文本值传递到 myRef DatabaseReference 中以删除相应的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58320817/
直接调用 ref 收到的数据有什么区别: console.log(firebase.db().ref('类别')) 与通过 value 事件中的 dataSnapShot 接收的数据相比,如下所示:
这是我的第二个问题,提前致谢。 问题基本上是无法将 qrCode(EditText) 字段的值传递到 myRef DatabaseReference 中,以便该值以(例如)“1-Breakfast”、
自 https://www.firebase.com/blog/2014-10-03-major-updates-to-firebase-user-auth.html ,在使用 myRef authU
我是一名优秀的程序员,十分优秀!