我試圖按照如何爲我的類項目構建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);
}
}
你在哪裏設置適配器RecyclerView?並檢查您是否添加了任何種類的帶有RecyclerView的LayoutManager。 – ajantha
啊是的,我在onCreate方法中設置爲'commentRecView =(RecyclerView)findViewById(R.id.comment_list); commentRecView.setLayoutManager(新的LinearLayoutManager(this)); commentDetailAdapter = new CommentDetailAlt(commentList,this); commentRecView.setAdapter(commentDetailAdapter);' –
在AsyncTask完成後您還需要通知適配器 –