每次在Firebase DB

时间:2019-01-11 18:02:18

标签: java android firebase-realtime-database

每当我在firebase中更改数据时,每次都会创建新的Activity并刷新以显示许多同类活动。

检索页面::::::::::::::::::::::::

  ArrayList<String>p;
      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirmation);
    Intent intent =getIntent();
    mw1= intent.getStringExtra(collectpayment.msg1);
    p.add(mw1);
     mid1 = (TextView) findViewById(R.id.id1);
     mname= (TextView) findViewById(R.id.name);
     mhno=(TextView) findViewById(R.id.hno);
       // amt=(TextView) findViewById(R.id.id);
    //id1.setText(mw1);
    mOrder = (Button) findViewById(R.id.sp);
   mItemSelected = (TextView) findViewById(R.id.tvItemSelected);
    listItems = getResources().getStringArray(R.array.shopping_item);
    checkedItems = new boolean[listItems.length];
    mOrder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlertDialog.Builder mBuilder = new AlertDialog.Builder(confirmation.this);
            mBuilder.setTitle(R.string.dialog_title);
            mBuilder.setMultiChoiceItems(listItems, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int position, boolean isChecked) {
                    if(isChecked){
                        mUserItems.add(position);
                    }else{
                        mUserItems.remove((Integer.valueOf(position)));
                    }
                }
            });

            mBuilder.setCancelable(false);
            mBuilder.setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int which) {
                    String item = "";
                    for (int i = 0; i < mUserItems.size(); i++) {
                        item = item + listItems[mUserItems.get(i)];

                        if (i != mUserItems.size() - 1) {
                            item = item + ", ";
                        }
                        switch (listItems[mUserItems.get(i)]) {
                            case "star":
                                //Toast.makeText(getApplicationContext(), "star", Toast.LENGTH_LONG).show();
                                p.add("star");
                                break;
                            case "ZEE":
                                p.add("ZEE");
                                break;
                        }

                    }
                    mItemSelected.setText(item);

                }
            }).setNegativeButton(R.string.dismiss_label, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });


            mBuilder.setNeutralButton(R.string.clear_all_label, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int which) {
                    for (int i = 0; i < checkedItems.length; i++) {
                        checkedItems[i] = false;
                        mUserItems.clear();
                        mItemSelected.setText("");
                        amt.setText("");
                    }
                }
            });

            AlertDialog mDialog = mBuilder.create();
            mDialog.show();
        }
    });
}


@Override
public void onStart()
{
    super.onStart();
    mRef = new Firebase("url");
    myValueEventListner= new ValueEventListener(){

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ID = (String) dataSnapshot.child(mw1).child("SetTopBoxDetails").getValue();
            name=(String)dataSnapshot.child(mw1).child("CustomerName").getValue();
            hno=(String)dataSnapshot.child(mw1).child("HouseNo").getValue();
            mid1.setText(ID);
            mname.setText(name);
            mhno.setText(hno);



        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }


    };
    mRef.addListenerForSingleValueEvent(myValueEventListner);

}

@Override
public void onStop(){
    super.onStop();
    if (mRef != null && myValueEventListner != null){
    mRef.removeEventListener(myValueEventListner);}
}

public void sendsms(View view)
{
    for(String s: p)
    {
       sendingItems.add(s);

    }
    Intent intent=new Intent(this,BillGenerate.class);
    intent.putStringArrayListExtra(msg3, (ArrayList<String>)sendingItems);
    startActivity(intent);
    finish();


}

更新页面:::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bill_generate);
    Intent intent =getIntent();
    recieving=intent.getStringArrayListExtra(confirmation.msg3);
    mw2= recieving.get(0);

    for(int i=1;i<recieving.size();i++)
    {
        if(recieving.get(i).equals("star"))
            amount=amount+39;
        if(recieving.get(i).equals("ZEE"))
            amount=amount+20;
    }
    amount1=Integer.toString(amount);
    TextView amunt=(TextView)findViewById(R.id.amt);
    amunt.setText(amount1);
    gst=(amount*18)/100;
    total=amount+gst;
    TextView gt=(TextView)findViewById(R.id.gst);
     tot=(TextView)findViewById(R.id.tot);
    total1 = Integer.toString(total);
    gst1 = Integer.toString(gst);
    gt.setText(gst1);
    tot.setText(total1);
}

public void collect(View view) {

    mRef = new Firebase("url");

    for(int i=1;i<recieving.size();i++)
    {
        if(recieving.get(i).equals("star"))
            mRef.child(mw2).child("star").setValue("1");
        if(recieving.get(i).equals("ZEE"))
            mRef.child(mw2).child("ZEE").setValue("1");
    }

    Map map = new HashMap();
    map.put("timestamp", ServerValue.TIMESTAMP);
    mRef.child(mw2).updateChildren(map);



    String phoneNumber = "65435545";
    Uri uri = Uri.parse("smsto:" + phoneNumber);
    Intent sms = new Intent(Intent.ACTION_SENDTO, uri);
    String message = tot.getText().toString();
           sms.putExtra(msg4, message);
    String smsBody = "Your Bill amount is Rs." + message + " ThankYou for choosing us. For any Details Contact Us:84656865";
    sms.putExtra("sms_body", smsBody);

    Intent sms1 = new Intent(BillGenerate.this, MainActivity.class);
    sms1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(sms1);
    startActivity(sms);
    finish();

}

预期:我只需要更新数据库,而无需创建任何活动。

实际O / p:每当我进入更新页面并更新数据库时,上一个(或)检索页面都会以与已更新的子代数相同的号码来调用。

0 个答案:

没有答案