我无法从Firebase数据库中检索数据

时间:2018-04-24 19:13:34

标签: android firebase listview firebase-realtime-database

我发布此问题是因为我找不到任何与我的问题相关的正确解决方案。

我正在尝试从Firebase检索我的传感器数据,但我无法做到。请指出我在哪里做错了

Try.java

public class Try extends AppCompatActivity {

Button button;

ListView list;
DatabaseReference databaseTry;

List<TryObjectClass> tryObjectList;

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

    databaseTry = 
FirebaseDatabase.getInstance().getReference("sensor_data");

    button = (Button) findViewById(R.id.button);
    list = (ListView) findViewById(R.id.list);
    //list.setVisibility(View.INVISIBLE);

    tryObjectList = new ArrayList<>();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}

@Override
protected void onStart() {
    super.onStart();

    databaseTry.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            tryObjectList.clear();

            for (DataSnapshot TrySnapshot : dataSnapshot.getChildren()){
                TryObjectClass tryObjectClass = 
TrySnapshot.getValue(TryObjectClass.class);

                tryObjectList.add(tryObjectClass);
            }

            TryAdapter adapter = new TryAdapter(Try.this, tryObjectList);
            list.setAdapter(adapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });


}
}

这是我的对象类

TryObjectClass.java

public class TryObjectClass  {

private String date;
private String time;
private String humidity;
private String motion;
private String distance;
private String temperature;



public TryObjectClass(){

}

public TryObjectClass(String date, String time, String humidity, String 
motion, String distance, String temperature) {
    this.date = date;
    this.time = time;
    this.humidity = humidity;
    this.motion = motion;
    this.distance = distance;
    this.temperature = temperature;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getHumidity() {
    return humidity;
}

public void setHumidity(String humidity) {
    this.humidity = humidity;
}

public String getMotion() {
    return motion;
}

public void setMotion(String motion) {
    this.motion = motion;
}

public String getDistance() {
    return distance;
}

public void setDistance(String distance) {
    this.distance = distance;
}

public String getTemperature() {
    return temperature;
}

public void setTemperature(String temperature) {
    this.temperature = temperature;
}
}

这是我的适配器类

TryAdapter.java

public class TryAdapter extends ArrayAdapter<TryObjectClass> {

private Activity context;
private List<TryObjectClass> objectList;

public TryAdapter(@NonNull Activity context, List<TryObjectClass> 
objectList) {
    super(context, R.layout.new_list,0);
    this.context = context;
    this.objectList = objectList;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull 
ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();

    View listViewItem = inflater.inflate(R.layout.new_list,null,true);

    TextView date = (TextView) listViewItem.findViewById(R.id.date);
    TextView distance = (TextView) 
listViewItem.findViewById(R.id.distance);
    TextView humidity = (TextView) 
listViewItem.findViewById(R.id.humidity);
    TextView motion = (TextView) listViewItem.findViewById(R.id.motion);
    TextView temperature = (TextView) 
listViewItem.findViewById(R.id.temperature);
    TextView time = (TextView) listViewItem.findViewById(R.id.time);

    TryObjectClass tryObjectClass = objectList.get(position);

    date.setText(tryObjectClass.getDate());
    distance.setText(tryObjectClass.getDistance());
    humidity.setText(tryObjectClass.getHumidity());
    motion.setText(tryObjectClass.getMotion());
    temperature.setText(tryObjectClass.getTemperature());
    time.setText(tryObjectClass.getTime());

    return super.getView(position, convertView, parent);
}
}

try_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context=".Try"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your Retrieved data"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
    android:padding="10dp"
    android:layout_marginLeft="90dp"
    android:layout_marginTop="20dp"/>

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="click here to retrieve the data"/>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

这是我的清单项目

new_list.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date -"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="#000"
            android:padding="10dp"
            android:layout_marginLeft="20dp"/>
        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Distance -"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="#000"
            android:padding="10dp"
            android:layout_marginLeft="20dp"/>
        <TextView
            android:id="@+id/distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Humidity -"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="#000"
            android:padding="10dp"
            android:layout_marginLeft="20dp"/>
        <TextView
            android:id="@+id/humidity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Motion -"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="#000"
            android:padding="10dp"
            android:layout_marginLeft="20dp"/>
        <TextView
            android:id="@+id/motion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Temperature -"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="#000"
            android:padding="10dp"
            android:layout_marginLeft="20dp"/>
        <TextView
            android:id="@+id/temperature"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Time -"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="#000"
            android:padding="10dp"
            android:layout_marginLeft="20dp"/>
        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>

    </LinearLayout>

</LinearLayout>

</ScrollView>

这是我的数据库

smartborder-cef61
sensor_data
    100
        date: 1875
        distance: "50M"
        humidity:  4573
        motion: "yes"
        temperature: "30c"
        time: 7377200
    200
        date: 23111996
        distance: "50M"
        humidity: 45
        motion: "yes"
        temperature: "30C"
        time: 73775

2 个答案:

答案 0 :(得分:0)

我找到了一些东西,试着将你的setAdapter() 放在外面你的听众。 内部您的监听器,您将输入以下代码:

public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot TrySnapshot : dataSnapshot.getChildren()){
                TryObjectClass tryObjectClass = TrySnapshot.getValue(TryObjectClass.class);
                tryObjectList.add(tryObjectClass);
            }
            adapter.notifyDataSetChanged();
}

并尝试将您声明的内容放在onCreate方法中,就像您的ValueEventListener一样,这样您就可以在onStart中添加它并在onDestroy中将其删除,当您的活动无法收听时当您的活动被销毁时,不再发生变化。这是我的片段ContatsFragment的一部分,它运行正常:

public class ContatsFragment extends Fragment {

    private ListView listView;
    private ArrayAdapter adapter;
    private ArrayList<Contat> contats;
    private DatabaseReference databaseReference;
    private ValueEventListener valueEventListenerContatos;
    private Context context;

    public ContatsFragment() {
        // Required empty public constructor
    }


    @Override
    public void onStart() {
        super.onStart();
        context = getActivity();
        databaseReference.addValueEventListener(valueEventListenerContatos);
    }

    @Override
    public void onStop() {
        super.onStop();
        databaseReference.removeEventListener(valueEventListenerContatos);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contats = new ArrayList<>();
        View view = inflater.inflate(R.layout.fragment_contats, container, false);

        listView = (ListView) view.findViewById(R.id.lv_contatos);

        adapter = new ContatAdapter(getActivity(), contats);
        listView.setAdapter(adapter);

        Preferences preferences = new Preferences(getActivity());
        String loggedUserId = preferences.getLoggedUserId();

        databaseReference = FirebaseConfig.getFirebaseReference();
        databaseReference = databaseReference.child("contats")
                .child(loggedUserId);

        valueEventListenerContatos = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                contats.clear();
                for (DataSnapshot dados : dataSnapshot.getChildren()) {
                    Contat contat = dados.getValue(Contat.class);
                    contats.add(contat);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        };

        return view;
    }
}

答案 1 :(得分:0)

我有解决方案......我在给出列表对象名称时犯了错误......