[英]listitems is returning null, from a ParseQuery (Edited)
In this snippet of code, I'm making a ParseQuery to retrieve objects based on a condition. 在这段代码中,我正在做一个ParseQuery来根据条件检索对象。 When I place a break point at the beginning of the for block, within the done method, I can see the number of objects it has, that I want to retrieve, as I step through, each item is being added to the listItems array, but when I fall through and hit the final breakpoint @return listItems the listitems array is null.
在完成方法中,在for块的开头放置一个断点时,我可以看到它要检索的对象数,逐步执行时,每个项目都将添加到listItems数组中,但是当我跌倒并遇到最后的断点@return listItems时,listitems数组为null。
I tried to follow some examples from the response, from SO, but can't seem to figure it out. 我尝试从SO的响应中遵循一些示例,但似乎无法弄清楚。 In the call back it gets the objects, but I don't know how to assign the results to the listitems object.
在回调中,它获取对象,但是我不知道如何将结果分配给listitems对象。
Could someone tell me what I am doing wrong here, I tracked my problem down to this snippet of code. 有人可以告诉我我在这里做错了什么,我一直将问题归结为这段代码。
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;
}
In my Fragment class, I instantiate a new RecyclerView that takes the returned value from the getHeader() and getListItems() methods and pass these values to my RecyclerView Adapter Class, for processing. 在我的Fragment类中,我实例化了一个新的RecyclerView,它从getHeader()和getListItems()方法获取返回的值,并将这些值传递给RecyclerView Adapter类进行处理。
final RecyclerAdapter adapter = new RecyclerAdapter(getHeader(), getListItems());
This is my custom adapter class 这是我的自定义适配器类
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);
}
}
And here is my ListItem Class 这是我的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;
}
I figured out how to return the list, thanks for the help to everyone trying to help me, but for my solution, the code below, has my project working. 我想出了如何返回列表,感谢所有试图帮助我的人的帮助,但是对于我的解决方案(下面的代码),我的项目正在工作。
For the getListItems() method, I changed it like so 对于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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.