Firebase数据库显示在recyclerview中

时间:2017-07-02 20:06:53

标签: android arrays

我有一个数据库,我想在我的数据库中显示数组的标题。我已经尝试过我能感谢的一切。请帮忙!我收到的错误代码是com.google.firebase.database.DatabaseException:无法将java.util.ArrayList类型的对象转换为com.dll_technologies.grouplist.ListObject

数据库设置是我想要后退一般和购物而不是孩子

"general" : [ {
"due" : "2017-07-04",
"name" : "Meeting"
} ],
"shopping" : [ {
"item" : "milk"
}]

我的代码是

public class ListFragment extends Fragment {
private static final String TAG = "ListFragment";
private FirebaseDBHelper firebaseDBHelper = new FirebaseDBHelper();
private FirebaseAuth mAuth;
//private DatabaseReference mRef;
private Query mListRef = firebaseDBHelper.getDemoListReference();
private RecyclerView mLists;
private ListObject listObject;
private GridLayoutManager mManager;
private FirebaseRecyclerAdapter<ListObject, ListsHolder> 
mRecyclerViewAdapter;

private OnFragmentInteractionListener mListener;

public ListFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_list, container, false);
    mAuth = FirebaseAuth.getInstance();
    mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

        }
    });

    mLists = (RecyclerView)view.findViewById(R.id.listsRecycler);

    mManager = new GridLayoutManager(getContext(), 2);
    mManager.setReverseLayout(false);

    mLists.setHasFixedSize(false);
    mLists.setLayoutManager(mManager);

    return view;
}

@Override
public void onStart() {
    super.onStart();
    attachRecyclerViewAdapter();
}

private void attachRecyclerViewAdapter() {
    mRecyclerViewAdapter = new FirebaseRecyclerAdapter<ListObject, 
ListsHolder>
            (ListObject.class, R.layout.fragment_list_card_view, 
ListsHolder.class, mListRef) {
        @Override
        protected void populateViewHolder(ListsHolder viewHolder, ListObject 
model, int position) {
            mListRef = firebaseDBHelper.getDemoListReference();
            mListRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    ListObject listObject = 
dataSnapshot.getValue(ListObject.class);
                    Toast.makeText(getContext(), "" + dataSnapshot, 
Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            /*
            mListRef = mRecyclerViewAdapter.getRef(position);
            Toast.makeText(getContext(), "" + mListRef, 
Toast.LENGTH_SHORT).show();
            viewHolder.setName(model.getActivity());
            */
        }
    };

    mRecyclerViewAdapter.registerAdapterDataObserver(new 
RecyclerView.AdapterDataObserver() {
        @Override
        public void onItemRangeInserted(int positionStart, int itemCount) {
            mManager.smoothScrollToPosition(mLists, null, 
mRecyclerViewAdapter.getItemCount());
        }
    });

    mLists.setAdapter(mRecyclerViewAdapter);
}

@Override
public void onStop() {
    super.onStop();
    mRecyclerViewAdapter.cleanup();
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

public static class ListsHolder extends RecyclerView.ViewHolder implements 
View.OnClickListener {
    View mView;

    public ListsHolder(View itemView) {
        super(itemView);
        mView = itemView;
        mView.setOnClickListener(this);
    }

    public void setName(String name){
        TextView listname = 
(TextView)mView.findViewById(R.id.fragment_list_name);
        listname.setText(name);
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), "Clicked View Postion " + 
getAdapterPosition(), Toast.LENGTH_SHORT).show();
    }
}
}

0 个答案:

没有答案