T3rrance Low
T3rrance Low

Reputation: 57

Sometimes after clicking a Button, text disappears, sometimes I have to click twice before it works

I don't know what's the problem with this button. After clicking the button, the text will disappear and after I click twice it starts working. All buttons are the same and after clicking one time the text disappears... do I need to re-do the project?

Image

Here is the button Onclick.

@Override
    public void onClick(View v) {
        Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isInserted = myDb.insertData(e_Txn.getText().toString(),
                        e_Name.getText().toString(),
                        e_Amount.getText().toString(),
                        Display_date.getText().toString().trim(),
                        e_Description.getText().toString(),
                        Description.getSelectedItem().toString());

                if (isInserted == true) {
                    Toast.makeText(Donation.this, "Data Inserted", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(Donation.this, "Data not Inserted", Toast.LENGTH_LONG).show();
                }


            }
        });
        New.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                e_Name.getText().clear();
                e_Amount.getText().clear();
                e_Txn.getText().clear();
                e_Description.getText().clear();
            }
        });
    }

I opened a method for "SetOnclick"

private void setOnClick() {
        this.New.setOnClickListener(this);
        this.Save.setOnClickListener(this);
        this.Print.setOnClickListener(this);
        this.Back.setOnClickListener(this);
    }

Upvotes: 0

Views: 649

Answers (3)

Pei
Pei

Reputation: 11643

So as per my reading, you're setting click listeners again in onClick() method which is called when one of 4 buttons you're setting click listeners in setOnClick() is clicked. For example, you have to click twice on New button to be able to do what you want.

To fix this, I'd suggest to do like following:

@Override
public void onClick(View v) {
    if (v == Save) {
        boolean isInserted = myDb.insertData(e_Txn.getText().toString(),
                e_Name.getText().toString(),
                e_Amount.getText().toString(),
                Display_date.getText().toString().trim(),
                e_Description.getText().toString(),
                Description.getSelectedItem().toString());
        if (isInserted == true) {
            Toast.makeText(Donation.this, "Data Inserted", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(Donation.this, "Data not Inserted", Toast.LENGTH_LONG).show();
        }
    }
    else if {v == New) {
        e_Name.getText().clear();
        e_Amount.getText().clear();
        e_Txn.getText().clear();
        e_Description.getText().clear();
     }
}

The other method should be the same.

The reason of text disappearing isn't related to the code you posted here I think. Maybe that's caused from the other parts like styles or layouts, etc.

Upvotes: 0

Anisur Rahman Tonu
Anisur Rahman Tonu

Reputation: 444

You should write onclick method as follows

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.Save://relpace Save with the button id
            boolean isInserted = myDb.insertData(e_Txn.getText().toString(),
                    e_Name.getText().toString(),
                    e_Amount.getText().toString(),
                    Display_date.getText().toString().trim(),
                    e_Description.getText().toString(),
                    Description.getSelectedItem().toString());

            if (isInserted == true) {
                Toast.makeText(Donation.this, "Data Inserted", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(Donation.this, "Data not Inserted", Toast.LENGTH_LONG).show();
            }

            break;

        case R.id.New://relpace New with the button id
            e_Name.getText().clear();
            e_Amount.getText().clear();
            e_Txn.getText().clear();
            e_Description.getText().clear();
            break;


        default:
            break;
    }

}

Upvotes: 1

GianhTran
GianhTran

Reputation: 3711

I don't know the root cause but I think you have wrong approach with your code, please do not setOnClickListener in onClick() method.

Let's make it simple

private void setOnClick() {
        Save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isInserted = myDb.insertData(e_Txn.getText().toString(),
                        e_Name.getText().toString(),
                        e_Amount.getText().toString(),
                        Display_date.getText().toString().trim(),
                        e_Description.getText().toString(),
                        Description.getSelectedItem().toString());

                if (isInserted == true) {
                    Toast.makeText(Donation.this, "Data Inserted", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(Donation.this, "Data not Inserted", Toast.LENGTH_LONG).show();
                }


            }
        });
        New.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                e_Name.getText().clear();
                e_Amount.getText().clear();
                e_Txn.getText().clear();
                e_Description.getText().clear();
            }
        });
    }

Upvotes: 1

Related Questions