【发布时间】:2017-05-15 15:21:17
【问题描述】:
我尝试从firebase 检索数据并使用adapter 在listview 中显示它们。
检索数据一切正常,我通过调试器进行了检查,但没有显示任何内容。
这是更新的resource_layout.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="match_parent"
android:background="@drawable/borders">
<ListView
android:id="@+id/listViewBooks"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</RelativeLayout>
更新了 resource_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
ResourceAdapter.java:
public class ResourceAdapter extends ArrayAdapter<Books> {
private Activity context;
private List<Books> bookList;
public ResourceAdapter(Activity context, List<Books> bookList){
super(context, R.layout.resource_item, bookList);
this.context = context;
this.bookList = bookList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.resource_item, null, true);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.name);
TextView textViewAuthor = (TextView) listViewItem.findViewById(R.id.author);
TextView textViewDate = (TextView) listViewItem.findViewById(R.id.date);
Books book = bookList.get(position);
textViewName.setText(book.getName());
textViewAuthor.setText(book.getAuthor());
textViewDate.setText(String.valueOf(book.getDate()));
return listViewItem;
}
}
ResourceActivity.Java:
public class Resource extends AppCompatActivity {
DatabaseReference databaseBooks;
ListView listViewBooks;
List<Books> bookList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resource_layout);
databaseBooks = FirebaseDatabase.getInstance().getReference("books");
bookList = new ArrayList<>();
listViewBooks = (ListView) findViewById(R.id.listViewBooks);
}
@Override
protected void onStart() {
super.onStart();
databaseBooks.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
bookList.clear();
for (DataSnapshot bookSnapshot : dataSnapshot.getChildren()){
Books book = bookSnapshot.getValue(Books.class);
bookList.add(book);
}
ResourceAdapter adapter = new ResourceAdapter(Resource.this, bookList);
listViewBooks.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
【问题讨论】:
标签: android listview firebase firebase-realtime-database adapter