Marialena
Marialena

Reputation: 817

ListView adapter returns only the last row of Parse.com object - Android

Using an AsyncTask, I am doing a query to an object named Shop_comments in Parse.com. Inside the results of the query, I am getting the username, that is a pointer to _User object and I retrieve info from that user. All that info of the user and the comments, I put them in the results which is an object to SetCommentsResultsConditions class which gates the data from queries and returns them through other functions, in another fragment activity.


This is my AsyncTask:

    private class ViewDataTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //startLoading();

            proDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create the array
            searchlist = new ArrayList<SetCommentsResultsConditions>();

            //use it to find the shop object id
            final ParseObject shop_id = ParseObject.createWithoutData("Shop", shopname);
//            Log.d("SHOP ID ", shopname);

            try {
                final ParseQuery comment = new ParseQuery("Shop_comments");
                comment.whereEqualTo("name", shop_id);
                comment.include("username");

                comment.orderByDescending("createdAt");

                final SetCommentsResultsConditions result = new SetCommentsResultsConditions();

                ob = comment.find();

                if (ob.size() == 0) {
                    flag_show = false;
                } else {
                    flag_show = true;
                }

                if (flag_show) {
                    for (ParseObject shop : ob) {
                        ParseUser user = shop.getParseUser("username");
                        String username = user.getUsername();

                        Log.d("USER", username);
                        Log.d("COMMENT", String.valueOf(shop.getString("comment")));
                        Log.d("REWARD RATING", String.valueOf(user.getInt("reward_rating")));

                        result.setUserName(String.valueOf(username));
                        result.setApprovedFlag(shop.getBoolean("label_approved"));
                        result.setComment(String.valueOf(shop.getString("comment")));
                        result.setDate(shop.getUpdatedAt());

                        if (user.getParseFile("photo") != null) {
                            ParseFile image = (ParseFile) user.get("photo");

                            if (image.getUrl() != null)
                                result.setImage(image.getUrl()); //send photo
                        }

                        result.setReward_rating(String.valueOf(user.getInt("reward_rating")));
                        result.setReward_shop(String.valueOf(user.getInt("reward_shop")));
                        result.setGender(user.getString("gender"));

                        searchlist.add(result);

                        Log.d("GET COMMENT", result.getComment());
                    }
                }
            } catch (com.parse.ParseException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            proDialog.hide();
            //stopLoading();

            if (flag_show) {
//                Toast.makeText(getActivity().getApplicationContext(), "There are comments for this shop!", Toast.LENGTH_LONG).show();

                // Locate the listview in listview_main.xml
                listview = (ListView) convertView.findViewById(R.id.comments);

                // Pass the results into ListViewAdapter.java
                adapter = new ListViewAdapter_ViewComments(getActivity().getApplicationContext(), searchlist);

                // Binds the Adapter to the ListView
                listview.setAdapter(adapter);
            }
            else{
                Toast.makeText(getActivity().getApplicationContext(), "We couldn't find any valid comments!", Toast.LENGTH_LONG).show();

                Intent intent;
                intent = new Intent(getActivity().getApplicationContext(), BaseActivity.class);

                intent.putExtra("flag_failviewcomment", "Y");

                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

                getActivity().overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);

                startActivity(intent);
            }
        }
    }

This is the SetResuktsConditions class in which I send the data from the query:

public class SetCommentsResultsConditions {
    private String image;
    private String reward_shop;
    private String reward_rating;
    private String username;
    private String comment;
    private Date date;
    private String gender;
    private boolean flag_approved;

    public String getUserName() {
        return username;
    }
    public void setUserName(String username) {
        this.username = username;
    }

    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getReward_shop() {
        return reward_shop;
    }
    public void setReward_shop(String reward_shop) {
        this.reward_shop = reward_shop;
    }

    public String getReward_rating() {
        return reward_rating;
    }
    public void setReward_rating(String reward_rating) {
        this.reward_rating = reward_rating;
    }

    public boolean getApprovedFlag(){ return flag_approved;}
    public void setApprovedFlag(Boolean flag_approved) {
        this.flag_approved = flag_approved;
    }

    public Date getDate(){ return date;}
    public void setDate(Date date) {
        this.date = date;
    }

    public String getImage() {
        return image;
    }
    public void setImage(String image) {
        this.image = image;
    }

    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
}

And this is my ListViewAdapter_ViewComments class:

