在基本适配器中滚动后

在基本适配器中滚动后

本文介绍了在基本适配器中滚动后,TextView值变回先前的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究这个问题,找不到解决方案.关于我的自定义列表视图的所有内容似乎都能正常执行.当我单击holder.feedUpVoteButton时,文本正确更改了+=1.但是,当我向下滚动并向上滚动时,文本值将还原为单击之前的值.

I've been researching this forever and can't find a solution. Everything about my custom list view seems to perform correctly. When I click on the holder.feedUpVoteButton, the text changes +=1 correctly. However, when I scroll down and scroll back up, the text value is reverted to the value it had before it was clicked.

我拒绝使用notifyDatasetChanged,因为我没有添加任何东西或从列表中删除任何东西.

I refuse to use notifyDatasetChanged because I am not adding anything or removing anything from the list.

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;

    }

}

推荐答案

经过不断的反复试验.我终于想出了滚动后如何更改textview的方法.我的问题是我得到了错误的ParseObject值.我的主要活动包含ParseQuery,而我是从Hashmap();获取likes的.但是,由于某种原因,我无法直接传递Likes的值,因此我传递了ParseObject本身.因此,不需要在我的BaseAdapter类中进行查询.然后,我在GetView();中实现了以下几行代码,以回答我的原始问题:

After continuous trial and error. I finally figured out how to change a textview after it scrolls. My problem was I was getting the wrong ParseObject value. My main activity contains a ParseQuery and I was getting the likesfrom a Hashmap(); However, for some reason I couldn't pass the value of likes directly so I passed the ParseObject itself. Therefore, needing no query in my BaseAdapter Class. Then, I implemented these lines of code in GetView(); to answer my original question:

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

    }

这篇关于在基本适配器中滚动后,TextView值变回先前的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:11