无法填充RecylcerView Android

时间:2016-10-13 13:27:45

标签: android android-recyclerview android-adapter

我试图了解如何为我的课程项目建立一个recyclerview,但出于某种原因,我无法做到。我不确定我错过了什么。想法?

编辑:我继续在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);



    }


}

2 个答案:

答案 0 :(得分:0)

将onPostExecute函数添加到AsyncTask并通知适配器

protected void onPostExecute(String result) {
     commentDetailAdapter.notifyDataSetChanged(); 
}
后台进程完成后

onPostExecute调用(之前coz数组可能为空),onPostExecute可以访问主线程。

答案 1 :(得分: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);