持有人返回错误的价值观

时间:2015-06-24 10:20:14

标签: android adapter android-viewholder

在我的getView方法中,当我尝试单击视图的图像按钮时,其相邻视图的图像按钮被单击并且对于其他UI来说是相同的,这意味着它每次都返回错误的视图。我不是能够找出问题所在。

  

适配器类

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView iv;
    //View v = convertView;
    String content = null;
    LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView==null) {
        convertView=inflater.inflate(R.layout.row_item2,parent,false);
        holder = new ViewHolder();  
        pos=position;



        holder.read_more=(TextView)convertView.findViewById(R.id.readMore);
        holder.share=(TextView)convertView.findViewById(R.id.share);
        holder.heading = (TextView)convertView.findViewById(R.id.heading);
        holder.date = (TextView)convertView.findViewById(R.id.date);
        holder.message = (TextView)convertView.findViewById(R.id.message);
        holder.gridview=(GridView)convertView.findViewById(R.id.grid_view);
        holder.comment_executed=(ImageView)convertView.findViewById(R.id.comments);
        holder.image=(ImageView)convertView.findViewById(R.id.img);
        holder.like_img=(ImageButton)convertView.findViewById(R.id.like);
        convertView.setTag(holder);

    }else {
        holder=(ViewHolder)convertView.getTag();
    }
    holder.read_more.setVisibility(View.GONE);
    holder.image.setVisibility(View.GONE);
    holder.gridview.setVisibility(View.GONE);
    holder.heading.setText(Html.fromHtml(modelList.get(position).getTitle()));
    //String setDate=parseDate(modelList.get(position).getDate());
    holder.date.setText(modelList.get(position).getDate());
    holder.like_img.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(context,holder.heading.getText()+" "+modelList.get(position).getTitle(), Toast.LENGTH_SHORT).show();
            holder.like_img.setImageResource(R.drawable.up_arrow_hover);
        }
    });
    holder.gridview.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    // Sending image id to FullScreenActivity
                    Intent i = new Intent(context, FullImageActivity.class);
                    // passing array index
                    i.putExtra("id", position);
                   context.startActivity(i);
                }
            });
    content=Html.fromHtml(modelList.get(position).getContent())+"";
    size=content.length();

                    String altered_string="";
        if(size>200) {
            altered_string=content.substring(0,200);
            holder.message.setText(altered_string+"...");
            holder.read_more.setVisibility(View.VISIBLE);
            holder.read_more.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    intent_to_details=new Intent(v.getContext(),PostDetail.class);
                    Log.e("after",position+"");
                    intent_to_details.putExtra("list", modelList.get(position));
                    ((Activity)context).startActivity(intent_to_details);

                }
            });
        }else {
        holder.message.setText(Html.fromHtml(modelList.get(position).getContent()));
        }
        holder.message.setMovementMethod(LinkMovementMethod.getInstance());
        holder.message.setLinksClickable(true);

        if(modelList.get(position).getModelChild().size() != 0) {
            if(modelList.get(position).getModelChild().size() ==1) {
                Bitmap bit;
                child_list=modelList.get(position).getModelChild();
                flag=true;
                holder.image.setVisibility(View.VISIBLE);
                imageLoader.DisplayImage(child_list.get(0).getImage_normal(),loader,holder.image);
                //new DoAsync().execute(child_list);

            }else {
            holder.gridview.setVisibility(View.VISIBLE);
            child_list=modelList.get(position).getModelChild();
            new DoAsync().execute(modelList.get(position).getModelChild());
            }
        }
        holder.comment_executed.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
        SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, ((Activity)context).MODE_PRIVATE );
            if(prefs.getString("succ", "").equals("success") || (Session.getActiveSession() != null)) {
                Toast.makeText(context,holder.heading.getText(), Toast.LENGTH_SHORT).show();
                int post_id=Integer.parseInt(modelList.get(position).getId());
                intent_to_commentClass=new Intent(context,Comments.class);
                Log.e("post id sent: ","position:  "+position+"  post id:   "+post_id+"");
                intent_to_commentClass.putExtra("post_id", post_id);
                context.startActivity(intent_to_commentClass);

            }else {
                intent_to_login=new Intent(context,LoginClass.class);
                context.startActivity(intent_to_login);
                Toast.makeText(context, "Please login first to like or comment", Toast.LENGTH_SHORT).show();
            }
            }
        }); 




    return convertView;
}

1 个答案:

答案 0 :(得分:0)

您需要在执行任何更改后通知。

holder.like_img.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(context,holder.heading.getText()+" "+modelList.get(position).getTitle(), Toast.LENGTH_SHORT).show();
            holder.like_img.setImageResource(R.drawable.up_arrow_hover);
            notifyDataSetChanged();//notify after any changes
        }
    });