我希望从FireBase数据库中检索数据并使用自定义BaseAdapter在ListView中显示它: 我有一个班级模型书:
public class Book {
String nom_livre;
String desc_livre;
String prix_livre;
//Other Variables
// getters and sitters
}
自定义适配器FeedListAdapter:
public class FeedListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Book> feedItems;
public FeedListAdapter( Activity activity, List<Book> feedItems) {
this.activity = activity;
this.feedItems = feedItems;
}
@Override
public int getCount() {
return feedItems.size();
}
@Override
public Object getItem(int location) {
return feedItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.feed_item, null);
TextView name = (TextView) convertView.findViewById(R.id.name_book_display);
TextView statusMsg = (TextView) convertView.findViewById(R.id.desc_livre);
TextView prix = (TextView) convertView.findViewById(R.id.prix_display);
Book item = feedItems.get(position);
name.setText(item.getNom_livre());
if (!TextUtils.isEmpty(item.getDesc_livre())) {
statusMsg.setText(item.getDesc_livre());
statusMsg.setVisibility(View.VISIBLE);
} else {
statusMsg.setVisibility(View.GONE);
}
if (item.getPrix_livre() != null) {
prix.setText(item.getPrix_livre());
prix.setVisibility(View.VISIBLE);
} else {
prix.setVisibility(View.GONE);
}
return convertView;
}
}
更新活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accueil);
listView = (ListView) findViewById(R.id.list);
feedItems = new ArrayList<Book>();
ref = FirebaseDatabase.getInstance().getReference("books");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {
Book valueBook=dataSnap.getValue(Book.class);
String idBookDisplay=valueBook.getId_Book();
if(id_user.equals(idBookDisplay)){
String titreLivreToDisplay=valueBook.getNom_livre();
String descLivreToDisplay=valueBook.getDesc_livre();
String prixLivreToDisplay=valueBook.getPrix_livre();
String timeToDisplay=valueBook.getDate_creation();
String filePathToDiplay=valueBook.getChemin_image();
Book item = new Book();
item.setNom_livre(titreLivreToDisplay);
item.setDesc_livre(descLivreToDisplay);
item.setPrix_livre(prixLivreToDisplay);
item.setDate_creation(timeToDisplay);
item.setChemin_image(filePathToDiplay);
feedItems.add(item);
}
}
listAdapter = new FeedListAdapter(this,feedItems);
listView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
});
但我有这个错误:
'FeedListAdapter(android.app.Activity, java.util.List<com.mydreambook.model.Book>)' in 'com.mydreambook.adapter.FeedListAdapter' cannot be applied to '(anonymous com.google.firebase.database.ValueEventListener, java.util.List<com.mydreambook.model.Book>)'
如何在自定义适配器中使用ValueEventListener?
使用源布局更新:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@null" />
<include
layout="@layout/app_bar_accueil"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_accueil"
app:menu="@menu/activity_accueil_drawer" />
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
更改
beforeCreate
到
listAdapter = new FeedListAdapter(this, feedItems);
listAdapter = new FeedListAdapter(YourActivity.this, feedItems);
是YourActivity
的名称,您在其中调用Activity
。
内部addValueEventListener
方法onDataChange
== this
,而不是ValueEventListener
创建者所需的Activity