简体   繁体   English

在android每个listview项中单击后的Chage按钮文本

[英]Chage button text after clicking it in android each listview item

I have a listview which set up by using a custom listAdapter. 我有一个使用自定义listAdapter设置的listview。 There is a 'Like' button in each listView item to like. 每个listView项目中都有一个“喜欢”按钮,供您点赞。 After clicking button I need to change the text of this button to 'Unlike'. 单击按钮后,我需要将此按钮的文本更改为“不喜欢”。 But the following code changes the text of all items buttons to 'Unlike'. 但是以下代码将所有项目按钮的文本更改为“不喜欢”。 How to solve it. 如何解决。

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

    final HomeItem hi = itemsArrayList.get(position);
    String type = hi.getType();

    final ViewHolder viewHolder;

    if(convertView==null) { 

        if(type.equalsIgnoreCase("Job")) {
            convertView = mInflater.inflate(R.layout.home_row, null);
        } else if(type.equalsIgnoreCase("Work")) {
            convertView = mInflater.inflate(R.layout.home_row_work, null);
        }

        viewHolder = new ViewHolder();
        viewHolder.pic = (ImageView) convertView.findViewById(R.id.pic);
        viewHolder.lblCaptionName = (TextView) convertView.findViewById(R.id.caption_name);
        viewHolder.lblCaptionText = (TextView) convertView.findViewById(R.id.caption_text);
        viewHolder.lblCaptionItem = (TextView) convertView.findViewById(R.id.caption_item);
        viewHolder.lblTime = (TextView) convertView.findViewById(R.id.time);

        if(type.equalsIgnoreCase("Job")) {
            JobViewHolder jobHolder;
            jobHolder = new JobViewHolder();
            jobHolder.lblCategory = (TextView) convertView.findViewById(R.id.category);
            jobHolder.lblPost = (TextView) convertView.findViewById(R.id.post);
            jobHolder.btnLike = (Button) convertView.findViewById(R.id.likeBtn);
            viewHolder.jobViewHolder = jobHolder;
        } else {
            WorkViewHolder workHolder;
            workHolder = new WorkViewHolder();
            workHolder.lblTitle = (TextView) convertView.findViewById(R.id.title);
            workHolder.lblDescription = (TextView) convertView.findViewById(R.id.description);
            workHolder.btnLike = (Button) convertView.findViewById(R.id.likeBtn);
            viewHolder.workViewHolder = workHolder;
        }
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.lblCaptionName.setText(app.buildString(context, "<u>"+hi.getName()+"</u>",hi.getName()));
    viewHolder.lblCaptionName.setMovementMethod(LinkMovementMethod.getInstance());
    viewHolder.lblCaptionName.setLinkTextColor(Color.BLACK);

    viewHolder.lblCaptionText.setText(hi.getCaption());

    viewHolder.lblCaptionItem.setText(app.buildStringToOpenJobActivity(context, "<u>"+hi.getPost()+"</u>",hi.getUid()));
    viewHolder.lblCaptionItem.setMovementMethod(LinkMovementMethod.getInstance());
    viewHolder.lblCaptionItem.setLinkTextColor(Color.BLACK);

    viewHolder.lblTime.setText(hi.getDate());

    if(type.equalsIgnoreCase("Job")) {
        if(viewHolder.jobViewHolder!=null) {
            viewHolder.jobViewHolder.lblCategory.setText(hi.getJobItem().getCategory());
            viewHolder.jobViewHolder.lblPost.setText(hi.getJobItem().getPost());

            viewHolder.jobViewHolder.btnLike.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    viewHolder.jobViewHolder.btnLike.setText("Unlike");

                }
            });
        }

    } else if(type.equalsIgnoreCase("Work")) {
        if(viewHolder.workViewHolder!=null) {
            viewHolder.workViewHolder.lblTitle.setText(hi.getWorkItem().getTitle());
            viewHolder.workViewHolder.lblDescription.setText(hi.getWorkItem().getDescription());

            viewHolder.workViewHolder.btnLike.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    viewHolder.workViewHolder.btnLike.setText("Unlike");

                }
            });
        }
    }

    if (hi.getImage() != null) {
        viewHolder.pic.setImageBitmap(hi.getImage());
    } else {
        viewHolder.pic.setImageResource(R.drawable.user);
    }     

    return convertView;
}


static class ViewHolder {
    ImageView pic;
    TextView lblCaptionName;
    TextView lblCaptionText;
    TextView lblCaptionItem;
    TextView lblTime;
    JobViewHolder jobViewHolder;
    WorkViewHolder workViewHolder;
}

static class JobViewHolder {
    TextView lblCategory;
    TextView lblPost;
    Button btnLike;
}

static class WorkViewHolder {
    TextView lblTitle;
    TextView lblDescription;
    Button btnLike;
}

You need to save the like status in your data source. 您需要在数据源中保存“ like status The ListView 's rows are reused once they are out of view, so as soon as you set one to be liked, it will be reused for another row that will seem liked as well. ListView的行在不可见时将被重用,因此,一旦您将其设置为“喜欢”,它就会被重新用于看起来也很喜欢的另一行。

Just add a class member called liked or similar to your datasource class (seems to be HomeItem in your case), set that in your onClickListener (you might want to set btnLike 's tag to the row index, so you know which item you have to modify), and then call notifyDataSetChanged() on your adapter. 只需添加一个类的成员叫liked或类似于您的数据源类(似乎是HomeItem你的情况),设置在onClickListener(您可能需要设置btnLike的标签行索引,那么你知道你有哪些项目进行修改),然后在您的适配器上调用notifyDataSetChanged()

You'll also have to add code that marks a row as liked depending on the datasource item's value. 您还必须根据数据源项的值添加将行标记为喜欢的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM