信息从我的适配器中的一个ListView项目传递到另一个

时间:2016-08-28 17:27:46

标签: java android listview adapter

我的条件是检查likeCount_int >= 4。如果条件为true,则取消隐藏当前项中的TextView。不幸的是,当我快速滚动时,几乎所有其他列表项都隐藏了这个TextView。我怎样才能解决这个问题,因为列表视图之间没有数据传输?

public class MarketFeedAdapter extends ArrayAdapter<ParseObject> {

    protected Context mContext;
    protected List<ParseObject> mYeets;
    private MarketFeedAdapter adapter;

    ImageLoader profilePictureImageLoader;

    public MarketFeedAdapter(Context context, List<ParseObject> yeets) {
        super(context, R.layout.yeet_listview_item, yeets);
        mContext = context;
        mYeets = yeets;
        this.adapter = this;

        profilePictureImageLoader = new ImageLoader(new ProfilePictureFileCache(mContext));
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.yeet_listview_item, null);

            holder = new ViewHolder();
            holder.username = (TextView)convertView.findViewById(R.id.username);
            holder.fullName = (TextView)convertView.findViewById(R.id.fullName);
            holder.messageText = (TextView)convertView.findViewById(R.id.messageText);
            holder.time = (TextView)convertView.findViewById(R.id.time);
            holder.profilePicture = (ImageView) (convertView.findViewById(R.id.profilePicture));
            holder.likeImage = (ImageView) convertView.findViewById(R.id.likeImage);
            holder.likeCount = (TextView) convertView.findViewById(R.id.likeCount);
            holder.premiumContent = (TextView) convertView.findViewById(R.id.premiumContent);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        final ParseObject yeets = mYeets.get(position);

        Date createdAt = yeets.getCreatedAt();
        long now = new Date().getTime();
        String convertedDate = DateUtils.getRelativeTimeSpanString(createdAt.getTime(), now, DateUtils.SECOND_IN_MILLIS).toString();

        holder.username.setText(yeets.getString(ParseConstants.KEY_SENDER_NAME));

        if (!(yeets.getString(ParseConstants.KEY_SENDER_FULL_NAME).equals(""))) {
            holder.fullName.setText(yeets.getString(ParseConstants.KEY_SENDER_FULL_NAME));
        } else {
            holder.fullName.setVisibility(View.GONE);
        }

        holder.messageText.setText(yeets.getString(ParseConstants.KEY_NOTIFICATION_TEXT));
        holder.time.setText(convertedDate);

        int likeCount_int = yeets.getInt(ParseConstants.KEY_LIKE_COUNT);
        String likeCount_string = Integer.toString(likeCount_int);
        holder.likeCount.setText(likeCount_string);

        if (likeCount_int >= 4) {
            holder.premiumContent.setVisibility(View.VISIBLE);
            Typeface tf_bold = Typeface.createFromAsset(getContext().getAssets(), "fonts/Lato-Bold.ttf");
            holder.premiumContent.setTypeface(tf_bold);
        }

        String profilePictureURL = yeets.getString(ParseConstants.KEY_SENDER_PROFILE_PICTURE);

        // Asynchronously display the profile picture downloaded from Parse
        if(profilePictureURL != null) {
            profilePictureImageLoader.DisplayImage(profilePictureURL, holder.profilePicture, null);
        } else {
            holder.profilePicture.setImageResource(Integer.parseInt(String.valueOf(R.drawable.ic_profile_pic_add)));
        }

        holder.username.setOnClickListener(view -> {
            view.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.image_click));
            retrievePointerObjectId(yeets);
        });

        holder.fullName.setOnClickListener(view -> {
            view.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.image_click));
            retrievePointerObjectId(yeets);
        });

        holder.profilePicture.setOnClickListener(view -> {
            view.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.image_click));
            retrievePointerObjectId(yeets);
        });

        holder.likeImage.setOnClickListener(v -> {
            v.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.image_click));
            createLike(position);
        });

        convertView.setOnClickListener(v -> retrievePointerObjectIdForComment(yeets));

        convertView.setOnLongClickListener(v -> {

            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
            dialogBuilder.setTitle("Delete");
            dialogBuilder.setMessage("Do you want to delete this Yeet?");
            dialogBuilder.setPositiveButton("Yes", (dialog, which) -> {

                deleteComment(position);

            });
            dialogBuilder.setNegativeButton("No", (dialog, which) -> {
            });
            AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.show();

            return false;
        });

        return convertView;
    }

 // A view holder for all the iterative elements in our list, i.e. username, fullName, etc.
    public static class ViewHolder {
        TextView username;
        TextView fullName;
        TextView messageText;
        TextView time;
        ImageView profilePicture;
        ImageView likeImage;
        TextView likeCount;
        TextView premiumContent;
    }

0 个答案:

没有答案