简体   繁体   English

如何根据条件设置Textview?

[英]How can I set the Textview on the basis of a condition?

I want to set the text of a TextView on the basis of particular condition. 我想根据特定条件设置TextView的文本。 I am fetching a value from firebase and storing it in the variable called as pestBinaryValue. 我正在从Firebase中获取一个值,并将其存储在名为pestBinaryValue的变量中。 The value which is fetched is either a 1 or 0. On the basis of this value, I want to change the text of my TextView. 获取的值是1或0。基于此值,我想更改TextView的文本。 Could someone please help me out on how to implement this... 有人可以帮我解决这个问题吗...

Here's the JAVA code 这是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!");
        }

    }
}

Here's the XML code: 这是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>
pestBinaryValue=="1"

This is not proper syntax. 这是不正确的语法。

pestBinaryValue.equals("1") //is proper.
pestBinaryValue == 1 //this is for an int value.

If the value stored in the database table is already type Boolean you can also change these lines: 如果存储在数据库表中的值已经是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!");
        }

To this: 对此:

>public Boolean pestBinaryValue;
>pestBinaryValue = dataSnapshot.child("pest").getValue();
>if(pestBinaryValue)
            pestUpdate.setText("Pest Has Been Detected!");
 else
            pestUpdate.setText("No Pests!");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM