Android ListView click item to realize check box radio selection and deselection

Keywords: Android

Foreword

ListView with CheckBox implementation radio selection is often used in Android development. The common practice is to use a bean to record the state of the CheckBox, and judge the state of the bean every time you get the view. Let's use the click item to implement.

Design sketch

In the figure, a bottom dialog pops up, which is actually the same as the page
code snippet
JavaBean entity class data
    public class SaleEntity {
                        private String price;
                        private String title;
                        private String time;
                        private boolean checked; // item state

                        public boolean isChecked() {
                            return checked;
                        }

                        public void setChecked(boolean checked) {
                            this.checked = checked;
                        }

                        public SaleEntity() {
                        }

                        public String getPrice() {
                            return price;
                        }

                        public void setPrice(String price) {
                            this.price = price;
                        }

                        public String getTitle() {
                            return title;
                        }

                        public void setTitle(String title) {
                            this.title = title;
                        }

                        public String getTime() {
                            return time;
                        }

                        public void setTime(String time) {
                            this.time = time;
                        }
                        }
SaleAdapter: an important getView code is pasted in the Adapter. In getView, the status is judged according to the position status and javaBean.
public class SaleAdapter extends BaseAdapter {

    private Context context;
    private List<SaleEntity> list;

    public SaleAdapter(Context context, List<SaleEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_sale, null);

            holder.rl = convertView.findViewById(R.id.item_rl);
            holder.rb = convertView.findViewById(R.id.item_rb);
            holder.price = convertView.findViewById(R.id.item_price);
            holder.time = convertView.findViewById(R.id.item_time);
            holder.title = convertView.findViewById(R.id.item_title);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(list.get(position).getTitle());
        holder.time.setText(list.get(position).getTime());
        holder.price.setText(list.get(position).getPrice());
        // Judge the status of the checkbox
        if (list.get(position).isChecked()){
            holder.rb.setImageResource(R.mipmap.check_true); // Selection
        }else{
            holder.rb.setImageResource(R.mipmap.check_no); // Unchecked
        }
        return convertView;
    }
    class ViewHolder{

        RelativeLayout rl;
        ImageView rb;
        TextView price,title,time;
    }

}
SaleActivity: in OnItemClickListener, check box is controlled by isChecked status in SaleEntity.
public class SaleActivity extends ToolbarActivity{

    private List<SaleEntity> list;
    private SaleAdapter adapter;
    private TextView name;
    private TextView price;
    private RelativeLayout rl1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale);

        setCenterTitle("Coupon",true);

        RelativeLayout rl = $(R.id.rl);
        name = $(R.id.yhq_name);
        price = $(R.id.yhq_price);
        rl1 = $(R.id.rl1);

        initData();

        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
    }

    private void initData() {
        list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            SaleEntity saleEntity = new SaleEntity();
            saleEntity.setPrice(i+"element");
            saleEntity.setTitle(i+"Title");
            saleEntity.setTime("Term of validity"+i+"year");
            list.add(saleEntity);
        }
    }

    private void showDialog() {
        View view = View.inflate(this, R.layout.dialog_sale, null);
        final ListView recy = view.findViewById(R.id.recy);
        TextView btmName = view.findViewById(R.id.btm_name);
        RelativeLayout img_rl = view.findViewById(R.id.img_rl);
        final Dialog dialog = new Dialog(this, R.style.dialog);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        //Set pop-up size
        assert window != null;
        window.setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //Set display position
        window.setGravity(Gravity.BOTTOM);
        //Animate
        window.setWindowAnimations(R.style.AnimBottom);
        dialog.show();

        if (list != null && list.size() > 0){
            adapter = new SaleAdapter(this, list);
            recy.setAdapter(adapter);
        }else{
            btmName.setVisibility(View.GONE);
            recy.setVisibility(View.GONE);
            img_rl.setVisibility(View.VISIBLE);
        }

        recy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            int currentNun = -1;
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (SaleEntity saleEntity : list){  // Traversing the data in the list collection
                    saleEntity.setChecked(false);  // Set all to unchecked
                }
                if (currentNun == -1){  // If selected
                    list.get(position).setChecked(true);
                    currentNun = position;
                }else if(currentNun == position){  // If the same item is selected, it will not be selected
                    for (SaleEntity saleEntity : list){
                        saleEntity.setChecked(false);
                    }
                    currentNun = -1;
                }else if(currentNun != position){  // If the current item is not selected for the same item, the previous selected status will be removed
                    for (SaleEntity saleEntity : list){
                        saleEntity.setChecked(false);
                    }

                    list.get(position).setChecked(true);
                    currentNun = position;
                }

                    if (list.get(position).isChecked()){
                        name.setText(list.get(position).getTitle());
                        price.setText("¥"+list.get(position).getPrice());
                        rl1.setVisibility(View.VISIBLE);
                    }else{
                        name.setText("");
                        price.setText("");
                        rl1.setVisibility(View.GONE);
                    }
                Toast.makeText(parent.getContext(),list.get(position).getTitle(), Toast.LENGTH_SHORT).show();
                adapter.notifyDataSetChanged();//Refresh adapter

            }
        });

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                for (SaleEntity saleEntity : list){
                    saleEntity.setChecked(false);
                }
            }
        });

        btmName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                for (SaleEntity saleEntity : list){
                    saleEntity.setChecked(false);
                }
            }
        });
    }

}
Concluding remarks:

If there is anything wrong, please point out. Thank you!!

Posted by ruach on Tue, 31 Dec 2019 22:16:54 -0800