我希望变量一,二和三能够在扫描QR码时从我的数据库中读取但我不能让它们显示在Builder.setMessage()行中...我试过调用displayDeals ()方法在几个地方但没有工作...
package com.example.darren.offerapp;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class PopUpPage extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
private FirebaseAuth firebaseAuth;
private FirebaseUser firebaseUser;
private DatabaseReference databaseReference;
private String dealID;
private Deals_Information deals_information;
private String one = "";
private String two = "";
private String three = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pop_up_page);
//stuff for popup window
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*0.8), (int)(height*0.6));
//calling the method which will pull the deals from the database
//displaySuggestion();
firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = firebaseAuth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference();
dealID = firebaseUser.getUid();
displayDeals();
}
public void displayDeals(){
databaseReference.child("FruitDeals").child(dealID).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
deals_information = dataSnapshot.getValue(Deals_Information.class);
one = deals_information.getDeal();
two = deals_information.getPrice();
three = deals_information.getAisleNum();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void onClick(View v){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(Result result) {
//do anything with result here
Log.w("handleResult", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Your Deal");
builder.setMessage("Offer: " + one + "\r\n\r\nPrice: " + two + "\r\n\r\nAisle Number: " + three + "");
AlertDialog alertDialog = builder.create();
alertDialog.show();
//resume scanning
//mScannerView.resumeCameraPreview(this); //uncomment out this line when you want to scan again
}
}
Deals_Information类在这里:
package com.example.darren.offerapp;
公共类Deals_Information {
private String deal;
private String price;
private String aisleNum;
//getters and setters
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAisleNum() {
return aisleNum;
}
public void setAisleNum(String aisleNum) {
this.aisleNum = aisleNum;
}
public String getDeal() {
return deal;
}
public void setDeal(String deal) {
this.deal = deal;
}
public Deals_Information(){
}
public Deals_Information(String deal, String price, String aisleNum)
{
this.deal = deal;
this.price = price;
this.aisleNum = aisleNum;
}
}