listItem上显示错误的listitem点击android

时间:2016-03-16 06:16:49

标签: android listview android-adapter

我是android的新手,并在listView上使用自定义listitem.I我有一个listView与自定义控件包括一些图像和labes,我的问题是我点击一个项目它给了我错误的listItem detail.most might it给出下面列出的项目数据,anybuddy可以帮我修复它,我的代码是:

package one.tusk.stush.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.company.stush.R;

import java.util.List;

import one.tusk.stush.connect.Post;
import one.tusk.stush.views.PostListItem;

public class TimelineAdapter extends ArrayAdapter<Post> {

    private final LayoutInflater mInflater;


    public TimelineAdapter(Context context) {

        super(context, R.layout.list_item_post);
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



    public void setData(List<Post> data) {

        clear();
        if (data != null) {
            addAll(data);
            notifyDataSetChanged();
        }

    }


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

        Post post = getItem(position);
        PostListItem view;
        if (convertView == null) {
            view =  (PostListItem) mInflater.inflate(R.layout.list_item_post, parent, false);


        } else {
            view = (PostListItem) convertView;

        }
        view.setPost(post);

        return view;
    }
}

点击活动

@Override
public void onClick(final View button) 

{

            if (button == buttonLike) {
                if (!mPost.likedThisPost) {
                    buttonLike.setCompoundDrawablesWithIntrinsicBounds(
                            R.drawable.btn_likes, 0, 0, 0);
                } else {
                    buttonLike.setCompoundDrawablesWithIntrinsicBounds(
                            R.drawable.btn_liked, 0, 0, 0);
                }
            } else if (button == buttonLikes) {
                Intent likesIntent = new Intent(PostListItem.this.getContext(),
                        LikesActivity.class);
                likesIntent.putExtra("postID", mPost.postID);
                PostListItem.this.getContext().startActivity(likesIntent);
            } else if (button == buttonComment || button == mNumberOfComments) {
                Intent commentIntent = new Intent(PostListItem.this.getContext(),
                        CommentsActivity.class);
                commentIntent.putExtra("postID", mPost.postID);
                PostListItem.this.getContext().startActivity(commentIntent);

            } else if (button == buttonShare) {
                // Log.e("Click button Share", "Success");
                album = mPost.inAlbum;

                showPopupMenu(button);
            }
            else if(button == imageViewPostImage) {
                //Toast.makeText(PostListItem.this.getContext(),"item Clicked...!!!",Toast.LENGTH_LONG).show();
                if(Const.post_det.equals("0")) {
                    Intent intentPostDetail = new Intent(PostListItem.this.getContext(), PostDetailActivity.class);
                    Post post = mPost;
                    System.out.print("========MY POST IS======>" + mPost.toString());
                    intentPostDetail.putExtra("Post", post);
                    intentPostDetail.putExtra("flag", "post");
                    intentPostDetail.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    getContext().startActivity(intentPostDetail);
                    flag_det = "1";
                    //Const.post_det = "1";
                }

            }else if(button == imguser) {
                flag_pro = "1";
                showUser();
            }

            new AsyncTask<Void, Void, Boolean>() {

                @Override
                protected Boolean doInBackground(Void... params) {

                    Connect sharedConnect = Connect.getInstance(getContext());
                    if(button == buttonShare)
                    {
                        //return sharedConnect.deletePost(mPost.postID);
                    }
                    if (button == buttonLike) {
                        if (!mPost.likedThisPost) {
                            ///buttonLike.setCompoundDrawablesWithIntrinsicBounds(
                            //  R.drawable.btn_likes, 0, 0, 0);
                            return sharedConnect.likePost(mPost.postID);
                        } else {
                            //buttonLike.setCompoundDrawablesWithIntrinsicBounds(
                            //  R.drawable.btn_liked, 0, 0, 0);
                            return sharedConnect.unlikePost(mPost.postID);
                        }
                    } else if (button == buttonLikes) {
                        return true;
                    } else if (button == buttonComment
                            || button == mNumberOfComments) {
                        return true;
                    } else {
                        return null;
                    }
                }

                protected void onPostExecute(Boolean result) {

                    if (button == buttonLike) {
                        if (result && !mPost.likedThisPost) {
                            buttonLike.setCompoundDrawablesWithIntrinsicBounds(
                                    R.drawable.btn_likes, 0, 0, 0);
                            mPost.likedThisPost = true;
                            mPost.postLikesCount++;
                        } else if (result) {
                            buttonLike.setCompoundDrawablesWithIntrinsicBounds(
                                    R.drawable.btn_liked, 0, 0, 0);
                            mPost.likedThisPost = false;
                            mPost.postLikesCount--;
                        }
                        buttonLikes.setText(likeOrLikes(mPost.postLikesCount));
                    } else if (button == buttonLikes) {

                    } else if (button == buttonComment
                            || button == mNumberOfComments) {

                    } else if (button == buttonShare) {

                    }else if(button == imageViewPostImage){
    //                  Intent intentPostDetail = new Intent(PostListItem.this.getContext(), PostDetailActivity.class);
    //                  Post post = mPost;
    //                  intentPostDetail.putExtra("Post", post);
    //                  getContext().startActivity(intentPostDetail);
                        if(flag_det.equals("0")) {
                            Intent intentPostDetail = new Intent(PostListItem.this.getContext(), PostDetailActivity.class);
                            Post post = mPost;
                            System.out.print("========MY POST IS======>" + mPost.toString());
                            Toast.makeText(getContext(),post.postID,Toast.LENGTH_LONG).show();

                            intentPostDetail.putExtra("flag", "post");
                            intentPostDetail.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                            intentPostDetail.putExtra("Post", post);
                            getContext().startActivity(intentPostDetail);
                            flag_det = "1";
                        }else {
                            flag_det = "0";
                        }
                    }

                    else if(button == imguser) {
                        if(flag_pro.equals("0")) {
                            showUser();
                            flag_pro = "1";
                        }else {
                            flag_pro = "0";
                        }
                    }
                }

            }.execute();
        }

