在基础适配器中滚动后,TextView值将更改回先前的值

时间:2016-11-28 09:18:11

标签: android listview baseadapter

我一直在研究这个问题并且无法找到解决方案。我的自定义列表视图的一切似乎都正确执行。当我点击holder.feedUpVoteButton时,文字会正确更改+=1。但是,当我向下滚动并向上滚动时,文本值将恢复为单击之前的值。

我拒绝使用notifyDatasetChanged因为我没有添加任何内容或从列表中删除任何内容。

public class CustomFeedListViewAdapter extends BaseAdapter{

    CustomFeedListViewAdapter customFeedListViewAdapter;
    Date createdAt, currentDate; int num;
    static HashMap<String, String> oneData = new HashMap<String, String>();
    HashMap<String, String> iFeed = new HashMap<>();


    private String likesString;

    String upVoteClicked, downVoteClicked;
    HashMap<String, String> mFeed = new HashMap<>();
    List<ParseObject>  mObjects;

    private ParseObject parseObFeed;

        CustomFeedListViewAdapter(Context context, ArrayList<HashMap<String, String>> data) {

        super();
        this.mContext = context;
        GlobalFeedTab.arrayFeedList = data;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


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

    @Override
    public Object getItem(int i) {
        return GlobalFeedTab.arrayFeedList.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

         likes = new int[GlobalFeedTab.arrayFeedList.size()];
         countryNames = new int[GlobalFeedTab.arrayFeedList.size()];
        dateNames = new String[GlobalFeedTab.arrayFeedList.size()];

       final ViewHolder holder;

        if (view == null) {

            position = i;

            view = inflater.inflate(R.layout.feed_list_row, viewGroup, false);

            holder = new ViewHolder();


            holder.feedNumOfLikes = (TextView) view.findViewById(R.id.feedNumofLikes);
            holder.feedUpVoteButton = (Button) view.findViewById(R.id.feedUpVoteButton);



        } else {
            position = i;
            holder = (ViewHolder) view.getTag();
        }


        mFeed = GlobalFeedTab.arrayFeedList.get(position);

        holder.feedNumOfLikes.setText(mFeed.get("likes"));

        likesString = mFeed.get("likes");
        likes[position] = Integer.valueOf(likesString);

        holder.feedUpVoteButton.setTag(position);

        ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("FeedItem");
        query2.setLimit(250);
        query2.addDescendingOrder("createdAt");

        query2.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(final List<ParseObject> objects, ParseException e) {

                if (e == null) {

                    for ( final ParseObject object : objects) {



                        holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                int pos = (Integer) v.getTag();
                                parseObFeed = objects.get(pos);
                                username = parseObFeed.getString("username");
                                createdAt = parseObFeed.getDate("createdAt");

                                likes[pos] += 1;                           
                                parseObFeed.put("likes", likes[pos]);
                                holder.feedNumOfLikes.setText(String.valueOf(parseObFeed.getInt("likes")));

                                parseObFeed.put(ParseUser.getCurrentUser().getUsername() + "upvoteClicked", true);

                                parseObFeed.saveInBackground();
                            }
                        });                

            }
        });


        return view;
    }


    private class ViewHolder {

        ImageView feedProfilePic;
        TextView feedUsername;
        TextView feedNumOfLikes;
        TextView feedFeedItem;
        TextView feedDate;
        TextView feedNumofReplies;
        Button feedUpVoteButton;
        Button feedDownVoteButton;
        Button feedCommentButton;
        ListView feedListView;

    }

}

3 个答案:

答案 0 :(得分:2)

实际上这就是整个理论。当你增加一个值时。你必须在列表中从你获取数据的位置增加该值。因为当你向下滚动时,上面的行将会丢失。当你向后滚动时up,list将从列表值中再次加载。所以你要做的就是增加列表中的值,即GlobalFeedTab.arrayFeedList this 链接有关详细信息,适用于寻求更多详细信息的任何人 当你写这一行

likes[pos] += 1;

您还必须增加你的arraylist(mFeed)中的值,然后编写notifyDataSetChanged();

答案 1 :(得分:1)

每次调用getView时,都会重新制作

likes = new int[GlobalFeedTab.arrayFeedList.size()];

滚动时不会保存,并且您单击的行将离开并重新进入显示。持有者可以保持这个整数值。

private class ViewHolder {
    int likes;
    TextView feedNumOfLikes; 
}

并相应地更新

holder.feedUpVoteButton
    .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            holder.feedNumOfLikes.setText(String.valueOf(++holder.likes));

但是,您还需要更新parse对象。

