user2322215
user2322215

Reputation:

Show/Hide checkboxes in Custom Adapter

I want to show/hide checkboxes in my custom adapter but i couldn't find any solution for 3 days of search.

In my custom adapter all checkboxes are hidden as default when user click show button then make them visible also click again invisible them. Here is my code.

Thanks for any help

Main Activity

public class PhonebookList extends ListActivity{
private ArrayList<PhoneListView>    lists;
private ArrayList<String>           phoneName   = new ArrayList<String>();
private PhoneListAdapter            adapter;
private ListView                    lv;

private RelativeLayout              rl;
private ImageView                   im1;
private ImageView                   im2;

private boolean                     checkUncheck = true;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_phonelist); // Get main display layout

    lv = (ListView) findViewById(android.R.id.list);

    String uniqueName = "";
    int index = 0;
    lists = new ArrayList<PhoneListView>();

    Cursor phones = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext())
    {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        if(!name.equals(uniqueName))
        {
            uniqueName = name;
            phoneName.add(name);
            lists.add(new PhoneListView(name, index, checkUncheck));

            index++;
        }
        else
        {
            continue;
        }
    }

    int x = lists.size();
    PhoneListView[] toArray = new PhoneListView[x];
    toArray = lists.toArray(toArray);

    adapter = new PhoneListAdapter(this,
            R.layout.custom_phone_listview_rows, toArray); // Layout whose hold rows
    lv.setAdapter(adapter);
    lv.setClickable(true);

    lv.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id)
        {
            Intent i = new Intent(getApplicationContext(),
                    PhoneBookDetail.class);
            i.putExtra("id", phoneName.get(position));
            startActivity(i);
        }
    });
}

@Override
protected void onResume()
{
    final ImageView showButton = (ImageView) findViewById(R.id.show_hide_button);
    showButton.setBackgroundResource(R.drawable.icon_choise);

    showButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if(checkUncheck)
            {
                showButton.setBackgroundResource(R.drawable.icon_choise);
                checkUncheck = false;

                rl.setVisibility(View.GONE);
                im1.setVisibility(View.GONE);
                im2.setVisibility(View.GONE);
            }
            else
            {
                showButton.setBackgroundResource(R.drawable.icon_completion);
                checkUncheck = true;

                rl.setVisibility(View.VISIBLE);
                im1.setVisibility(View.VISIBLE);
                im2.setVisibility(View.VISIBLE);
            }
        }
    });

    rl  = (RelativeLayout) findViewById(R.id.show_hide_bar);
    im1 = (ImageView) findViewById(R.id.show_hide_backup);
    im2 = (ImageView) findViewById(R.id.show_hide_checkall);

    rl.setVisibility(View.GONE);
    im1.setVisibility(View.GONE);
    im2.setVisibility(View.GONE);
    super.onResume();
}

public void backupForChecked(View v){
    if(adapter.checkedItem.size() == 0)
        Toast.makeText(getApplicationContext(), "Select atleast one item", Toast.LENGTH_SHORT).show();
    else
    {
        //  Send selected values as Array
        int[] myArray = new int[adapter.checkedItem.size()];

        Bundle b=new Bundle();
        for(int i = 0; i < adapter.checkedItem.size(); i++)
        {
            myArray[i] = Integer.parseInt(adapter.checkedItem.get(i));
        }
        b.putIntArray("contactID", myArray);

        Intent i = new Intent(getApplicationContext(), SaveAllWithoutAsk.class);
        i.putExtra("contactID", myArray);
        startActivity(i);
    }
}

public void checkUncheckAll(View v)
{

}

public void modoru(View v)
{
    finish();
}
  }

Custom ListView

package custom_list;

  public class PhoneListView{
private String  name;
private int     id;
private boolean itemToggled = false;

public PhoneListView()
{
    super();
}

public PhoneListView(String name, int id, boolean itemToggled)
{
    this.setName(name);
    this.setId(id);
    this.setItemToggled(itemToggled);
}

public void setName(String name)
{
    this.name = name;
}

public void setId(int id)
{
    this.id = id;
}

public String getName()
{
    return name;
}

public int getId()
{
    return id;
}

public boolean getItemToggled()
{
    return itemToggled;
}

public void setItemToggled(boolean itemToggled)
{
    this.itemToggled = itemToggled;
}
  }

And my Custom Adapter

public class PhoneListAdapter extends ArrayAdapter<PhoneListView>{
private Context             context;
private int                 resID;
private PhoneListView[]     data        = null;
public ArrayList<String>    checkedItem = new ArrayList<String>();

public PhoneListAdapter(Context context, int resource,
        PhoneListView[] objects)
{
    super(context, resource, objects);
    this.context = context;
    this.resID = resource;
    this.data = objects;
}

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

    if (row == null)
    {
        LayoutInflater inflate = ((Activity) context).getLayoutInflater();
        row = inflate.inflate(resID, null);

        holder = new CustomHolder();
        holder.cb = (CheckBox) row.findViewById(R.id.phonelist_checkbox);
        holder.tw = (TextView) row
                .findViewById(R.id.phonelist_contact_name);

        row.setTag(holder);
    }
    else
        holder = (CustomHolder) row.getTag();

    final PhoneListView plv = data[position];
    holder.tw.setText(plv.getName().toString());
    holder.id = plv.getId();

    holder.checked = plv.getItemToggled();

    if (holder.checked == false)
        holder.cb.setVisibility(View.GONE);
    else
    {
        holder.cb.setVisibility(View.VISIBLE);
        holder.cb.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (((CompoundButton) v).isChecked())
                {
                    checkedItem.add("" + plv.getId());
                }
                else
                {
                    checkedItem.remove("" + plv.getId());
                }
            }
        });
    }

    return row;
}

public class CustomHolder
{
    CheckBox    cb;
    TextView    tw;
    int         id;
    boolean     checked;
}
  }

Screen shots are here

enter image description here

enter image description here

Upvotes: 0

Views: 1467

Answers (4)

Prasad Pathak
Prasad Pathak

Reputation: 43

I think you could create a loop for the number of check boxes and then traverse through the loop i.e.

1) you could have one method called checkall() where all the elements would be looped through and checked for their status and if checked would remain checked and if unchecked would be checked.

2) another method uncheckall() to be looped and all checkboxes unchecked.

3) Also see if you can create a checkbox group for the check boxes u have.

best regards

The code below is a modified version of some code from android sample code see if it helps.

public void onCheckboxClicked(View view) {

// Here you can create an array of the check boxes available in the view .

// create a loop which will check which one of the check boxes is checked and which one is not 

for ( ... .... ... )

{ boolean checked = ((CheckBox) view).isChecked();

// Check which checkbox was clicked
switch(view.getId()) {
    case R.id.checkbox_meat:
        if (checked)
            // Put some meat on the sandwich
        else
            // Remove the meat
        break;
    case R.id.checkbox_cheese:
        if (checked)
            // Cheese me
        else
            // I'm lactose intolerant
        break;
    // TODO: Veggie sandwich

   }
}

}

Upvotes: 0

user2322215
user2322215

Reputation:

Thanks for reply. I have solved my problem #1 i am going to write solution here who may help someone's hours and hairs.

I have created new public method and call it when show/hide button clicked.

Now i am still researching how to handle check/uncheck all and i still need idea and help

Upvotes: 0

Prasad Pathak
Prasad Pathak

Reputation: 43

Can you put your layout xml file that ur using to display ? The widgets have properties assigned to them show / hide them as required. Check your checkbox widget for such propetry.

regards Prasad

Upvotes: -1

TmKVU
TmKVU

Reputation: 3000

You should notify the adapter that the dataset is changed by calling the notifyDatasetChanged() function in the OnClickListener of your button.

You should also remove the check if row is null. Usually it is good practice to reuse views, but since your Views change and you need to update after you changed the settings you need to recreate the items.

Upvotes: 0

Related Questions