在此我的点击事件为imageViewPostImage

1 个答案:

答案 0 :(得分:0)

这是我最近项目的 getView 。我有listView,其中包含几个按钮,你看我实现了每个按钮的 onClickListener

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

    final PostModel item = getItem(position);

    int itemType = getItemViewType(position);

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListener.onItemClick(null, null, position, getItemId(position));
        }
    };

            final ViewHolderVideo holder;
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.row_list_videos, parent, false);
                holder = new ViewHolderVideo();
                holder.videoImage = (ImageView) convertView.findViewById(R.id.image_video_preload);
                holder.title = (TextView) convertView.findViewById(R.id.text_video_title);
                holder.date = (TextView) convertView.findViewById(R.id.text_video_date);
                holder.share = (TextView) convertView.findViewById(R.id.btn_share);
                holder.download = (TextView) convertView.findViewById(R.id.btn_download);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolderVideo) convertView.getTag();
            }

            if (item != null) {

                String thumbLink = item.getThumbnailLink();
                if (!TextUtils.isEmpty(thumbLink)) {
                    ImageLoader.displayImage(mContext, holder.videoImage, thumbLink);
                }


                if (!TextUtils.isEmpty(item.getTitlePlain())) {
                    holder.title.setText(item.getTitlePlain());
                } else {
                    holder.title.setText("");
                }


                if (!TextUtils.isEmpty(item.getDate())) {
                    holder.date.setText(item.getDate());

                } else {
                    holder.date.setVisibility(View.GONE);
                }


                holder.share.setOnClickListener(new  View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AnimationHelper.changeIconAnim(holder.share, R.string.icon_mail_reply);
                        IntentHelper.shareOnSocials(mContext, item.getTitlePlain() + "\n" + item.getMediaLink());
                    }
                });

                holder.download.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AnimationHelper.changeIconAnim(holder.download, R.string.icon_download);
                        downloadVideoFile(item);
                    }
                });

                holder.videoImage.setOnClickListener(onClickListener);
                holder.title.setOnClickListener(onClickListener);
                holder.date.setOnClickListener(onClickListener);
                convertView.setOnClickListener(onClickListener);

            }
            return convertView;

}