public class ListViewAdapter_ViewComments extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ImageLoader_Comments imageLoader;

    private List<SetCommentsResultsConditions> searchlist = null;
    private ArrayList<SetCommentsResultsConditions> arraylist;

    public ListViewAdapter_ViewComments(Context context, List<SetCommentsResultsConditions> searchlist) {
        this.context = context;
        this.searchlist = searchlist;
        inflater = LayoutInflater.from(context);
        this.arraylist = new ArrayList<SetCommentsResultsConditions>();
        this.arraylist.addAll(searchlist);
        imageLoader = new ImageLoader_Comments(context);
    }

    public class ViewHolder {
        TextView username;
        TextView date;
        TextView reward_rate;
        TextView reward_addshop;
        ImageView trustorno;
        TextView trustornotext;
        TextView comment;

        com.github.siyamed.shapeimageview.CircularImageView user;
    }

    @Override
    public int getCount() {
        return searchlist.size();
    }

    @Override
    public Object getItem(int position) {
        return searchlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.listview_item_view_comments, null);

            // Locate the TextViews in listview_item.xml
            holder.username = (TextView) view.findViewById(R.id.username);
            holder.date = (TextView) view.findViewById(R.id.date);
            holder.reward_rate = (TextView) view.findViewById(R.id.reward_rate);
            holder.reward_addshop = (TextView) view.findViewById(R.id.reward_addshop);
            holder.trustorno = (ImageView) view.findViewById(R.id.trustorno);
            holder.trustornotext = (TextView) view.findViewById(R.id.trustornotext);
            holder.comment = (TextView) view.findViewById(R.id.comment);

            // Locate the ImageView in listview_item.xml
            holder.user = (com.github.siyamed.shapeimageview.CircularImageView) view.findViewById(R.id.user);
            view.setTag(holder);
        }
        else {
            holder = (ViewHolder) view.getTag();
        }

        int counter = getCount();

        if (counter == 0) {
            Toast.makeText(ListViewAdapter_ViewComments.this.context, "", Toast.LENGTH_LONG).show();
        } else {
//            Log.d("username", String.valueOf(searchlist.get(position).getUserName()));
//            Log.d("date", String.valueOf(searchlist.get(position).getDate()));

            SimpleDateFormat dayFormat = new SimpleDateFormat("dd");
            String day = dayFormat.format((searchlist.get(position).getDate()));
//            Log.d("day", day);

            SimpleDateFormat monthFormat = new SimpleDateFormat("MM");
            String month = monthFormat.format((searchlist.get(position).getDate()));
//            Log.d("month", month);

            SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
            String year = yearFormat.format((searchlist.get(position).getDate()));
//            Log.d("year", year);

            SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
            String time = timeFormat.format((searchlist.get(position).getDate()));
//            Log.d("time", time);

            // Set the results into TextViews
            holder.username.setText(String.valueOf(searchlist.get(position).getUserName()));
            holder.date.setText(day + "/" + month + "/" + year + " - " + time);
            holder.reward_rate.setText(String.valueOf(searchlist.get(position).getReward_rating()));
            holder.reward_addshop.setText(String.valueOf(searchlist.get(position).getReward_shop()));
            holder.comment.setText(String.valueOf(searchlist.get(position).getComment()));

            if (searchlist.get(position).getApprovedFlag() == true){
                holder.trustorno.setImageResource(R.drawable.approved);
                holder.trustornotext.setText("Verified comment!");
            }
            else{
                holder.trustorno.setImageResource(R.drawable.nonapproved);
                holder.trustornotext.setText("Unverified comment!");
            }

            // Set the results into ImageView
            if (searchlist.get(position).getImage() != null) {
                imageLoader.DisplayImage(searchlist.get(position).getImage(), holder.user);
            }
            else{
                if (searchlist.get(position).getGender().equals("Female")){
                    imageLoader.DisplayImage(String.valueOf(R.drawable.female), holder.user);
                }
                else if (searchlist.get(position).getGender().equals("Male")){
                    imageLoader.DisplayImage(String.valueOf(R.drawable.male), holder.user);
                }
            }

            ParseQuery.clearAllCachedResults();
        }

At execution of my program, from AsyncTask, I can see from Logcat that inside the AsyncTask, the values I send to the results is different with each for.

03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/USER﹕ michael
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/COMMENT﹕ helloo9i
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/REWARD RATING﹕ 0
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/GET COMMENT﹕ helloo9i
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/USER﹕ marialena
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/COMMENT﹕ hello there
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/REWARD RATING﹕ 0
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/GET COMMENT﹕ hello there
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/USER﹕ marialena
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/COMMENT﹕ dkoc
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/REWARD RATING﹕ 0
03-29 18:05:48.406  10219-10571/guide_me_for_all.guide_me_for_all D/GET COMMENT﹕ dkoc

A photo from my comments in Parse.com to prove it:

enter image description here


The comments that I see at last, are three same comments,which represents the last row / last comment in Parse.com

enter image description here


So how can I display all the comments from Parse.com?

Upvotes: 0

Views: 317

Answers (1)

Michael Shi
Michael Shi

Reputation: 88

In your AsyncTask, you did:

result.setUserName(String.valueOf(username));
result.setApprovedFlag(shop.getBoolean("label_approved"));                                 
etc...

and then

searchlist.add(result);

However, you have result instantiated outside the loop. You are modifying values of the same instance object and inserting it in the list. Effectively, you are inserting the same object every time into your list, but just modifying that object with the value in the for loop when iterating through your Parse data.

What you need to do is instantiate a new object inside your for loop instead of outside.

Example:

for (ParseObject shop : ob) {
    final SetCommentsResultsConditions result = new SetCommentsResultsConditions();
    ...

}

Upvotes: 1

Related Questions