【问题标题】:Multiple listView item buttons are getting pressed even when I press the button in only one listview Item即使我仅在一个列表视图项中按下按钮,也会按下多个列表视图项按钮
【发布时间】:2018-11-04 05:17:09
【问题描述】:

我正在创建一个电子商务应用程序以在 listView 中显示产品列表。 每个列表项都有一个标题、图像和按钮,分别命名为喜欢、不喜欢和最喜欢的。

当用户按下特定列表项上的点赞按钮时,按钮的图像应更改为“点赞”并调用特定的 API。我已经使用 ArrayAdapter 类在 getView() 方法内的 onClickListener 中完成了此操作。

像这样。

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

    View listItemView = convertView;
    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);
        likeButton= (Button) 
 listItemView.findViewById(R.id.like_button);
        //likeButton.setClickable(currentProduct.getmLikeButton());
        dislikeButton = (Button) 
 listItemView.findViewById(R.id.dislike_button);

        favoritesButton = (Button) 
  listItemView.findViewById(R.id.favorite_button);
    }

    TextView productNameView = (TextView) 
 listItemView.findViewById(R.id.product_name);
    ImageView productImageView = (ImageView) 
  listItemView.findViewById(R.id.product_image);

    currentProduct = getItem(position);
    likeButton.setTag(position);
    dislikeButton.setTag(position);
    favoritesButton.setTag(position);

    likeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            int position=(Integer)v.getTag();
            currentProduct = getItem(position);
            productId = currentProduct.getProductId();

            likeButton = (Button) v.findViewById(R.id.like_button);

            Uri baseUri = Uri.parse(UNBXD_UACTION);
                Uri.Builder uriBuilder = baseUri.buildUpon();
                uriBuilder.appendQueryParameter("uaction", "like");
                uriBuilder.appendQueryParameter("user", "8222");
                uriBuilder.appendQueryParameter(PRODUCT_ID,productId);
                uriBuilder.appendQueryParameter(MATERIALL, "true");
                //uriBuilder.appendQueryParameter(USER_ID, "5");
                uriBuilder.appendQueryParameter(UNDO, "false");
                Intent intent = new 
                Intent(Intent.ACTION_WEB_SEARCH, uriBuilder.build());
                intent.getData();
                likeButton.setBackgroundResource(R.drawable.liked);
Log.v("liked url is ", uriBuilder.toString() + " " + 
String.valueOf(position));
        }
    });

我注意到的问题是,如果我单击 listView 中第一个项目上的点赞按钮,不仅第一个项目的点赞按钮会更改为“喜欢”,而且每隔三个 listView 项目中的点赞按钮也会变为显示更改为“喜欢”,但 API 调用只发生一次,并且正确地适合所单击的项目。

我无法准确弄清楚每三个项目的点赞按钮是如何以及为什么从我实际按下的点赞按钮开始被按下的,但(幸运的是)只有一个并且正确的 API 被调用。

请帮忙。

【问题讨论】:

    标签: android listview


    【解决方案1】:

    那是因为你的观点在ListView中被回收了。当convertView != null 表示你得到了一个回收的视图,你需要重新绑定视图。

    要解决此问题,您可能需要在您的 Product 类或适配器或某处中添加一个新属性,并简化点击侦听器中的操作:

    likeButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int position = (Integer)v.getTag();
            Product currentProduct = getItem(position);
            currentProduct.setLiked(true); // or false
            notifyDataSetChanged(); // notify so ListView can update
        }
    // continue...
    

    然后在您的getView 方法中:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        // ...
        likeButton = (Button) v.findViewById(R.id.like_button);
        likeButton.setBackgroundResource(currentProduct.isLiked() ? R.drawable.liked : R.drawable.something);
        // continue...
    

    【讨论】:

    • 谢谢你的回答,我还是有问题。如果我从 onClick() 方法中删除 setBackgroundResource 语句,按下按钮后图像甚至不会更改,但会调用 API。
    • @ShashankAC 对不起,我的错,你必须在currentProduct.setLiked(true) 之后打电话给notifyDataSetChanged,否则你的ListView 不知道是否有任何变化。我已经更新了答案。
    • 它不工作。我读到 notifyDataSetChanged() 仅在您使用 add()、insert()、remove() clear() 时才有效。 stackoverflow.com/questions/3669325/…
    • 当您的数据发生变化时它也可以工作,它甚至在方法名称中说明。除非您没有正确绑定视图,或者没有正确设置数据。
    • 我不知道你的意思是什么,你能告诉我你认为哪里可能错了吗?,我使用计数变量来查看在 getView 中究竟调用了多少次 onClick 方法在我单击一次喜欢按钮后。它只被调用一次,尽管每次我上下滚动 listView 时都会调用 getView()。 onClick 方法中的所有内容都只运行一次,除了用于更改图像的 setBackgroundResource 语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多