Button doesn't intent after clicked

时间:2016-10-20 18:38:02

标签: android android-intent firebase firebase-realtime-database

What I'm trying to do was, trying to get data to firebase after pressing the button update. but it doesn't intent to next activity. it just stays right there and nothing happened. Is there something I'm missing on part of firebase codes?

public class Details extends AppCompatActivity implements View.OnClickListener {
    EditText name, matric, ic, phone, department, roomBlock, roomLevel, roomNumber;
    Button update;
    private FirebaseAuth auth;
    private DatabaseReference mDatabase;
    String mUserId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        name = (EditText)findViewById(R.id.editText2);
        matric = (EditText)findViewById(R.id.editText3);
        ic = (EditText)findViewById(R.id.editText4);
        phone = (EditText)findViewById(R.id.editText5);
        department = (EditText)findViewById(R.id.editText6);
        roomBlock = (EditText)findViewById(R.id.editText7);
        roomLevel = (EditText)findViewById(R.id.editText8);
        roomNumber = (EditText)findViewById(R.id.editText9);

        update = (Button)findViewById(R.id.btnupdate);

        auth = FirebaseAuth.getInstance();

        mDatabase = FirebaseDatabase.getInstance().getReference();
        mUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();

    }

    @Override
    public void onClick(View view){
        if(view == update){
            final String key = mDatabase.child("data").push().getKey();
            final String name1 = name.getText().toString().trim();
            final String matric1 = matric.getText().toString().trim();
            final String ic1 = ic.getText().toString().trim();
            final String phone1 = phone.getText().toString().trim();
            final String department1 = department.getText().toString().trim();
            final String roomBlock1 = roomBlock.getText().toString().trim();
            final String roomLevel1 = roomLevel.getText().toString().trim();
            final String roomNumber1 = roomNumber.getText().toString().trim();

            Item newItem = new Item(name1, matric1, ic1, phone1, department1, roomBlock1, roomLevel1, roomNumber1);
            Map<String, Object> itemValues = newItem.toMap();
            Map<String, Object> childUpdates = new HashMap<>();
            childUpdates.put("/data/" + mUserId + "/" + key, itemValues);
            mDatabase.updateChildren(childUpdates);

            Toast.makeText(Details.this, "Details Updated!", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Details.this, DoorActivity.class);
            startActivity(intent);
        }

    }
}

3 个答案:

答案 0 :(得分:0)

You haven't set onClickListener to your Button.

update = (Button)findViewById(R.id.btnupdate);
update.setOnClickListener(this);

答案 1 :(得分:0)

I guess your problem is here:

if(view == update){

you could do it for example like this:

    @Override
   public void onClick(View view){

    switch(view.getId()){

    case(R.id.btnupdate):

    //do you stuff here

    break;

    }   
  }

You are comparing the view to your button, but you should do it by the method getId().

Also you can do it without implementing OnClickListener in your activity and set it to every button like:

 update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              //do your stuff
            }
        });

答案 2 :(得分:0)

add clickListener to your button

yourButton.setOnClickListener((this);

and in the onClick

    public void onClick(View v) {
    if(v.getId==R.id.yourButtonId){
       // Perform action here

    }
}            
 });

good luck