2
在這段代碼中,我做了一個ParseQuery來檢索基於條件的對象。當我在for塊的開始處放置一個斷點時,在done方法內,我可以看到它所具有的對象的數量,我想要檢索,當我逐步瀏覽時,每個項目都被添加到listItems數組中,但是當我跌倒並擊中最後的斷點時@return listItems listitems數組爲null。listitems返回null,從一個ParseQuery(編輯)
我試圖從SO中迴應一些例子,但似乎無法弄清楚。在回調中它獲取對象,但我不知道如何將結果分配給listitems對象。
有人能告訴我我在做什麼錯在這裏,我追蹤到我的問題到這段代碼。
public List<ListItem> getListItems()
{
final List<ListItem> listItems = new ArrayList<ListItem>();
ParseUser currentUser = ParseUser.getCurrentUser();
ParseQuery<ParseObject> postQuery = ParseQuery.getQuery("posts");
postQuery.whereEqualTo("username", currentUser.getUsername());
postQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (ParseObject object : objects) {
ListItem item = new ListItem();
ParseFile imgFile = object.getParseFile("pic");
item.setPostImage(imgFile);
listItems.add(item);
}
}
}
});
return listItems;
}
以我片段類,我實例化新的RecyclerView從getHeader()和getListItems()方法都返回值並通過這些值,以我的RecyclerView適配器類別,以供處理。
final RecyclerAdapter adapter = new RecyclerAdapter(getHeader(), getListItems());
這是我的自定義適配器類
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private Header header;
private List<ListItem> listItems;
public RecyclerAdapter(Header header, List<ListItem> listItems)
{
if (header == null) {
throw new IllegalArgumentException("Header may not be null");
}
this.header = header;
this.listItems = listItems;
}
public boolean isHeader(int position) {
return position == 0;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_HEADER)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
return new VHHeader(v);
}
else if(viewType == TYPE_ITEM)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
return new VHItem(v);
}
throw new RuntimeException("There is no type that matches the type " + viewType + " + make sure your using types correctly");
}
private ListItem getItem(int position)
{
return listItems.get(position);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (isHeader(position)) {
if(holder instanceof VHHeader)
{
VHHeader VHheader = (VHHeader)holder;
VHheader.txtFullname.setText(header.getFullname());
VHheader.txtWeb.setText(header.getWeb());
VHheader.txtBio.setText(header.getBio());
VHheader.txtSwaggCount.setText(String.valueOf(header.getSwaggCount()));
VHheader.txtFollowerCount.setText(String.valueOf(header.getFollowerCount()));
VHheader.txtFollowingCount.setText(String.valueOf(header.getFollowingCount()));
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(header.getProfileImage().getDataStream());
} catch (ParseException e) {
e.printStackTrace();
}
VHheader.cImageView.setImageBitmap(bmp);
VHheader.btnEditProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Context context = view.getContext();
Intent editProfileIntent = new Intent(context, EditProfileActivity.class);
context.startActivity(editProfileIntent);
}
});
return;
}
}
if(holder instanceof VHItem)
{
ListItem currentItem = getItem(position - 1);
VHItem VHItem = (VHItem)holder;
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(currentItem.getPostImage().getDataStream());
} catch (ParseException e) {
e.printStackTrace();
}
VHItem.iv.setImageBitmap(bmp);
}
}
// Need to override this method
public int getItemViewType(int position) {
if(isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position){
return position == 0;
}
@Override
public int getItemCount() {
return listItems.size() + 1;
}
private class VHHeader extends RecyclerView.ViewHolder {
TextView txtFullname;
TextView txtWeb;
TextView txtBio;
TextView txtSwaggCount;
TextView txtFollowerCount;
TextView txtFollowingCount;
CircleImageView cImageView;
Button btnEditProfile;
VHHeader(View itemView) {
super(itemView);
this.txtFullname = (TextView)itemView.findViewById(R.id.fullnameTV);
this.txtWeb = (TextView)itemView.findViewById(R.id.webTV);
this.txtBio= (TextView)itemView.findViewById(R.id.bioTV);
this.txtSwaggCount = (TextView)itemView.findViewById(R.id.swaggsCountTV);
this.txtFollowerCount = (TextView)itemView.findViewById(R.id.followerCountTV);
this.txtFollowingCount = (TextView)itemView.findViewById(R.id.followingCountTV);
this.cImageView = (CircleImageView)itemView.findViewById(R.id.avaImageIV);
this.btnEditProfile = (Button)itemView.findViewById(R.id.editProfileBtn);
}
}
private class VHItem extends RecyclerView.ViewHolder {
ImageView iv;
VHItem(View itemView){
super(itemView);
this.iv = (ImageView)itemView.findViewById(R.id.ivListItem);
}
}
這裏是我的ListItem類
public class ListItem {
private ParseFile postImage;
private int id;
public ListItem() {
}
public ParseFile getPostImage() {
return postImage;
}
public void setPostImage(ParseFile postImage) {
this.postImage = postImage;
}
而不是返回您可以處理回調裏面的名單? –
@Abhijith可以詳細說明一下,你是說不是返回列表,而是將方法簽名更改爲public void getListItems(),處理完成並且列表項在回調中設置並添加,但這將如何影響我的適配器即調用該方法。這裏是來自適配器的調用,最終的RecyclerAdapter adapter = new RecyclerAdapter(getHeader(),getListItems()); –
你可以編輯問題添加額外的代碼? –