Firebase查询有时会起作用,有时则不起作用

时间:2017-06-30 15:11:30

标签: android firebase firebase-realtime-database event-listener

我是Firebase for Android的新用户。我正在制作拍卖应用程序。有一个屏幕,用户可以在其中输入其出价金额并提交。在同一个屏幕上,有一个TextView,显示目前在该项目上收到的当前最高出价。这正是我遇到问题的地方。我希望TextView动态更新。假设我处于投标提交屏幕,最高投标文本显示500(以任何货币假设)。假设其他人提交700的出价,我的屏幕上的文本视图应该更新。我尝试了一些东西,它适用于一定数量的出价,然后它停止更新。它卡在一个值。我不明白是什么导致故障。请告诉我是否已将事件监听器放在正确的地点。谢谢

public class BidMainScreen3 extends BaseActivity implements View.OnClickListener{

private DatabaseReference mRootDBRef;
private DatabaseReference mPostReference;
private Query highQuery;
private DatabaseReference mHighValue;
private Button mbidButton;
private TextView mHighest;
private EditText mbidAmount;
private String bidAmt;
private TextView mProdTitle;
private String mPostKey;
private String mTitle;
public String highAmount;



@Override
protected void onCreate(Bundle savedInstanceState)
{
   // savedInstanceState = null;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bidscreen);

    Bundle b = getIntent().getExtras();
    mPostKey = b.getString("PostKey");
    mTitle = b.getString("ProductTitle");
    highAmount = b.getString("HighAmount");



    mRootDBRef = FirebaseDatabase.getInstance().getReference();
    mPostReference = FirebaseDatabase.getInstance().getReference().child("Auctions").child(mPostKey);



    mbidButton = (Button) findViewById(R.id.bidBTN);
    mbidAmount = (EditText) findViewById(R.id.bidAmtET);
    mProdTitle = (TextView) findViewById(R.id.titleTV);
    mHighest = (TextView) findViewById(R.id.highestTV);


    mProdTitle.setText(mTitle);
    mHighest.setText(highAmount);



    mbidButton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            submitBid();
        }
    });


}





@Override
public void onClick(View v) {

}


public void submitBid()
{
    bidAmt = mbidAmount.getText().toString();
    final String userID = getUid();

    Toast.makeText(this, "Submitting bid...", Toast.LENGTH_SHORT).show();

    writeNewBid(userID,bidAmt);

    //Set editing enabled(true). Add this functionality later Refer to New Post Activity line 144


    finish();


}


private void writeNewBid(String userId, String bidAmt)
{
    String key = mRootDBRef.child("AuctionsNew").push().getKey();

    NewBid3 bid = new NewBid3(userId,bidAmt);

    Map<String, Object> postBidValues = bid.toMap();
    Map<String, Object> childUpdates = new HashMap<>();

    childUpdates.put("/AuctionsNew/" + mPostKey +"/" + key , postBidValues);

    mRootDBRef.updateChildren(childUpdates);
}


public void onStart() {
    super.onStart();
Query highVal = mRootDBRef.child("AuctionsNew").child(mPostKey).orderByChild("bidAmount").limitToFirst(1);
    highVal.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {


            String uid,bid;
            GenericTypeIndicator<Map<String,String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>(){};


            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);

            uid = map.get("uid");
            bid = map.get("bidAmount");


            mHighest.setText("Highest Bid Recieved : Rs." + bid);
            Log.v("E_Value","Printing from OnStart.OnchildAded");
            Log.v("E_Value","User ID(OSCA)"+uid);
            Log.v("E_Value","Amount(OSCA)"+bid);

        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            String uid,bid;

            GenericTypeIndicator<Map<String,String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>(){};


            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
            uid = map.get("uid");
            bid = map.get("bidAmount");


            mHighest.setText("Highest Bid Recieved : Rs." + bid);
            Log.v("E_Value","Printing from OnStart.OnchildChanged");
            Log.v("E_Value","User ID(OSCC)"+uid);
            Log.v("E_Value","Amount(OSCC)"+bid);




        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {


        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            String uid,bid;

            GenericTypeIndicator<Map<String,String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>(){};


            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);

            uid = map.get("uid");
            bid = map.get("bidAmount");


            mHighest.setText("Highest Bid Recieved : Rs." + bid);
            Log.v("E_Value","Printing from OnStart.OnchildMoved");
            Log.v("E_Value","User ID(OSCM)"+uid);
            Log.v("E_Value","Amount(OSCM)"+bid);



        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}}

0 个答案:

没有答案