ravi chandra ravi chandra - 1 month ago 10
Android Question

How to get a checkbox value using custom adapter and listview

I am trying to get a checked value but it is not showing the value, I have implemented custom adapter there i am not getting how to update position

My code is..

public Add_member_adapter(Context a, ArrayList<ItemsModel> d) {

context = a;
data = d;
itemChecked = new boolean[data.size()];

inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}

public final Object getItem(int position) {
return data.get(getCount() - position - 1);
}

public final long getItemId(int position) {
return position;
}

public static class ViewHolder {

public TextView Name, UserId_Fk;
CheckBox chkbox;

}

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

Log.d("getview:", "position=" + position);
vi = convertView;
final ViewHolder holder;

if (convertView == null) {

vi = inflater.inflate(R.layout.add_members_list_item, null);
holder = new ViewHolder();

holder.Name = (TextView) vi.findViewById(R.id.Name);
holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);

vi.setTag(holder);
} else {

holder = (ViewHolder) vi.getTag();

}
if (data.size() <= 0) {

vi.setVisibility(View.GONE);

} else {

tempValues = null;
tempValues = (ItemsModel) data.get(position);

holder.Name.setText(tempValues.getItemName().toString());
holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());
holder.chkbox.setChecked(false);



holder.chkbox.setChecked(itemChecked[position]);

holder.chkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked[position] = isChecked;
}
});
}
return vi;
}


and i am declaring List setOnItemClickListener another in activity

list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

CheckBox checkbox = ((CheckBox) view.findViewById(R.id.chkbox));

String Name = list.getItemAtPosition(position).toString();



}

});

Answer

Any clickable view inside a list view item will steal list view onItemClickListener click.

If you want to catch check event on your check boxes, you should use CompoundButton.OnCheckedChangeListener instead of View.OnClickListener.

Since convertView on ListView is re-useable, you must aware that your data will not accurate. You must make a data set to hold your values that you want to keep.

This code is from your adapter

public View getView(final int position, View convertView, ViewGroup parent) {
        Log.d("getview:", "position=" + position);
        vi = convertView;
        final ViewHolder holder;

        if (convertView == null) {

            vi = inflater.inflate(R.layout.add_members_list_item, null);
            holder = new ViewHolder();

            holder.Name = (TextView) vi.findViewById(R.id.Name);
            holder.UserId_Fk = (TextView) vi.findViewById(R.id.CusId_Fk);
            holder.chkbox = (CheckBox) vi.findViewById(R.id.chkbox);

            vi.setTag(holder);
        } else {

            holder = (ViewHolder) vi.getTag();

        }
        if (data.size() <= 0) {

            vi.setVisibility(View.GONE);

        } else {

            tempValues = null;
            tempValues = (ItemsModel) data.get(position);

            holder.Name.setText(tempValues.getItemName().toString());
            holder.UserId_Fk.setText(tempValues.getUserId_Fk().toString());

            holder.chkbox.setChecked(checkedHolder[position]);

            holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    checkedHolder[position] = isChecked;
                }
            });
        }
        return vi;
    }

    public boolean[] checkedHolder;

    private void createCheckedHolder() {
        checkedHolder = new boolean[getCount()];
    }

Call createCheckedHolder on your adapter's constructor. And if you want to get the checked value, you can get from checkedHolder

From the checkedHolder, you can get the position of any items that selected by the user, then you can call below code from your activity

private ArrayList<String> getSelectedNames(){
List<String> names = new ArrayList<String>();
for (int i = 0; i < adapter.getCount(); i++) {
                if(adapter.checkedHolder[i]){
                    //get all name values that checked by user
                    names.get(i).add(yourDataSet.getItemName().toString());
                }
            }
return names
}
Comments