我有一个自定义列表视图适配器,其中包含一个textview和一个单选按钮,我用它来填充我的listview。现在一切正常,但我面临的问题是,当我点击任何listview行时,我无法选择radiobutton。我尝试了不同的代码,如
How to set radiobutton unchecked in adapter class
Android ListView with RadioButton/CheckBox in singleChoice mode and a custom row layout conditions on radiobuttons in custom listview 在互联网上,但他们没有帮助我。任何人都可以帮我解决这个问题吗?
这是我的适配器类:
public class Adapterclass extends BaseAdapter
{
private Context mContext;
private List<String> items;
public String selectedUrl = "";
private RadioButton mSelectedRB;
private int selected = -1;
private String title = "";
public Adapterclass(Context c, List<String> items)
{
this.mContext = c;
this.items = items;
}
@Override
public int getCount()
{
return this.items.size();
}
@Override
public Object getItem(int position)
{
return this.items.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public int getViewTypeCount() {
//Count=Size of ArrayList.
return this.items.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(final int position, View child, ViewGroup parent)
{
TextView Title;
final RadioButton btnradio;
boolean[] mStarStates = new boolean[0];
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.list_cell, null);
}
Title = (TextView) child.findViewById(R.id.sound_title);
btnradio = (RadioButton) child.findViewById(R.id.radioSelectButton);
title = this.items.get(position);
Title.setText(title);
btnradio.setTag(title);
btnradio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(position != selected && mSelectedRB != null){
mSelectedRB.setChecked(false);
}
selected = position;
mSelectedRB = (RadioButton)view;
selectedUrl = (String)view.getTag();
//notifyDataSetInvalidated();
}
});
if(selected != position){
btnradio.setChecked(false);
}else{
btnradio.setChecked(true);
if(btnradio != null && btnradio != mSelectedRB){
mSelectedRB = btnradio;
}
}
return child;
}
}
我是否需要将radioGroup用于一个radiobutton。或者解决我想要做的事情。 任何帮助将不胜感激:)