Reputation: 133
I hope that some of you can help me with this:
I got a custom listview with 2 textviews and 1 radio button.
I want to make it as a single choice, but everytime I click on a item on the listview, it doesn't remove the "check" from the other radio button.
My xml code:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvMaterialName"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:padding="5dp"
android:text="MaterialName"
android:textSize="35px" />
<TextView
android:id="@+id/tvMaterialNo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:text="MaterialNo"
android:textSize="1px"
android:visibility="invisible" />
</LinearLayout>
<RadioButton
android:id="@+id/rdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:checked="false" />
</LinearLayout
And my onItemClickListener:
searchList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
LinearLayout item_view = (LinearLayout) v;
RadioButton itemcheck = (RadioButton) item_view
.findViewById(R.id.rdBtn);
itemcheck.setChecked(true);
if (itemcheck.isChecked()) {
itemcheck.setChecked(true);
} else {
itemcheck.setChecked(false);
}
}
});
My getView() in my adapter
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
final Materials materialItem = getItem(position);
parent.setClickable(true);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.d, null, false);
viewHolder = new ViewHolder();
viewHolder.tvMaterialName = (TextView) convertView
.findViewById(R.id.tvMaterialName);
viewHolder.tvMaterialNo = (TextView) convertView
.findViewById(R.id.tvMaterialNo);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvMaterialName.setText(materialItem.getMaterialName());
viewHolder.tvMaterialNo.setText(materialItem.getMaterialNo());
return convertView;
Thanks for the help
Upvotes: 3
Views: 7215
Reputation: 1810
Try like this :
public class CheckedLinearLayout extends RelativeLayout implements
Checkable {
private boolean isChecked;
private List<Checkable> checkableViews;
public CheckedLinearLayout (Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
initialise(attrs);
}
public CheckedLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
initialise(attrs);
}
public CheckedLinearLayout (Context context, int checkableId) {
super(context);
initialise(null);
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
for (Checkable c : checkableViews) {
c.setChecked(isChecked);
}
}
public void toggle() {
this.isChecked = !this.isChecked;
for (Checkable c : checkableViews) {
c.toggle();
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final int childCount = this.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(this.getChildAt(i));
}
}
/**
* Read the custom XML attributes
*/
private void initialise(AttributeSet attrs) {
this.isChecked = false;
this.checkableViews = new ArrayList<Checkable>(5);
}
/**
* Add to our checkable list all the children of the view that implement the
* interface Checkable
*/
private void findCheckableChildren(View v) {
if (v instanceof Checkable) {
this.checkableViews.add((Checkable) v);
}
if (v instanceof ViewGroup) {
final ViewGroup vg = (ViewGroup) v;
final int childCount = vg.getChildCount();
for (int i = 0; i < childCount; ++i) {
findCheckableChildren(vg.getChildAt(i));
}
}
}
}
Upvotes: 5