lundi 1 mai 2017

how to check the checkbox when row was clicked in ListView

I have created a custom listView with my adapter. Each row contains Checkbox, TextView, TextView. Checking option is eneble onLong press (it is connected with CAB) I want to set the checkbox check when user click on the row. It simply works but when I return from cab i could enter there one more time. Here is my adapter code

/**
 * Created by Michał on 26.04.2017.
 */

public class RowAdapter extends ArrayAdapter<ProductList>{
    private Context context;
    private int resource;
    private ArrayList<ProductList> data = null;
    private boolean longPress=false;
    RowBeanHolder holder = null;


    public RowAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<ProductList> data) {
        super(context, resource, data);
        this.context=context;
        this.resource=resource;
        this.data=(ArrayList<ProductList>)data;
    }

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



        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(resource, parent, false);

            holder = new RowBeanHolder();
            holder.txtTitle=(TextView) row.findViewById(R.id.name);
            holder.indicator=(TextView) row.findViewById(R.id.indicator);
            holder.checkBox=(CheckBox) row.findViewById(R.id.checkBoxDelete);
            row.setTag(holder);
        }
        else
        {
            holder = (RowBeanHolder)row.getTag();
            row.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(holder.checkBox.isChecked()){
                        holder.checkBox.setChecked(false);
                    }
                    else{
                        holder.checkBox.setChecked(true);
                    }

                }
            });
        }

        ProductList object = data.get(position);
        holder.txtTitle.setText(object.getName());
        holder.indicator.setText(object.getToBuy() + "/" + object.getBought());

        if(longPress){
            holder.checkBox.setVisibility(View.VISIBLE);
        }
        else{
            holder.checkBox.setVisibility(View.GONE);
            holder.checkBox.setChecked(false);
        }

        return row;
    }

    static class RowBeanHolder
    {
        TextView txtTitle;
        TextView indicator;
        CheckBox checkBox;

    }

    public void showCheckBox() {
        longPress=true;
        notifyDataSetChanged();
    }

    public void hideCheckBox(){
        longPress=false;

        notifyDataSetChanged();
    }
}




Aucun commentaire:

Enregistrer un commentaire