android - 在 ListView 中显示 firebase 子对象

标签 android listview firebase firebase-realtime-database

我是 Firebase 的新手。我正在尝试查询 firebase 数据库并在 ListView 中显示结果的所有子对象。我没有错误,但没有显示任何内容。它不会崩溃,但也不会执行任何操作。请帮帮我。

我的数据库的内容: Contents of my database

这是我的数据检索代码:

 imgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                key = search.getText().toString().trim();
                Firebase newRef = new Firebase("https://stockmanager-142503.firebaseio.com/Items");
                Query query = newRef.orderByChild("Idno").equalTo(key);
                query.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                        Map<String,Item> td = (HashMap<String,Item>) dataSnapshot.getValue();

                        List<Item> valuesToMatch = new ArrayList<Item>(td.values());
                        myAdapter myadapter=new myAdapter(getActivity(),valuesToMatch);
                        mlistView.setAdapter(myadapter);
                    }

                    @Override
                    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onChildRemoved(DataSnapshot dataSnapshot) {

                    }

                    @Override
                    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {

                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setMessage(firebaseError.getMessage())
                                .setTitle("Error!")
                                .setPositiveButton(android.R.string.ok, null);
                        AlertDialog dialog = builder.create();
                        dialog.show();


                    }
                });

这是我的适配器类:

public class myAdapter extends BaseAdapter {
    private List<Item> items;
    private Context mContext;


    public myAdapter(Context mContext, List<Item> items) {
        this.mContext=mContext;
        this.items=items;
    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LinearLayout linearLayout;
        Item item=items.get(i);
        if (view == null) {
            linearLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.rowitem, viewGroup, false);
        } else {
            linearLayout = (LinearLayout)view;
        }
        TextView text1=(TextView)linearLayout.findViewById(R.id.text1);
        TextView text2=(TextView)linearLayout.findViewById(R.id.text2);
        TextView text3=(TextView)linearLayout.findViewById(R.id.text3);
        TextView text4=(TextView)linearLayout.findViewById(R.id.text4);
        TextView text5=(TextView)linearLayout.findViewById(R.id.text5);
        text1.setText(item.getIdno());
        text2.setText(item.getName());
        text3.setText(item.getBrand());
        text4.setText(item.getCost());
        text5.setText(item.getDate());


        return null;

    }
}

这是项目类:

public class Item {
    private String Type;
    private String Name;
    private String Brand;
    private String Cost;
    private String Date;
    private String Store;
    private String Idno;

    public String getName() {
        return Name;
    }

    public String getIdno() {
        return Idno;
    }


    public String getCost() {
        return Cost;
    }

    public void setDate(String date) {
        Date = date;
    }

    public String getBrand() {
        return Brand;
    }




    public String getStore() {
        return Store;
    }



    public String getType() {
        return Type;
    }

    public String getDate() {
        return Date;
    }


}

最佳答案

我自己修好了。显然,查询返回了一个对象列表,因此我用 for 循环替换了 Hashmap 以获取子项并将它们添加到列表中。但是我的 ListView 根本不会显示检索到的数据。最后,我使用了下面给出的 firebase UI List Adpater 并修复了它。谢谢大家!

  imgbtn.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View view) {
                                          key = scanContent;
                                          Query q = dbRef.orderByChild("idno").equalTo(key);
                                          FirebaseListAdapter<Item> adapter =new FirebaseListAdapter<Item>(getActivity(),Item.class,R.layout.rowitem,q) {
                                              @Override
                                              protected void populateView(View v, Item item, int position) {
                                                  TextView text1=(TextView)v.findViewById(R.id.text1);
                                                  TextView text2=(TextView)v.findViewById(R.id.text2);
                                                  TextView text3=(TextView)v.findViewById(R.id.text3);
                                                  TextView text4=(TextView)v.findViewById(R.id.text4);
                                                  TextView text5=(TextView)v.findViewById(R.id.text5);
                                                  text1.setText(item.getIdno());
                                                  text2.setText(item.getName());
                                                  text3.setText(item.getBrand());
                                                  text4.setText(item.getCost());
                                                  text5.setText(item.getDate());
                                                  solditem=item;
                                              }
                                          };
                                          sell.setText("Add this result to Sold Items List?");
                                      sList.setAdapter(adapter);
                                      }
                                  });

关于android - 在 ListView 中显示 firebase 子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558397/

相关文章:

Android RecyclerView - 在键盘打开时保持最后一个可见项目可见

java - 如何将 ColorDrawable 的 getColor() 返回的 int 值转换为 RGB?

java - 如何从具有不同结果的 Realm 数据库中查询java

c# - Xamarin 表格 : Grouping ObservableCollection

javascript - Firebase 函数返回

Android:无法保存和读取 Bundle 中的所有值

c# - 如何在没有 WPF 的情况下将 ProgressBar 嵌入到 ListView 中?

javascript - jQuery移动动态分割按钮 ListView

reactjs - 在本地Firebase中,如何为当前环境设置projectId?

java - 如何使用 Firebase 停止正在进行的电话身份验证请求