Android 如何使用自定义baseAdapter在ListView中显示数据?
我想从FireBase数据库中检索数据,并使用自定义BaseAdapter将其显示在ListView中: 我有一本班级范本:Android 如何使用自定义baseAdapter在ListView中显示数据?,android,listview,firebase,firebase-realtime-database,baseadapter,Android,Listview,Firebase,Firebase Realtime Database,Baseadapter,我想从FireBase数据库中检索数据,并使用自定义BaseAdapter将其显示在ListView中: 我有一本班级范本: public class Book { String nom_livre; String desc_livre; String prix_livre; //Other Variables // getters and sitters } 自定义适配器FeedListAdapt
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;
}
}
公共类FeedListAdapter扩展了BaseAdapter{
私人活动;
私人充气机;
私人物品清单;
公共FeedListAdapter(活动,列出feedItems){
这个。活动=活动;
此项。feedItems=feedItems;
}
@凌驾
public int getCount(){
返回feedItems.size();
}
@凌驾
公共对象getItem(int位置){
返回feedItems.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
如果(充气器==null)
充气器=(充气器)活动
.getSystemService(上下文布局\充气机\服务);
if(convertView==null)
convertView=充气机。充气(R.layout.feed_项,空);
TextView name=(TextView)convertView.findViewById(R.id.name\u book\u display);
TextView statusMsg=(TextView)convertView.findViewById(R.id.desc_livre);
TextView prix=(TextView)convertView.findViewById(R.id.prix_显示);
图书项目=饲料项目。获取(位置);
name.setText(item.getNom_livre());
如果(!TextUtils.isEmpty(item.getDesc_livre())){
statusMsg.setText(item.getDesc_livre());
statusMsg.setVisibility(View.VISIBLE);
}否则{
statusMsg.setVisibility(View.GONE);
}
if(item.getPrix_livre()!=null){
prix.setText(item.getPrix_livre());
prix.setVisibility(视图可见);
}否则{
prix.setVisibility(View.GONE);
}
返回视图;
}
}
更新活动:
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();
}
});
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accueil);
listView=(listView)findViewById(R.id.list);
feedItems=新的ArrayList();
ref=FirebaseDatabase.getInstance().getReference(“书籍”);
ref.addValueEventListener(新的ValueEventListener(){
@凌驾
公共void onDataChange(DataSnapshot DataSnapshot){
对于(DataSnapshot dataSnap:DataSnapshot.getChildren()){
Book valueBook=dataSnap.getValue(Book.class);
字符串idBookDisplay=valueBook.getId_Book();
if(id_user.equals(idBookDisplay)){
字符串titreLivreToDisplay=valueBook.getNom_livre();
字符串descLivreToDisplay=valueBook.getDesc_livre();
字符串prixLivreToDisplay=valueBook.getPrix_livre();
字符串timeToDisplay=valueBook.getDate_creation();
字符串filePathToDiplay=valueBook.getChemin_image();
图书项目=新书();
项目:setNom_livre(标题显示);
项目.设置坡度(坡度显示);
项目:setPrix_livre(prixLivreToDisplay);
item.setDate_创建(timeToDisplay);
item.setChemin_图像(filePathToDiplay);
添加(项目);
}
}
listAdapter=新的FeedListAdapter(此为feedItems);
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>)'
“com.mydreambook.adapter.FeedListAdapter”中的“FeedListAdapter(android.app.Activity,java.util.List)”不能应用于“(匿名com.google.firebase.database.ValueEventListener,java.util.List)”
如何在自定义适配器中使用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>
更改
listAdapter = new FeedListAdapter(this, feedItems);
到
YourActivity
是您在其中调用的addValueEventListener
活动的名称。
在
onDataChange
方法中,此=ValueEventListener
不是活动
,这是FeedListAdapter
创建者所需的onDataChange我按照您的建议做了,我还有一个错误:java.lang.NullPointerException at Activity.onDataChange
在这一行:listView.setAdapter(listAdapter);
类似于ListView ListView=(ListView)findViewById(R.id.list);
在onCreate()中是否设置setContentLayout
?是否可以显示XML布局文件中的一段代码?(编辑您的问题,以及更多活动和布局代码)选中,没有可疑…我不知道为什么您的ListView
在侦听器中显示为null
(假设它在活动中调用,并且ListView
在上面声明onCreate
…)。你可能会删去很多代码;)这是另一个问题,我现在帮不了你,因为我要离开一段时间;)…提出新问题并在那里添加更多的活动代码,也许其他人会帮助你。XML布局看起来不错,findViewById
。祝你好运!:)
listAdapter = new FeedListAdapter(YourActivity.this, feedItems);