【发布时间】:2019-12-05 14:48:22
【问题描述】:
我首先构建了一个 ListView,它包含多个 没有 Firebase 的 TextView,并且运行良好(注意:我将数据存储在字符串数组中)。然后我用一个 TextView with Firebase 创建了一个 ListView (ArrayAdapter),效果也很好(注意:我将数据存储在一个 ArrayList 中)。但是一旦我将这两个项目合并在一起,它就不起作用了(注意:这个项目中存在多个 TextView,但我现在只是试图填充我的第一个 TextView)。我的应用会在尝试打开时立即关闭。
更新:在我使用 Firebase 创建一个带有单个 TextView 的 ListView (ArrayAdapter) 的单独项目中,我改用了 .notifyDataSetChanged()。当我在该项目中使用 .notify() 时,它不再起作用。我在这个项目中使用 .notify() 是否会导致问题?我似乎无法在这个项目中使用 .notfiyDataSetChanged。
这是我的包含 ListView 的 activity_main:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<ListView
android:id="@+id/eventsListView"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的 activity_list_view,它概述了 ListView 的视图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="vertical">
<TextView
android:id="@+id/eventTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:paddingTop="5dp"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/eventDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:textSize="16sp"
android:textStyle="italic" />
<TextView
android:id="@+id/eventDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
这是我的 MainActivity:
package com.example.firebase_listview_2;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.google.firebase.database.ChildEventListener;
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 java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView eventsList;
DatabaseReference dawnEvents;
ArrayList<String> titleArray = new ArrayList<>();
ArrayList<String> eventStartArray = new ArrayList<>();
ArrayList<String> eventFinishArray = new ArrayList<>();
ArrayList<String> eventGenderArray = new ArrayList<>();
ArrayList<String> eventAgeArray = new ArrayList<>();
ArrayList<String> descriptionArray = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListAdapter adapter = new com.example.firebase_listview_2.ListAdapter(this, titleArray, eventStartArray, eventFinishArray, eventGenderArray, eventAgeArray, descriptionArray);
eventsList = findViewById(R.id.eventsListView);
eventsList.setAdapter(adapter);
dawnEvents = FirebaseDatabase.getInstance().getReference("Timetable/Ladybridge High School, Bolton, UK/Date/2019-11-30/Events/Spanish Lesson");
dawnEvents.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String value = dataSnapshot.getValue(String.class);
titleArray.add(value);
adapter.notify(); // In the separate project where I created a ListView (ArrayAdapter) with a single TextView with Firebase, I used .notifyDataSetChanged() instead. When I use .notify() in that project, it no longer works. Is my use of .notify() in this project causing the issue? I cannot seem to use .notfiyDataSetChanged in this project.
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
这是我的 ListAdapter:
package com.example.firebase_listview_2;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final ArrayList<String> titleArray;
private final ArrayList<String> eventStartArray;
private final ArrayList<String> eventFinishArray;
private final ArrayList<String> eventGenderArray;
private final ArrayList<String> eventAgeArray;
private final ArrayList<String> descriptionArray;
public ListAdapter(Activity context, ArrayList<String> titleArray, ArrayList<String> eventStartArray, ArrayList<String> eventFinishArray, ArrayList<String> eventGenderArray, ArrayList<String> eventAgeArray, ArrayList<String> descriptionArray) {
super(context, R.layout.activity_list_view, titleArray);
this.context = context;
this.titleArray = titleArray;
this.eventStartArray = eventStartArray;
this.eventFinishArray = eventFinishArray;
this.eventGenderArray = eventGenderArray;
this.eventAgeArray = eventAgeArray;
this.descriptionArray = descriptionArray;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.activity_list_view, null, true);
TextView eventTitle = rowView.findViewById(R.id.eventTitle);
TextView eventDetails = rowView.findViewById(R.id.eventDetails);
TextView eventDescription = rowView.findViewById(R.id.eventDescription);
eventTitle.setText(titleArray.get(position));
String eventDetailsConcat = eventStartArray.get(position) + "-" + eventFinishArray.get(position) + ", " + eventGenderArray.get(position) + ", " + eventAgeArray.get(position);
eventDetails.setText(eventDetailsConcat);
eventDescription.setText(descriptionArray.get(position));
return rowView;
}
}
这是我的 Firebase 数据库结构的相关示例:
Timetable
Ladybridge High School, Bolton, UK
Date
2019-11-30
Spanish Lesson
Age: "All ages"
Description: "Example description for Spanish lesson"
Finish: "18:00"
Gender: "Male and female"
Start: "16:00"
Title: "Spanish for Beginners"
【问题讨论】:
标签: android firebase listview firebase-realtime-database