2016-10-13 70 views
0

我試圖按照如何爲我的類項目構建recyclerview,但出於某種原因,我無法做到這一點。我不確定我錯過了什麼。想法?無法填充RecylcerView Android

編輯:我繼續在reuqest增加了更多的代碼

的代碼,相關:

String selectedVenue; 
String id; 
String currentuser; 
String venueType; 
boolean canComment; 
ArrayList<Comments> commentList = new ArrayList<>(); 

ParseGeoPoint venueLocation; 
ParseGeoPoint userLocation; 

Button commentButton; 
RecyclerView commentRecView; 
SwipeRefreshLayout mySwipeRefreshLayout; 

CommentDetailAlt commentDetailAdapter; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    setContentView(R.layout.activity_user_feed_alt); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    try{ 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 
    currentuser = ParseUser.getCurrentUser().getUsername(); 


    mySwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshAlt); 
    mySwipeRefreshLayout.setOnRefreshListener(
      new SwipeRefreshLayout.OnRefreshListener() { 
       @Override 
       public void onRefresh() { 
        mySwipeRefreshLayout.setRefreshing(true); 
        commentList.clear(); 
        GetFeed download = new GetFeed(); 
        download.execute(); 
       } 
      } 

    ); 

    commentRecView = (RecyclerView) findViewById(R.id.comment_list); 
    commentRecView.setLayoutManager(new LinearLayoutManager(this)); 
    commentDetailAdapter = new CommentDetailAlt(commentList, this); 
    commentRecView.setAdapter(commentDetailAdapter); 

    UserFeedAlt.GetFeed download = new GetFeed(); 
    download.execute(); 

} 




public class GetFeed extends AsyncTask<String, Void, Void> { 
    ArrayList<Comments> temp = new ArrayList<>(); 

    @Override 
    protected Void doInBackground(String... strings) { 

     ParseQuery<ParseObject> contentFeed = new ParseQuery<>("UserCommentary"); 
     ParseQuery<ParseObject> drinks = new ParseQuery<>("venueDrinks"); 
     if(venueType.equals("House Parties")){ 
      //Query for Bars 
      drinks.whereEqualTo("venueName",selectedVenue); 
      drinks.whereEqualTo("venueID",id); 
      drinks.addDescendingOrder("createdAt"); 
      drinks.setLimit(2); 
      try{ 
       List<ParseObject>qReply = drinks.find(); 
       if(qReply.size()>0){ 
        for(ParseObject item : qReply){ 
         String drinkName = item.getString("venueDrinkName"); 
         float drinkPrice = (float) item.getInt("venuePriceDrink"); 
         boolean isImage = item.getBoolean("isImage"); 
         Bitmap bmp; 
         Comments drink = new Comments(String.valueOf(drinkPrice),selectedVenue,id,drinkName,0,true); 
         if(isImage){ 
          ParseFile dataPhoto = item.getParseFile("imgFile"); 
          byte[] data = dataPhoto.getData(); 
          bmp = BitmapFactory.decodeByteArray(data, 0 ,data.length); 
          drink.photoFile = bmp; 
         } 
         temp.add(drink); 
        } 
       } 
       else{ 
        String comment = "Nothing Here"; 
        String owner = "Mr. Smith"; 
        int count = 0; 
        String id = "Test"; 
        String venueName = "Smith's Mayback's"; 
        Comments newComment = new Comments(comment,venueName,id,owner,count,false); 
        temp.add(newComment); 
       } 
      } 
      catch (Exception e){ 
       e.printStackTrace(); 
      } 


      contentFeed.whereEqualTo("venueName",selectedVenue); 
      contentFeed.whereEqualTo("venueID",id); 
      contentFeed.addDescendingOrder("createdAt"); 
      try{ 
       List<ParseObject>qReply = contentFeed.find(); 
       if(qReply.size()>0){ 
        for(ParseObject item : qReply){ 
         String commentOwner = item.getString("owner"); 
         String commentContent = item.getString("comment"); 
         boolean isImage = item.getBoolean("image"); 
         Bitmap bmp; 
         Comments comment = new Comments(commentContent,selectedVenue,id,commentOwner,0,false); 
         if(isImage){ 
          ParseFile dataPhoto = item.getParseFile("imgFile"); 
          byte[] data = dataPhoto.getData(); 
          bmp = BitmapFactory.decodeByteArray(data, 0 ,data.length); 
          comment.photoFile = bmp; 
         } 
         temp.add(comment); 
        } 
       } 
      } 
      catch (Exception e){ 
       e.printStackTrace(); 
      } 


     } 
     else if(venueType.equals("Bars")){ 
      //Query for HouseParties 
      contentFeed.whereWithinMiles("Location",userLocation,0.00284091); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     commentList = temp; 
     Log.i("AppInfo", String.valueOf(commentList.size())); 
     commentDetailAdapter.notifyDataSetChanged(); 
     mySwipeRefreshLayout.setRefreshing(false); 



    } 


} 
+1

你在哪裏設置適配器RecyclerView?並檢查您是否添加了任何種類的帶有RecyclerView的LayoutManager。 – ajantha

+0

啊是的,我在onCreate方法中設置爲'commentRecView =(RecyclerView)findViewById(R.id.comment_list); commentRecView.setLayoutManager(新的LinearLayoutManager(this)); commentDetailAdapter = new CommentDetailAlt(commentList,this); commentRecView.setAdapter(commentDetailAdapter);' –

+0

在AsyncTask完成後您還需要通知適配器 –

回答

0

添加onPostExecute功能的AsyncTask,並通知適配器

protected void onPostExecute(String result) { 
    commentDetailAdapter.notifyDataSetChanged(); 
} 

onPostExecute在後臺進程完成之後調用(coz數組可能在此之前爲空),onPostExecute可以訪問主線程。

+0

我添加了代碼,出於某種原因,這並沒有解決它。 –

+0

是否確保數據數組不爲空 –

+0

不是。在post執行方法後,我檢查瞭解析服務器和數組的大小。 2項應該存在 –

0

原文:

commentDetailAdapter = new CommentDetailAlt(commentList, this); 
commentRecView.setAdapter(commentDetailAdapter); 

UserFeedAlt.GetFeed download = new GetFeed(); 
download.execute(); 

改變,要

UserFeedAlt.GetFeed download = new GetFeed(); 
download.execute(); 

commentDetailAdapter = new CommentDetailAlt(commentList, this); 
commentRecView.setAdapter(commentDetailAdapter); 
+0

謝謝你的回覆,事實證明我很確定自定義對象'Comments'實際上是問題的根源。我正在修復一個方法來解決它。 –