ListView适配器仅返回Parse.com对象的最后一行--Android

时间:2015-03-29 15:21:10

标签: java android android-listview parse-platform listview-adapter

使用AsyncTask,我正在对Parse.com中名为Shop_comments的对象进行查询。在查询结果中,我获取了用户名,即指向_User对象的指针,我从该用户检索信息。用户和评论的所有信息,我把它们放在结果中,这是SetCommentsResultsConditions类的一个对象,它从查询中获取数据并通过其他函数返回它们,在另一个片段活动中。


这是我的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);
            }
        }
    }

这是我从查询中发送数据的SetResuktsConditions类:

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

这是我的ListViewAdapter_ViewComments课程:

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

执行我的程序时,从AsyncTask,我可以从Logcat看到,在AsyncTask中,我发送给results的值与每个值不同。

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

我在Parse.com上发表的一张照片证明了这一点:

enter image description here


我最后看到的评论是三条相同的评论,代表Parse.com中的最后一行/最后一条评论

enter image description here


那么如何显示Parse.com的所有评论?

1 个答案:

答案 0 :(得分:1)

在你的AsyncTask中,你做了:

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

然后

searchlist.add(result);

但是,您在循环外部result进行了实例化。您正在修改同一实例对象的值并将其插入列表中。实际上,您每次都会在列表中插入相同的对象,但只需在迭代Parse数据时使用for循环中的值修改该对象。

您需要做的是在for循环内而不是在外部实例化一个新对象。

示例:

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

}