Did I not set the method? Where should I connect the error code?

Asked 2 years ago, Updated 2 years ago, 44 views

public class MainActivity extends AppCompatActivity { Button button1; EditText edittext1;

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

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");

// button1 = (Button) findViewById(R.id.button1); // // edittext1 = (EditText) findViewById(R.id.edittext1);

    myRef.addValueEventListener(new ValueEventListener() {

        public void click1button (View v){
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference myRef = database.getReference("message");
            myRef.child("message").push().setValue(edittext1.getText().toString());
        }

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            TextView textview1 = (TextView)findViewById(R.id.textview1);
            // // This method is called once with the initial value and again
            // // whenever data at this location is updated.
            String value = dataSnapshot.getValue(String.class);
            textview1.setText(value); //database value
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // // Failed to read value
        }
    });

}
public void onClick(View v){
    Intent intent = new Intent(this, SubActivity.class);
    startActivity(intent);
}

}

<RatingBar
    android:id="@+id/ratingBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:isIndicator="false"
    android:max="5"
    android:numStars="5"
    android:rating="3"
    android:stepSize="0.5" />


<EditText
    android:id="@+id/edittext1"
    android:layout_width="391dp"
    android:layout_height="144dp"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Content" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="204dp"
        android:layout_height="100dp"
        android:onClick="onClick"
        android:text="OK" />

    <Button
        android:id="@+id/button2"
        android:layout_width="204dp"
        android:layout_height="100dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Temporary Created Area" />

</LinearLayout>

The public void click1button (View v) is not working on the I don't know where to connect...

If you look at the xml, there are a total of two buttons, but I didn't attach them to the article, but button 1 is set to display another activity screen.

Maybe that's why click1button should be responsible for receiving the value from the database, but it doesn't work at all... I'm following the example one by one, but it's hard ㅠ<

What I'm referring to is http://blog.naver.com/PostView.nhn?blogId=kkrdiamond77&logNo=221305647401&parentCategoryNo=&categoryNo=65&viewDate=&isShowPopularPosts=true&from=search This site... Thank you

android-studio

2022-09-21 11:37

1 Answers

I'm not sure which code it is, but click1button seems to be the click listener of the button, just like onClick. ValueEventListener Don't go inside, take it out.

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

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            TextView textview1 = (TextView)findViewById(R.id.textview1);
            // // This method is called once with the initial value and again
            // // whenever data at this location is updated.
            String value = dataSnapshot.getValue(String.class);
            textview1.setText(value); //database value
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // // Failed to read value
        }
    });

}

public void onClick(View v){
    Intent intent = new Intent(this, SubActivity.class);
    startActivity(intent);
}

public void click1button (View v){
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference("message");
    myRef.child("message").push().setValue(edittext1.getText().toString());
}

I don't know which button acts like the save button in the source article, but is it button2? Likewise, you can register as a click listener.

<Button
    android:id="@+id/button2"
    android:layout_width="204dp"
    android:layout_height="100dp"
    android:onClick="click1button"
    android:text="Button" />


2022-09-21 11:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.