parseObFeed.put("likes", holder.likes);
parseObFeed.saveInBackground();
// notifyDataSetChanged(); // Up to you. Might not work without
  

我拒绝使用notifyDatasetChanged因为我没有添加任何内容或从列表中删除任何内容。

您正在修改数据。调用它将使您的适配器数据与Parse Server中存储的数据一致。

修改

这变得复杂,因为你使每个“行”都有一些与它相关联的“嵌套”对象列表。我想你可以把整个清单存放在持有人手中。

private class ViewHolder {
    // Java variables to hold onto
    int likes;
    List<ParseObject> feedItems = new ArrayList<ParseObject>();

    // Android views to bind those values to
    TextView feedNumOfLikes; 
}

当在适配器内部时,您调用Parse来获取适配器中每个项目的辅助列表,但您只需将其存储在持有者中。 query2返回时,不要对它做任何其他事情。

你真正的问题是,query2甚至还没有回归。肯定是这是一些数据列表,但你真的想在List中显示List吗?此外,适配器的每个项目都从Parse查询完全相同的数据。我认为您需要一个过滤器...然后,您的position变量在GlobalFeedTab.arrayFeedListList<ParseObject>之间不对应,因为它们是不同的列表。

以下是我可以指出的一些评论。

    else {
        holder = (ViewHolder) view.getTag();
    }

    // You don't need 'position', it is just the 'i' value...
    mFeed = GlobalFeedTab.arrayFeedList.get(i);

    // This is stored in the Activity, I guess? 
    likesString = mFeed.get("likes");
    holder.feedNumOfLikes.setText(likesString);

    holder.feedUpVoteButton.setTag(i);

    ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("FeedItem");

    // This is getting some related "FeedItem" for the current row
    // Just store the info, don't do anything with it yet
    query2.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(final List<ParseObject> objects, ParseException e) {
            if (e == null) {
                holder.feedItems.clear();
                holder.feedItems.addAll(objects);
                notifyDataSetChanged();
            }
        } 
    };

    // Here, you can setup the button for the current row
    holder.feedUpVoteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            int pos = (Integer) v.getTag();

            // This is wrong... 'pos' is the position of the adapter! Not the index of List<ParseObject> results from earlier
            // And this is a very clear ArrayIndexOutOfBounds error...
            // The adapter could be shorter/longer than the holder.feedItems
            parseObFeed = holder.feedItems.get(pos);

            // !! TODO: Figure out which parseObFeed you actually want to use here 

            username = parseObFeed.getString("username");
            createdAt = parseObFeed.getDate("createdAt");

            holder.likes++;
            parseObFeed.put("likes", holder.likes);

            // This is the value that "resets" when you scroll. 
            // parseObFeed has not yet been saved, so when you scroll, the data isn't changed. 
            holder.feedNumOfLikes
                 .setText(String.valueOf(parseObFeed.getInt("likes")));

            // Not really sure what this does - You can store an array of likes rather than one field per username
            parseObFeed.put(ParseUser.getCurrentUser().getUsername() + "upvoteClicked", true);

            parseObFeed.saveInBackground();
            notifyDataSetChanged();
        }
    });    
}

我不记得saveInBackground是否有回调,但这可能是值得关注的事情

答案 2 :(得分:1)

经过连续反复试验。我终于想出了如何在滚动后更改textview。我的问题是我得到了错误的ParseObject值。我的主要活动包含ParseQuery,我从likes获得了Hashmap();但是,出于某种原因,我无法直接传递喜欢的值,所以我通过了ParseObject本身。因此,我的BaseAdapter班级不需要查询。然后,我在GetView();中实现了这些代码行来回答我原来的问题:

   holder.upvote[position] =  holder.parseObList[position].getBoolean(ParseUser.getCurrentUser().getUsername() + "upvoteClicked");
    holder.downvote[position] =  holder.parseObList[position].getBoolean(ParseUser.getCurrentUser().getUsername() + "downvoteClicked");

    if(holder.upvote[position]  ){
        holder.feedUpVoteButton.setBackgroundResource(R.drawable.arrowclicked);
        holder.feedNumOfLikes.setText(String.valueOf(holder.likes[position]));

    }
    else if(!holder.upvote[position]){
        holder.feedUpVoteButton.setBackgroundResource(R.drawable.arrowunclicked);
        holder.feedNumOfLikes.setText(String.valueOf(holder.likes[position]));


    }
    if(holder.downvote[position]){
        holder.feedDownVoteButton.setBackgroundResource(R.drawable.arrowclicked);

    }
    else if(!holder.downvote[position]){
        holder.feedDownVoteButton.setBackgroundResource(R.drawable.arrowunclicked);

    }