在这段代码中,我制作了一个ParseQuery来根据条件检索对象。当我在for块的开头放置一个断点时,在done方法中,我可以看到它想要检索的对象数量,当我单步执行时,每个项目都被添加到listItems数组中,但当我通过并击中最终断点@return listItems时,listitems数组为空。
我尝试从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;
}
在我的Fragment类中,我实例化一个新的RecyclerView,它从getHeader()和getListItems()方法获取返回值,并将这些值传递给我的RecyclerView Adapter Class进行处理。
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;
}
答案 0 :(得分:0)
我想出了如何返回列表,感谢大家帮助我的帮助,但是对于我的解决方案,下面的代码让我的项目有效。
对于getListItems()方法,我改变了它
public List<ListItem> getListItems()
{
ParseUser currentUser = ParseUser.getCurrentUser();
ParseQuery<ParseObject> postQuery = ParseQuery.getQuery("posts");
postQuery.whereEqualTo("username", currentUser.getUsername());
List<ParseObject> objects;
List<ListItem> listItems = new ArrayList<>();
try {
objects = postQuery.find();
for (ParseObject object : objects) {
ListItem item = new ListItem();
ParseFile imgFile = object.getParseFile("pic");
item.setPostImage(imgFile);
listItems.add(item);
}
} catch (ParseException e) {
e.printStackTrace();
}
return listItems;
}