Soham Soham - 4 months ago 32
Android Question

Set Radiobutton true onItemclick on listview

I want to mark

radio button
true in
listview
onItemClick
so what I am doing is

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
LinearLayout item_view = (LinearLayout) view;
final RadioButton itemcheck = (RadioButton) item_view
.findViewById(R.id.listview_radiobutton);

if (itemcheck.isChecked()) {
itemcheck.setChecked(true);
} else {
itemcheck.setChecked(false);
}

itemcheck.setChecked(true);

}


My
listview


<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/view_margin_15"
android:layout_marginRight="@dimen/view_margin_15"
android:layout_marginTop="@dimen/view_margin_20"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:divider="@drawable/list_divider"
android:dividerHeight="@dimen/padding_2"
android:fastScrollEnabled="true"
android:footerDividersEnabled="true"
android:listSelector="@null"
android:scrollbars="none"
android:textFilterEnabled="true"
android:textStyle="normal" />


Edit:--

My Adapter code :--

public class Adapter extends ArrayAdapter<Details> {

private Context mContext;
private List<Details> transList;
private LayoutInflater infalInflater;
private OnCheckedRadioButon onCheckedRadioButon;
private Typeface mTypeface, mEditTypeface, mPasswdTypeface;
private int mSelectedPosition = -1;
private RadioButton mSelectedRB;
private PromoViewHolder viewHolder;
public Adapter(Context context, List<Details> mtransList, OnCheckedRadioButon onCheckedRadioButon) {
super(context, R.layout.dialog_listview, mtransList);
this.mContext = context;
this.transList = mtransList;
this.onCheckedRadioButon = onCheckedRadioButon;
this.infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

View row = convertView;

try {
viewHolder = null;

row = infalInflater.inflate(R.layout.dialog_listview_code, parent, false);

viewHolder = new ViewHolder();

viewHolder.radiobutton = (RadioButton) row.findViewById(R.id.radiobutton);
viewHolder.listview_name = (TextView) row.findViewById(R.id.listview_name);

setValueText(viewHolder, position);
viewHolder.radiobutton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
onCheckedRadioButon.onCheckedButton(transList.get(position));
}
}
});

viewHolder.radiobutton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
}

mSelectedPosition = position;
mSelectedRB = (RadioButton) v;
}
});


if (mSelectedPosition != position) {
viewHolder.radiobutton.setChecked(false);
} else {
viewHolder.radiobutton.setChecked(true);
if (mSelectedRB != null && viewHolder.radiobutton != mSelectedRB) {
mSelectedRB = viewHolder.radiobutton;
}
}


row.setTag(viewHolder);

} catch (Exception e) {
e.printStackTrace();
}

return row;

}

private void setValueText(ViewHolder viewHolder, final int position) {
viewHolder.listview_name.setText(transList.get(position).getName());
}

public interface OnCheckedRadioButon {
void onCheckedButton(Details pr);
}

class ViewHolder {
RadioButton radiobutton;
TextView listview_name;
}

}


It is working but if I click on another position of the
listview
then the previous
radiobutton
position is not unchecked.I want to uncheck all the previous ones and mark only one at a time.
Any help will be appreciated.

Answer

Use POJO classes (Setter or Getter) to manage this type of condition. Use boolean variable in that class and change its values according to the position true or false.

POJO Class Like :

public class CheckListSource {       
    public boolean isSelected;

    public boolean isSelected() {
            return isSelected;
    }

    public void setSelected(boolean selected) {
            isSelected = selected;
    }        

}

In your adapter :

private ArrayList<CheckListSource > itemsData;

public ChildListAdapter(Activity activity, ArrayList<ChildListResponse> baseResponse) {
    this.itemsData = baseResponse;
    this.activity = activity;
}

In BindViewHolder Like :

viewHolder.checkParentView.setTag(itemsData.get(position));
viewHolder.checkParentView.setOnClickListener(checkedListener);

    private View.OnClickListener checkedListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        CheckListSource childListResponse = (CheckListSource ) v.getTag();
        if (childListResponse.isSelected())
            childListResponse.setSelected(false);
        else
            childListResponse.setSelected(true);
        notifyDataSetChanged();
    }
};
Comments