android:listView项目有按钮

时间:2014-04-15 07:08:32

标签: android android-listview android-button

enter image description here

我的列表视图项中有一些条目。

当我点击项目时首先像按钮....只有像按钮颜色值的最后项目正在改变。 当我点击项目第二个像按钮....只有像按钮颜色值的最后项目正在改变。 但是

当我点击第一项时,我得到position = 0和offer_id = O101

当我点击第二项时,我得到position = 1并且offer_id = O103

当我点击第三项时,我得到position = 2和offer_id = O104

每当我点击任何列表视图项目的任何类似按钮时......列表视图颜色的最后一项的类似按钮就会改变。

我希望每当我点击列表项的任何类似按钮时,只有该列表项的类似按钮才会改变。 如何纠正这个问题。 这是我的代码......

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

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.offer_list_item,
                    parent, false);
            holder.tv1 = (TextView) convertView
                    .findViewById(R.id.offer_name);
            holder.tv2 = (TextView) convertView
                    .findViewById(R.id.expiry_date);
            holder.b1 = (Button) convertView.findViewById(R.id.like_button);

            holder.b2 = (Button) convertView
                    .findViewById(R.id.share_button);
            holder.tvoid = (TextView) convertView
                    .findViewById(R.id.offer_id);
            holder.tv = (TextView) convertView.findViewById(R.id.like);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final OfferItem it = item.get(position);
        holder.tv1.setText(it.getoffer_name());
        final String message = holder.tv1.getText().toString();
        holder.tv2.setText("Offer ends: " + it.getexpiry_date());
        holder.tvoid.setText(it.getoffer_id());
        holder.tv.setText("Like");
        id_offiers_list.add(it.getoffer_id());

        holder.b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String offerid = id_offiers_list.get(position);
                System.out.println(offerid);
                if (clicked) {
                    System.out.println(position);
                    holder.b1
                            .setBackgroundResource(R.drawable.like_icon_hover);
                    holder.tv.setTextColor(Color.parseColor("#eaa232"));
                    clicked = false;

                } else {
                    System.out.println(position);
                    holder.b1.setBackgroundResource(R.drawable.like_icon);
                    holder.tv.setTextColor(Color.parseColor("#454545"));
                    clicked = true;
                }
            }
        });

        holder.b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent facebookIntent = new Intent(Intent.ACTION_SEND);
                facebookIntent.setType("text/plain");
                // facebookIntent.setPackage("com.facebook.katana");
                facebookIntent.putExtra(Intent.EXTRA_TEXT, message);
                startActivity(Intent.createChooser(facebookIntent,
                        "Share..."));
            }
        });
        return convertView;
    }

2 个答案:

答案 0 :(得分:0)

删除持有人)

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

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.offer_list_item,
                        parent, false);

            } 

            TextView tv1 = (TextView) convertView
                        .findViewById(R.id.offer_name);
            TextView tv2 = (TextView) convertView
                        .findViewById(R.id.expiry_date);
            Button b1 = (Button) convertView.findViewById(R.id.like_button);

            Button b2 = (Button) convertView
                        .findViewById(R.id.share_button);
            TextView tvoid = (TextView) convertView
                        .findViewById(R.id.offer_id);
            TextView  tv = (TextView) convertView.findViewById(R.id.like);

            final OfferItem it = item.get(position);
            tv1.setText(it.getoffer_name());
            final String message = tv1.getText().toString();
            tv2.setText("Offer ends: " + it.getexpiry_date());
            tvoid.setText(it.getoffer_id());
            tv.setText("Like");
            id_offiers_list.add(it.getoffer_id());

            b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String offerid = id_offiers_list.get(position);
                    System.out.println(offerid);
                    if (clicked) {
                        System.out.println(position);
                        b1.setBackgroundResource(R.drawable.like_icon_hover);                         
                        tv.setTextColor(Color.parseColor("#eaa232"));
                        clicked = false;

                    } else {
                        System.out.println(position);
                        b1.setBackgroundResource(R.drawable.like_icon);
                        tv.setTextColor(Color.parseColor("#454545"));
                        clicked = true;
                    }
                }
            });

            b2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent facebookIntent = new Intent(Intent.ACTION_SEND);
                    facebookIntent.setType("text/plain");
                    // facebookIntent.setPackage("com.facebook.katana");
                    facebookIntent.putExtra(Intent.EXTRA_TEXT, message);
                    startActivity(Intent.createChooser(facebookIntent,
                            "Share..."));
                }
            });
            return convertView;
        }

答案 1 :(得分:0)

您应该在OnClickListener中单击布尔值。由于它现在是全球性的,它会影响所有按钮。

holder.b1.setOnClickListener(new View.OnClickListener() {
        private boolean clicked = false;

        @Override
        public void onClick(View v) {
            String offerid = id_offiers_list.get(position);
            System.out.println(offerid);
            if (clicked) {
                System.out.println(position);
                holder.b1
                        .setBackgroundResource(R.drawable.like_icon_hover);
                holder.tv.setTextColor(Color.parseColor("#eaa232"));
                clicked = false;

            } else {
                System.out.println(position);
                holder.b1.setBackgroundResource(R.drawable.like_icon);
                holder.tv.setTextColor(Color.parseColor("#454545"));
                clicked = true;
            }
        }
    });