Mayur Raval
Mayur Raval

Reputation: 3275

Android hide and show checkboxes in custom list view on button click

i use the adapter which is given below. i explain in the figures. figures is just for referances. i use textview and checkbox in for single line. textviews should appear and checkboxes hide initially.after on button press checkbox should appear and textview remain same.please help me.thanks in advance..

enter image description here enter image description here

 public class Adapter extends ArrayAdapter<SectionsModel>{

    private ArrayList<SectionsModel> list;
    public Adapter(Context context, int resource, ArrayList<SectionsModel> objects) {
        super(context, resource, objects);
        this.list = new ArrayList<SectionsModel>();
        this.list.addAll(objects);
    }

    ViewHolder v = new ViewHolder();
    public void setcheckbox() {

        Log.d("viewholser" + v, "checkbox" + v.selected);

        v.selected.setVisibility(View.VISIBLE);

    }

    public class ViewHolder {
        public TextView setting;
        public CheckBox selected;
    }

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


        ViewHolder holder = null;
        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.menu_frame_child, null);
            holder = new ViewHolder();

            holder.setting = (TextView) convertView.findViewById(R.id.child_setting_header);
            holder.selected = (CheckBox) convertView.findViewById(R.id.settings_check);

        //  holder.selected.setVisibility(View.INVISIBLE);

            convertView.setTag(holder);

            //holder.selected.setVisibility(View.INVISIBLE);

            holder.selected.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    SectionsModel _state = (SectionsModel) cb.getTag();
                    _state.setSelected(cb.isChecked());
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        SectionsModel section = list.get(position);

        holder.setting.setText(section.getSection());
        holder.selected.setChecked(section.isSelected());

        holder.selected.setTag(section);

        return convertView;
    }

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

    @Override
    public int getPosition(SectionsModel item) {

        return super.getPosition(item);
    }


}

UPDATE: In getview i just add following code AppConstants.ischeckboxvisible is variable where Appconstants is class and ischeckboxvisible is public static variable on that class. initially this variable is false

            if (!AppConstants.ischeckboxvisible)
            {
       holder.selected.setVisibility(View.INVISIBLE);
    }
        if (AppConstants.ischeckboxvisible)
            {
           holder.selected.setVisibility(View.VISIBLE);
        }

Upvotes: 2

Views: 7932

Answers (2)

Levan Voronin
Levan Voronin

Reputation: 107

just in case maybe someone still need it.

I solved the problem next way:

In getView - method in ArrayAdapter I set:

CheckBox checkBox = (CheckBox)listItem.findViewById(R.id.checkBox);
    checkBox.setVisibility(View.GONE);

Then inside setOnItemLongClickListener:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {

            for(int i = 0; i != arrayList.size(); i++) {

        mCheckBox = (CheckBox) playlist.getChildAt(i).findViewById(R.id.checkBox);
        mCheckBox.setVisibility(View.VISIBLE);
    }

            return true;
        }
    });

After you perform long click on any item all check boxes will appear.

Upvotes: 3

yahya
yahya

Reputation: 4860

Add another attribute to your SectionModel as inEditMode, and set it to true at all items in your array when user clicks edit, and call notifyDataSetChanged on your adapter.

In Adapter check that flag on every item and make them visible or invisible.

Upvotes: 1

Related Questions