如何在listView中获取选中的检查项目并将值传递给下一个活动?

时间:2018-07-21 09:41:56

标签: android listview checkbox

  1. 问题是当我单击复选框时, CurrentSelectedItems 没有项目。
  2. 在我添加 onItemCheck onItemUnCheck 私有ClickListener onItemCheckListener 私有最终ClickListener onItemClick this.onItemClick = onItemCheckListener 和.... (holder).setOnClickListener(new View.OnClickListener())。它应该允许onclick在recyclerView中的项目,但是在我添加这些项目之后,它会直接断开连接。有什么问题吗?

-在主要活动中

    lodgingAdapter.setOnItemClickListener( new LodgingAdapter.ClickListener() {
        public void onItemClick(int position, View v) {
            Intent intent = new Intent(FavouriteLodging.this, ViewFavouriteDetails.class);
            intent.putExtra("UserID", getIntent().getStringExtra("UserID"));
            intent.putExtra("LodgingID", ll.get(position).getLodgingId());
            startActivity(intent);
        }

       @Override
        public boolean onItemLongClick(int position, View v) {
            return true;

        }

-CurrentSelectedItems没有添加项目。

        @Override
        public void onItemCheck(Lodging item) {
            currentSelectedItems.add(item);
        }

        @Override
        public void onItemUncheck(Lodging item) {
            currentSelectedItems.add(item);

        }
    });

-我的适配器

公共类LodgingAdapter扩展了RecyclerView.Adapter {

private List<Lodging> ll;
private static ClickListener clickListener;
private static int CHECKED_BOX_MAXIMUM = 0;


public interface ClickListener {
    void onItemClick(int position, View v);
    boolean onItemLongClick(int position, View v);
    void onItemCheck(Lodging item);
    void onItemUncheck(Lodging item);
}

@NonNull
private ClickListener onItemCheckListener;
private final ClickListener onItemClick;

public LodgingAdapter(List<Lodging> LodgingList) {
    ll = LodgingList;
    this.onItemClick = onItemCheckListener;
}


public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener,CompoundButton.OnCheckedChangeListener{

    public TextView title;
    public TextView price;
    public TextView type;
    public ImageView picture;
    public CheckBox cbBox;

    public ViewHolder(View v) {
        super(v);
        v.setOnClickListener(this);
        v.setOnLongClickListener(this);
        title = v.findViewById(R.id.titleTv);
        price = v.findViewById(R.id.priceTv);
        type = v.findViewById(R.id.lodgingTypeTv);
        picture = v.findViewById(R.id.lodgingPicIv);
        cbBox = v.findViewById(R.id.cbCompare);
        cbBox.setOnCheckedChangeListener(this);
    }

   public void setOnClickListener(View.OnClickListener onClickListener) {
        itemView.setOnClickListener(onClickListener);
    }

    public boolean onLongClick(View view) {
        clickListener.onItemLongClick(getAdapterPosition(), view);
        return true;
    }

    @Override
    public void onClick(View view) {
        clickListener.onItemClick(getAdapterPosition(), view);
    }

如果单击该复选框,则为CHECKED_BOX_MAXIMUM ++,否则为CHECKED_BOX_MAXIMUM--;如果单击了两个复选框,则其他复选框不可单击。

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()){
            case R.id.cbCompare :
                if(isChecked == true){
                    CHECKED_BOX_MAXIMUM++;
                }else{
                    CHECKED_BOX_MAXIMUM--;
                }if (isChecked && CHECKED_BOX_MAXIMUM > 2) {
                     cbBox.setChecked(false);
                     CHECKED_BOX_MAXIMUM--;
            }
        }
    }


}



public void setOnItemClickListener(ClickListener clickListener) {
    LodgingAdapter.clickListener = clickListener;
}

@Override
public void onBindViewHolder(final LodgingAdapter.ViewHolder holder, int position) {

        final Lodging l = ll.get(position);
        holder.title.setText(l.getTitle());
        holder.price.setText("RM" + String.format("%.2f", l.getPrice()));
        holder.type.setText(l.getLodgingType());
      (holder).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                (holder).cbBox.setChecked(!(holder).cbBox.isChecked());
                if ((holder).cbBox.isChecked()) {
                    onItemClick.onItemCheck(l);
                } else {
                    onItemClick.onItemUncheck(l);
                }
            }
        });

        Glide.with(holder.itemView.getContext())
                .load(l.getImage())
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .centerCrop()
                .into(holder.picture);


}

}

This is My First UI, CLICK ME

After clicking the checkbox, it suppose to pass here one will show on the top and the other will show the bottom. CLICK ME

1 个答案:

答案 0 :(得分:0)

在适配器类中,创建一个返回所选项目列表的函数。

然后在活动中单击菜单项时调用该函数,在此比较返回列表的大小> = 2,如果真,则如函数>>> list_ = [54, 26, 93, 17, 77, 31, 44, 55, 20] >>> passnum = int(input("Enter the number of passes\n")) Enter the number of passes 2 >>> bubblesort(passnum, list_) >>> list_ [26, 17, 54, 31, 44, 55, 20, 77, 93]