2014-10-15 7 views
3

firebase 데이터베이스의 데이터를 읽고 listView에 넣고 싶은 앱을 개발 중입니다.firebase에서 데이터를 읽고 listview에 채우기

3 가지 다른 읽기 작업이 있습니다.

  1. 앱이 처음 시작될 때 가장 최근에 읽은 10 개의 행을 가져 오려고합니다.

  2. listView의 끝에 도달하면 지금까지 읽은 가장 오래된 것부터 시작하여 10 개의 행이 더 필요합니다.

  3. listView의 맨 위로 가져 가면 첫 번째 행으로 표시되는 행 뒤에있는 모든 행을 가져 오려고합니다. (그게 이해 ...?)

참고 : 그것은 단지 내가 필요로하는 중포 기지-호출을 그래서 나는 이미 내가 상단과 목록보기의 끝을 읽을 때 리스너를 구현했습니다.

어떻게해야합니까?

+0

이미 시도해 보셨습니까? 그렇다면 코드를 공유하십시오. 그렇지 않은 경우 : https://www.firebase.com/docs/android/guide/retrieving-data.html –

+0

방금 ​​원했던대로 작동하도록했습니다. 여기에 답변을 게시합니다. – 4ice

+0

나는이 유용한 http://qiita.com/niusounds/items/2de6e08d308f95b8f4dd – Nepster

답변

8

나는이 문제를 해결했으며, 여기에 내가 한 일이있다.

1. 앱이 처음 시작될 때 가장 최근에 읽은 10 개의 행을 가져 오려고합니다. 나는 목록보기의 끝에 도달 할 때

//read the 15 latest posts from the db 
postQuery.limit(15).addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     //Read each child of the user 
     for(DataSnapshot child : dataSnapshot.getChildren()) 
     { 
      //If this is the first row we read, then this will be the oldest post that is going to be read, 
      //Therefore we save the name of this post for further readings. 
      if(prevOldestPostId.equals(oldestPostId)) 
      { 
       //Save the name of the last read post, for later readings 
       oldestPostId = child.getName(); 
      } 
      //This if-statment is just a fail-safe, since I have an counter in the post, and that one I do not want to read. 
      if(child.getName() != "nrOfPosts") 
      { 
       //Gather the data from the database 
       UserPost readPost = new UserPost(); 
       for(DataSnapshot childLvl2 : child.getChildren()) { 
        if(childLvl2.getName() == "post") 
         readPost.setMessage(childLvl2.getValue().toString()); 
        else 
         readPost.setDate(childLvl2.getValue().toString()); 
       } 
       //Add the data to a temporary List 
       toAdd.add(0, readPost); 
      } 
      //Keep the name of the newest post that has been read, we save this for further readings. 
      newestPostId = child.getName(); 
     } 
     //prevOlddestPostId is helping us to keep the the currently oldest post-name. 
     prevOldestPostId = oldestPostId; 
     //Add the new posts from the database to the List that is connected to an adapter and later on a ListView. 
     for (UserPost aToAdd : toAdd) thePosts.add(aToAdd); 

     // We need notify the adapter that the data have been changed 
     mAdapter.notifyDataSetChanged(); 

     // Call onLoadMoreComplete when the LoadMore task, has finished 
     mListView.onRefreshComplete(); 
    } 

    @Override 
    public void onCancelled(FirebaseError firebaseError) { 

    } 
}); 

2. 지금까지 읽은 가장 오래된에서 시작, 10 개 행을 싶어. 나는 목록보기의 상단에 당길 때

postQuery.endAt(1, oldestPostId).limit(15).addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     //Gather the data 
     for(DataSnapshot child : dataSnapshot.getChildren()) 
     { 
      if(prevOldestPostId.equals(oldestPostId) && !child.getName().equals("nrOfPosts")) 
      { 
       //Save the name of the last read post, for later readings 
       oldestPostId = child.getName(); 
      } 

      if(child.getName() != "nrOfPosts" && !prevOldestPostId.equals(child.getName())) 
      { 
       UserPost readPost = new UserPost(); 

       for(DataSnapshot childLvl2 : child.getChildren()) { 
        if(childLvl2.getName() == "post") 
         readPost.setMessage(childLvl2.getValue().toString()); 
        else 
         readPost.setDate(childLvl2.getValue().toString()); 
       } 
       toAdd.add(0, readPost); 

      } 
     } 
     prevOldestPostId = oldestPostId; 

     //Insert it to the List for the listView 
     for (UserPost aToAdd : toAdd) 
     { 
      thePosts.add(aToAdd); 


      // We need notify the adapter that the data have been changed 
      mAdapter.notifyDataSetChanged(); 
     } 
    } 

    @Override 
    public void onCancelled(FirebaseError firebaseError) { 

    } 
}); 

3. 이제 첫 번째로 표시되는 행 이후에 된 모든 행을 싶어. 나는 희망

postQuery.startAt(1, newestPostId).addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     for(DataSnapshot child : dataSnapshot.getChildren()) 
     { 
      if(child.getName() != "nrOfPosts") 
      { 
       UserPost readPost = new UserPost(); 

       for(DataSnapshot childLvl2 : child.getChildren()) { 
        if(childLvl2.getName() == "post") 
         readPost.setMessage(childLvl2.getValue().toString()); 
        else 
         readPost.setDate(childLvl2.getValue().toString()); 
       } 
       //Prevent from get the currently newest post again... 
       if(!readPost.getMessage().equals(thePosts.get(0).getMessage())) 
        toAdd.add(readPost); 

       //Save the name of the last read post, for later readings 
       newestPostId = child.getName(); 
      } 
     } 
     for (UserPost aToAdd : toAdd) 
     { 
      thePosts.add(0, aToAdd); 

      // Call onLoadMoreComplete when the LoadMore task, has finished 
      mListView.onRefreshComplete(); 
     } 
    } 

    @Override 
    public void onCancelled(FirebaseError firebaseError) { 

    } 
}); 

나는이어야 누군가에게 도움이 될 수 있습니다 (이해할 수 ...?)입니다.

+1

찾을 수 plz 당신의 코드에서 postQuery 무엇인지 설명해 주실 수 있습니다 ..? \ –

관련 문제