我想根据特定条件设置TextView的文本。我正在从Firebase中获取一个值,并将其存储在名为pestBinaryValue的变量中。获取的值是1或0。基于此值,我想更改TextView的文本。有人可以帮我解决如何实现这一目标...
这是JAVA代码
package com.example.android.greenharvest;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class PestDetails extends AppCompatActivity {
TextView pestUpdate;
DatabaseReference firebase;
public String pestBinaryValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pest_details);
pestUpdate = findViewById(R.id.textViewPest);
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
String userid=user.getUid();
firebase = FirebaseDatabase.getInstance().getReference("/0/users").child(userid);
firebase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
pestBinaryValue = dataSnapshot.child("pest").getValue().toString();
Toast.makeText(PestDetails.this,pestBinaryValue,Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
if(pestBinaryValue=="1"){
pestUpdate.setText("Pest Has Been Detected!");
}
if(pestBinaryValue=="0"){
pestUpdate.setText("No Pests!");
}
}
}
这是XML代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pest_details">
<include
android:id="@+id/include"
layout="@layout/toolbar_layout" />
<TextView
android:id="@+id/profile_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="@string/pest_details"
android:textAlignment="center"
android:textColor="@color/colorHomescreen"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.487"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/include"
app:layout_constraintVertical_bias="0.26"
app:srcCompat="@drawable/baseline_bug_report_black_18dp" />
<TextView
android:id="@+id/textViewPest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Pest is present!"
android:textColor="#000"
android:textSize="26dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/include" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
pestBinaryValue=="1"
这是不正确的语法。
pestBinaryValue.equals("1") //is proper.
pestBinaryValue == 1 //this is for an int value.
答案 1 :(得分:1)
如果存储在数据库表中的值已经是Boolean类型,则还可以更改以下几行:
>public String pestBinaryValue;
>pestBinaryValue = dataSnapshot.child("pest").getValue().toString();
>if(pestBinaryValue=="1"){
pestUpdate.setText("Pest Has Been Detected!");
}
if(pestBinaryValue=="0"){
pestUpdate.setText("No Pests!");
}
对此:
>public Boolean pestBinaryValue;
>pestBinaryValue = dataSnapshot.child("pest").getValue();
>if(pestBinaryValue)
pestUpdate.setText("Pest Has Been Detected!");
else
pestUpdate.setText("No Pests!");