简体   繁体   中英

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. 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.

I tried to follow some examples from the response, from SO, but can't seem to figure it out. In the call back it gets the objects, but I don't know how to assign the results to the listitems object.

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.

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

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM