Mayur Raval Mayur Raval - 5 days ago 5
Android Question

Android hide and show checkboxes in custome listview 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);
}

Answer

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.

Comments