在Android Firebase数据库中的密钥下检索孩子时遇到问题

时间:2019-03-21 04:07:32

标签: android firebase firebase-realtime-database

我在为应用程序用户构建燃料收据跟踪时假设使用FB DB放屁。我有当前的数据库结构。.

enter image description here

层次结构为“住区/用户ID /结算ID /燃料”。目的是在给定用户的任何给定结算的“加油”键的帮助下,将所有孩子归还给该用户。将来还会有其他与燃料无关的条目,但与相同的lementationId相关。

这是我的valueEventListener,用于检索数据。

public class FragmentFuel extends Fragment {
    private ArrayList<Fuel> fuel = new ArrayList<>();
    private Context context;
    private MI MI;
    private RecyclerView.Adapter recyclerAdapter = new recyclerAdapter();
    private ValueEventListener valueEventListener;
    private DatabaseReference reference;
    //private DecimalFormat usdFormat = new DecimalFormat("#.00");

    public FragmentFuel() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Logger.i(dataSnapshot.getRef().toString());
                Logger.i("COUNT " + dataSnapshot.getChildrenCount());
                fuel.clear();
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    fuel.add(child.getValue(Fuel.class));
                }
                recyclerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        };
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_fuel, container, false);
        RecyclerView recyclerView = v.findViewById(R.id.recycler);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(recyclerAdapter);
        return v;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        MI = (MI) getActivity();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (MI != null) {
            reference = MI.getReference().child("settlements").child(MI.getUID()).child(MI.getSID()).child("fuel");
            reference.addValueEventListener(valueEventListener);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        reference.removeEventListener(valueEventListener);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        MI = null;
    }

    private class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.viewHolder> {
        @Override
        public viewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new viewHolder(getLayoutInflater().inflate(R.layout.fuel_row, parent, false));
        }

        @Override
        public void onBindViewHolder(viewHolder holder, int position) {
            Fuel row = fuel.get(position);
            holder.date.setText(TimeConvert.toDateAndTime(row.getStamp()));
            holder.cost.setText("$" + formatInt(row.getCost()));
            holder.odometer.setText("odometer: " + formatInt(row.getOdometer()));
            holder.gallons.setText(formatInt(row.getGallons()) + " gallons");
            if (row.getNote().equals("")) holder.note.setVisibility(View.GONE);
            else holder.note.setVisibility(View.VISIBLE);
            holder.note.setText(row.getNote());
        }

        @Override
        public int getItemCount() {
            return fuel.size();
        }

        class viewHolder extends RecyclerView.ViewHolder {
            TextView date, cost, odometer, gallons, note;

            viewHolder(View itemView) {
                super(itemView);
                date = itemView.findViewById(R.id.date);
                cost = itemView.findViewById(R.id.cost);
                odometer = itemView.findViewById(R.id.odometer);
                gallons = itemView.findViewById(R.id.gallons);
                note = itemView.findViewById(R.id.note);
            }
        }
    }

    private String formatInt(int count) {
        return NumberFormat.getNumberInstance(Locale.US).format(count);
    }
}

我的日志输出显示OnDataChanged被正确调用,但是getChildCount()返回值为0。引用路径正确,应该返回4个子代。

enter image description here

希望有人能看到我错过的内容。

1 个答案:

答案 0 :(得分:0)

问题是参考路径错误。您会注意到数据中的结帐ID是“ -LaSu7s73QvpfeE6D3TS”,但记录的参考路径没有破折号“-”。

“ LaSu7s73QvpfeE6D3TS”应该是“ -LaSu7s73QvpfeE6D3TS”