我尝试从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) {
}
});
}
}
答案 0 :(得分:0)
ListView
身高0dp
且体重有限。它不适用于RelativeLayout
。改变它。
<ListView
android:id="@+id/listViewBooks"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<强>更新强>
像那样改变行布局。从weight
中移除TextViews
个参数,并将LinearLayout的layout_height
设置为wrap_content
。高度计算可能会导致问题。
<?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>
更新2:尝试更改适配器的getView()方法,如下所示。
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if(convertView==null){
// inflate the layout
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.resource_item, null, false);
}
TextView textViewName = (TextView) convertView.findViewById(R.id.name);
TextView textViewAuthor = (TextView) convertView.findViewById(R.id.author);
TextView textViewDate = (TextView) convertView.findViewById(R.id.date);
Books book = bookList.get(position);
textViewName.setText(book.getName());
textViewAuthor.setText(book.getAuthor());
textViewDate.setText(String.valueOf(book.getDate()));
return convertView;
}
答案 1 :(得分:0)
问题出在XML
。您的父级布局为RelativeLayout
,您使用的是android:layout_weight="1"
和android:layout_height="0dp"
。
您可以在ListView
内使用RelativeLayout
,如下所示:
<?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>
您还可以使用LinearLayout
,如下所示:
<?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:background="@drawable/borders"
android:orientation="vertical">
<ListView
android:id="@+id/listViewBooks"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_width="match_parent" />
</<LinearLayout>