如何从Firebase数据库中读取除特定密钥之外的所有密钥的所有数据?

时间:2017-05-31 10:43:51

标签: android android-studio firebase firebase-realtime-database

我希望我的应用能够从Firebase数据库中读取所有密钥中的数据。

这是我的数据库的快照 snapshot of my Database

app_name下的键代表用户,用户键中的所有键都包含一些数据。我需要能够从每个用户密钥中读取数据,除了用户本身(如读取除我添加的数据之外的所有数据)。我显然不知道所有用户的密钥,并且该列表可以在以后扩展或最小化。我可以使用FirebaseDatabase.getInstance.getReference.child(string)访问的唯一级别位于应用名称下。有没有办法读取每个用户密钥内的密钥内的数据,并以列表视图 Recycler Adapter 的形式显示内容?

编辑:以下是我尝试的一些事情

public class FeedFragment extends Fragment {

//Firebase instance variables
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mQuestionDatabaseReference;
private ChildEventListener mChildEventListener;
private FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

尝试1:

FirebaseRecyclerAdapter mAdapter;
public FeedFragment() {
    //empty constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mQuestionDatabaseReference = mFirebaseDatabase.getReference();

    // Inflate the layout for this fragment
    View rootView =  inflater.inflate(R.layout.fragment_feed, container, false);

    RecyclerView recycler = (RecyclerView) rootView.findViewById(R.id.feed_view_list);
    recycler.setHasFixedSize(false);
    recycler.setLayoutManager(new LinearLayoutManager(getActivity()));

    mAdapter = new FirebaseRecyclerAdapter<DataGetter, StatsViewHolder>(DataGetter.class, 0, StatsViewHolder.class, mQuestionDatabaseReference) {
        @Override
        public void populateViewHolder(FeedViewHolder viewHolder, DataGetter feedDisplay, int position) {
            int layout = getItemViewType(position);
            if (layout == R.layout.binary_feed) {
                viewHolder.setQuestionTextBin(textDisplay.getQuestionText());
                viewHolder.setbOption1(textDisplay.getOption1());
                viewHolder.setbOption2(textDisplay.getOption2());
            }
            else if (layout == R.layout.rating_feed) {
                viewHolder.setQuestionTextRat(textDisplay.getQuestionText());
            } 
            else {
                viewHolder.setQuestionTextMul(textDisplay.getQuestionText());
                viewHolder.setmOption1(textDisplay.getOption1());
                viewHolder.setmOption2(textDisplay.getOption2());
                viewHolder.setmOption3(textDisplay.getOption3());
                if (textDisplay.getNoOfOptions() >= 4) {
                    viewHolder.setmOption4(textDisplay.getOption4());
                }
                if (textDisplay.getNoOfOptions() == 5) {
                    viewHolder.setmOption5(textDisplay.getOption5());
                }
            }
        }

        @Override
        public int getItemViewType(int position)
        {
            int view;
            if(getItem(position).getQuestionType() == MainActivity.BINARY){
                view = R.layout.binary_feed;
            }
            else if(getItem(position).getQuestionType() == MainActivity.RATING) {
                view = R.layout.rating_feed;
            }
            else //if(getItem(position).getQuestionType() == MainActivity.MULTIPLE)
            {
                view = R.layout.multiple_feed;
            }
            return view;
        }
    };
    recycler.setAdapter(mAdapter);

    return rootView;
}

@Override
public void onDestroy()
{
    super.onDestroy();
    mAdapter.cleanup();
}

尝试2:

FirebaseIndexRecyclerAdapter mAdapter;

public FeedFragment() {
    // empty constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    mQuestionDatabaseReference = mFirebaseDatabase.getReference();

    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_feed, container, false);

    RecyclerView recycler = (RecyclerView) rootView.findViewById(R.id.feed_view_list);
    recycler.setHasFixedSize(false);
    recycler.setLayoutManager(new LinearLayoutManager(getActivity()));

    //attempt -> from Firebase documentation, loop over all data
    final Query feedQuery = mQuestionDatabaseReference;

    ArrayList<DataSnapshot> feedListData = new ArrayList<DataSnapshot>();

    feedQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int i = 0;
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                ArrayList.add(postSnapshot);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
            // ...
        }
    });

    mAdapter = new FirebaseIndexRecyclerAdapter<DataGetter, FeedViewHolder>(DataGetter.class, 0, FeedViewHolder.class,
            feedQuery,mQuestionDatabaseReference) {
            //remaining code is same as attempt 1
    }

1 个答案:

答案 0 :(得分:0)

在上面的代码中,添加ValueAddedListener将postSnapshot中的值设置为Ki-7Xt之类的键。添加另一个循环以迭代postSnapshot.getChildren()将提供必要的内部数据&#34; 。

//a custom adapter extended from ArrayAdapter
FeedAdapter feedAdapter;
feedQuery.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            int i = 0;
            for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                /*One way to exclude some data is to ignore it whenever it
                 *appears and take action for the others. The same approach 
                 *has been taken here in the if condition*/
                if(postSnapshot.getKey().compareTo(user.getUid())!=0) {
                    //The inner loop to read data from the final desired location
                    for (DataSnapshot data : postSnapshot.getChildren()) {
                        DataGetter dataGetter = data.getValue(DataGetter.class);
                        //adding incoming data to adapter
                        feedAdapter.add(dataGetter);
                    }
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            //Getting values failed
            Log.i(TAG, "loadPost:onCancelled", databaseError.toException());
            //...
        }
    });

感谢@FrankvanPuffelen在您最近的2条评论中给出的解释。