gpt4 book ai didi

java - 无法将 result.getContents() 中的文本值传递到 myRef DatabaseReference 中以删除相应的键

转载 作者:行者123 更新时间:2023-12-01 23:23:05 25 4
gpt4 key购买 nike

这是我的第二个问题,提前致谢。

问题基本上是无法将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com