在自定义recyclelerview中显示firebase数据

时间:2018-02-21 10:49:15

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

我想将firebase数据显示到自定义的recyclerview中。数据从arduino添加到firebase,我想在应用程序本身中显示。

我正在尝试运行以下代码,但应用程序只是打开并且不显示任何内容。每次应用程序在Android上运行时,它都会打开并且不会显示任何内容。相同的代码正在与我的其他2-3个项目一起使用,但在这种情况下似乎不起作用。

1.MainActivity.java

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private DatabaseReference myref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.mango);
        myref = FirebaseDatabase.getInstance().getReference().child("database").child("values");
        FirebaseRecyclerAdapter<valueretriever, ProgramViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<valueretriever, ProgramViewHolder>(
                valueretriever.class,
                R.layout.individual_row,
                ProgramViewHolder.class,
                myref
        ) {
            @Override
            protected void populateViewHolder(ProgramViewHolder viewHolder, valueretriever model, int position) {
                viewHolder.setMoisture(model.getMoisture());
                viewHolder.setMotor_time(model.getMotortime());
                viewHolder.setPower(model.getPower());
            }
        };
        recyclerView.setAdapter(recyclerAdapter);
    }
    public static class ProgramViewHolder extends RecyclerView.ViewHolder {
        View mView;
        TextView txtmoisture;
        TextView txtmotor_time;
        TextView txtpower;
        String moisturee, motortimee, powerr;
    public ProgramViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            txtmoisture = itemView.findViewById(R.id.textview1);
            txtmotor_time = itemView.findViewById(R.id.textview2);
            txtpower = itemView.findViewById(R.id.textview3);
        }


        public void setMoisture(String moisture) {
            txtmoisture.setText(moisture);
            moisturee = txtmoisture.getText().toString();
        }

        public void setMotor_time(String motor_time) {
            txtmotor_time.setText(motor_time);
            motortimee = txtmotor_time.getText().toString();
        }

        public void setPower(String power) {
            txtpower.setText(power);
            powerr = txtpower.getText().toString();
        }

    }
}

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mango"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true" /> 
</RelativeLayout>

3.valueretriever.java

public class valueretriever {
    private String moisture;
    private String motor_time;
    private String power;

    public valueretriever() {
    }
    public valueretriever(String moisture, String motor_time, String power)  {
        this.moisture = moisture;
        this.motor_time = motor_time;
        this.power = power;
    }
    public String getMoisture() {
        return moisture;
    }
    public void setMoisture(String moisture) {
        this.moisture = moisture;
    }
    public String getMotortime() {
        return motor_time;
    }
    public void setMotortime(String motor_time) {
        this.motor_time = motor_time;
    }
    public String getPower() {
        return power;
    }
    public void setPower(String power) {
        this.power = power;
    }
}  

4.individual_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:background="#282828"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_below="@+id/textview2"
        android:layout_centerHorizontal="true"
        android:gravity="center_vertical"
        android:textColor="#ffffff"
        android:textSize="50sp" />
    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:textColor="#ffffff"
        android:textSize="50sp" />
    <TextView
        android:id="@+id/textview3"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:gravity="center_vertical"
        android:textColor="#ffffff"
        android:textSize="50sp" />
</RelativeLayout>

0 个答案:

没有答案