Android ListView重复点击项目

时间:2016-04-06 15:36:59

标签: android listview listadapter

我在listview中遇到问题。实际上我已经从Facebook Like Custom Feed实施了Facebook Like Feed 这是从MySQL数据库中获取数据。在每个列表项的底部,我有一个来自这个库的{Like}按钮Material Favourite Button。我的问题是,当我点击第一个列表项目时,第五个列表项目自动被喜欢,如果我点击第二个列表项目第四个列表项目被喜欢,依此类推。我尝试了我能做的一切,但没有解决这个问题。我试图将视图持有者添加到我的列表适配器类中,如此处的各种已回答的问题所示,但它没有解决我的问题。请帮忙!下面是我的适配器类:

 public class FeedListAdapter extends BaseAdapter {
    private Activity activity;

    private int lastPosition = -1;
    private DatabaseHandler db;
    ViewHolder holder;
      int id = 0;
    String email;

    private List<FeedItem> feedItems;

    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
        this.activity = activity;
        this.feedItems = feedItems;
    }

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

    @Override
    public Object getItem(int location) {
        return feedItems.get(location);
    }

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

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



        if (convertView == null){
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.feed_item, parent,false);
            holder = new ViewHolder();
            holder.materialFavoriteButtonNice =
                    (MaterialFavoriteButton) convertView.findViewById(R.id.like_anim);
            convertView.setTag(holder);
        }else{

            holder = (ViewHolder)convertView.getTag();
        }

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();


            TextView name = (TextView) convertView.findViewById(R.id.name);
            TextView timestamp = (TextView) convertView
                    .findViewById(R.id.timestamp);
            //get User Email
            db = new DatabaseHandler(activity.getApplication());
            db = new DatabaseHandler(activity.getApplication());
            HashMap<String, String> user = db.getUserDetails();
            email = user.get("email").toString();
            // End get User Email ID for sending it to db

        //Getting Views from Layout


        TextView statusMsg = (TextView) convertView
                .findViewById(R.id.txtStatusMsg);
        TextView url = (TextView) convertView.findViewById(R.id.txtUrl);
        final TextView like = (TextView) convertView.findViewById(R.id.like_box_no);
        TextView share = (TextView) convertView.findViewById(R.id.share_no);
        TextView comment = (TextView) convertView.findViewById(R.id.comment_no);
        NetworkImageView profilePic = (NetworkImageView) convertView
                .findViewById(R.id.profilePic);

        FeedImageView feedImageView = (FeedImageView) convertView
                .findViewById(R.id.feedImage1);
        //End Getting Views from Layout

        final FeedItem item = feedItems.get(position);
        name.setText(item.getName());

        // Converting timestamp into x ago format
        CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                Long.parseLong(item.getTimeStamp()),
                System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
        timestamp.setText(timeAgo);

        // Check for empty status message
        if (!TextUtils.isEmpty(item.getStatus())) {
            statusMsg.setText(item.getStatus());
            statusMsg.setVisibility(View.VISIBLE);
        } else {
            // status is empty, remove from view
            statusMsg.setVisibility(View.GONE);
        }
        // Chcek for empty Like
        if (!TextUtils.isEmpty(item.getLike())) {
            like.setText(item.getLike());
            like.setVisibility(View.VISIBLE);
        } else {
            // status is empty, remove from view
            like.setText("0");
        }
        // Chcek for empty Comment
        if (!TextUtils.isEmpty(item.getComment())) {
            comment.setText(item.getComment());
            comment.setVisibility(View.VISIBLE);
        } else {
            // status is empty, remove from view
            comment.setText("0");
        }
        // Chcek for empty Share
        if (!TextUtils.isEmpty(item.getShare())) {
            share.setText(item.getShare());
            share.setVisibility(View.VISIBLE);
        } else {
            // status is empty, remove from view
            share.setText("0");
        }
        if (item.getFav().equals("1")) {
            holder.materialFavoriteButtonNice.setFavorite(true, false);
            holder.materialFavoriteButtonNice.setVisibility(View.VISIBLE);

        } else {
            // status is empty, remove from view
            holder.materialFavoriteButtonNice.setFavorite(false, false);
        }

        // Checking for null feed url
        if (item.getUrl() != null) {
            url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
                    + item.getUrl() + "</a> "));

            // Making url clickable
            url.setMovementMethod(LinkMovementMethod.getInstance());
            url.setVisibility(View.VISIBLE);
        } else {
            // url is null, remove from the view
            url.setVisibility(View.GONE);
        }

        // user profile pic
        profilePic.setImageUrl(item.getProfilePic(), imageLoader);
        imageLoader.get(item.getProfilePic(), ImageLoader.getImageListener(profilePic, R.drawable._businessman, R.drawable._businessman));

        // Feed image
        if (item.getImge() != null) {
            feedImageView.setImageUrl(item.getImge(), imageLoader);
            feedImageView.setVisibility(View.VISIBLE);
            feedImageView
                    .setResponseObserver(new FeedImageView.ResponseObserver() {
                        @Override
                        public void onError() {
                        }

                        @Override
                        public void onSuccess() {
                        }
                    });
        } else {
            feedImageView.setVisibility(View.GONE);
        }

        //Animating the List View
        Animation animation = AnimationUtils.loadAnimation(activity.getApplication(), (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
        convertView.startAnimation(animation);
        lastPosition = position;
        //End Animating the List View

        //onClick Like


holder.materialFavoriteButtonNice.setOnFavoriteChangeListener(new MaterialFavoriteButton.OnFavoriteChangeListener() {
    @Override
    public void onFavoriteChanged(MaterialFavoriteButton buttonView, boolean favorite) {
        id = item.getId();
        Log.d("inFavChngeListner", "Clickd" + item.getId());
        Toast.makeText(activity.getApplication(), "Fav Changed : " + item.getId(), Toast.LENGTH_SHORT).show();
        if (favorite) {


            new send_json().execute();

        } else {

            holder.materialFavoriteButtonNice.setFavorite(false, true);
            new send_json_unlike().execute();
        }
    }
});
        return convertView;


    }
     static class ViewHolder {
        MaterialFavoriteButton materialFavoriteButtonNice;
    }

    //Sending Likes with email id and feed id to Remote Mysql Db
    public class send_json extends AsyncTask<String, String, JSONObject> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(!holder.materialFavoriteButtonNice.isFavorite())
            holder.materialFavoriteButtonNice.setFavorite(true, true);
        }

        @Override
        protected JSONObject doInBackground(String... params) {
            UserFunctions userFunction = new UserFunctions();

            JSONObject json = userFunction.like_func(email, String.valueOf(id));
            Log.d("BG Like, Email:" + email + "Id: " + String.valueOf(id), json.toString());
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            super.onPostExecute(jsonObject);
if(!holder.materialFavoriteButtonNice.isFavorite())
            holder.materialFavoriteButtonNice.setFavorite(true, true);
        }
    }
    //Sending Likes with email id and feed id to Remote Mysql Db
    public class send_json_unlike extends AsyncTask<String, String, JSONObject> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if(!holder.materialFavoriteButtonNice.isFavorite())
                holder.materialFavoriteButtonNice.setFavorite(false, true);
        }

        @Override
        protected JSONObject doInBackground(String... params) {
            UserFunctions userFunction = new UserFunctions();

            JSONObject json = userFunction.unlike_func(email, String.valueOf(id));
            Log.d("BG UnLike, Email:" + email + "Id: " + String.valueOf(id), json.toString());
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject jsonObject) {
            super.onPostExecute(jsonObject);
            if(!holder.materialFavoriteButtonNice.isFavorite())
                holder.materialFavoriteButtonNice.setFavorite(false, true);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不要使用ViewHolder实例全局。使其在功能范围内。并尝试改变逻辑。只更新数组中的模型,只需调用notifyDataSetChanged()方法。我认为这将解决您的问题。