Firebase在listview中检索数据

时间:2017-11-23 15:41:52

标签: android firebase

一切正常。但问题是当我尝试从firebase检索数据并在列表视图中显示时出现问题。列表视图中只显示一个节点。 这是检索数据的创建部分。

     @Override
      protected void onStart() {
        super.onStart();

        databaseBazars.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                 bazarList.clear();
                 for( DataSnapshot ds :dataSnapshot.getChildren()) {
                     Bazar bazar = ds.getValue(Bazar.class); //here 'Bazar' model class
                     bazarList.add(bazar);
                 }
            BazarList adapter = new BazarList(Bazar_Data_Input.this,bazarList);
                listViewBazars.setAdapter(adapter);
            }


            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

这里是模型类的内部

     public class Bazar {


        String bid;
        String date;
        String cost;
        String name;

        public Bazar(){

        }

        public Bazar(String bid, String date, String cost, String name) {
            this.bid = bid;
            this.date = date;
            this.cost = cost;
            this.name = name;
        }

        public String getBid() {
            return bid;
        }

        public String getDate() {
            return date;
        }

        public String getCost() {
            return cost;
        }

        public String getName() {
            return name;
        }
    }

               '

这是数组适配器

public class BazarList extends ArrayAdapter<Bazar> {
    private Activity context;
    private List<Bazar> bazarList;

    public BazarList(Activity context,List<Bazar>bazarList){
        super(context,R.layout.list_layout,bazarList);
        this.context=context;
        this.bazarList=bazarList;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();

        View ListViewItem= inflater.inflate(R.layout.list_layout,null,true);
        TextView textviewdate =(TextView) ListViewItem.findViewById(R.id.textviewdate);
        TextView textviewcost=(TextView) ListViewItem.findViewById(R.id.textviewcost);
        TextView texviewname=(TextView) ListViewItem.findViewById(R.id.textviewname1);

        Bazar bazar = bazarList.get(position);

        textviewdate.setText(bazar.getDate());
        textviewcost.setText(bazar.getCost());
        texviewname.setText(bazar.getName());
        return ListViewItem;
    }
}
    '
谁能告诉我我做错了什么?或者如何在列表视图中查看数据库中的所有数据?

这是用于显示数据的自定义布局。

&#39;

<?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/textviewdate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        />


    <TextView
        android:id="@+id/textviewcost"
        android:textColor="#fff"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textviewname1"
        android:textColor="#fff"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />



</LinearLayout>

&#39;

1 个答案:

答案 0 :(得分:0)

这里在onStart的代码中不要清除列表,也不要重置适配器:

  @Override
  protected void onStart() {
    super.onStart();

    databaseBazars.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot == null) return;
             for( DataSnapshot ds :dataSnapshot.getChildren()) {
                 Bazar bazar = ds.getValue(Bazar.class); //here 'Bazar' model class
                 bazarList.add(bazar);
             }
             adapter.notifyDataSetChanged()
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

确保在onCreate

中设置适配器和数据
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(blabla)
    // your other code
    bazarList = new ArrayList<>()
    BazarList adapter = new BazarList(Bazar_Data_Input.this,bazarList);
    listViewBazars.setAdapter(adapter);
}