使用适配器中的 onClick 侦听器填充单选按钮组的 ListView
Posted
技术标签:
【中文标题】使用适配器中的 onClick 侦听器填充单选按钮组的 ListView【英文标题】:Fill a ListView with radio button groups with an onClick listener in the Adapter 【发布时间】:2015-04-18 21:16:48 【问题描述】:我用我的适配器填充我的 ListView,但我的最后一组单选按钮无法正常工作。
当我选择前三个元素的选项时,效果很好。最后一个单选按钮在按下时不起作用。
这是我的适配器
public class ScoreAdapter extends BaseAdapter
private Activity activity;
private LayoutInflater inflater;
private List<Score> items;
private Resources resources;
public ScoreAdapter(Activity activity, List<Score> items)
this.activity = activity;
this.items = items;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resources = activity.getResources();
@Override
public int getCount()
return items.size();
@Override
public Object getItem(int location)
return items.get(location);
@Override
public long getItemId(int position)
return position;
public List<Score> getItems()
return items;
public Score getScore(int position)
return ((Score) getItem(position));
private static class ViewHolder
TextView name;
RadioButton radioTop, radioMiddle, radioLow;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
ViewHolder holder;
if (convertView == null)
convertView = inflater.inflate(R.layout.item_survey, null, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.txtName);
holder.radioTop = (RadioButton) convertView.findViewById(R.id.radioTop);
holder.radioMiddle = (RadioButton) convertView.findViewById(R.id.radioMiddle);
holder.radioLow = (RadioButton) convertView.findViewById(R.id.radioLow);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
Score item = items.get(position);
holder.name.setText(item.getQuestionName());
holder.radioTop.setOnClickListener(listener);
holder.radioTop.setText(item.getTopDescription());
holder.radioTop.setChecked(item.isTopScoreSelected());
holder.radioTop.setTag(position);
holder.radioMiddle.setOnClickListener(listener);
holder.radioMiddle.setText(item.getMiddleDescription());
holder.radioMiddle.setChecked(item.isMiddleScoreSelected());
holder.radioMiddle.setTag(position);
holder.radioLow.setOnClickListener(listener);
holder.radioLow.setText(item.getLowDescription());
holder.radioLow.setChecked(item.isLowScoreSelected());
holder.radioLow.setTag(position);
return convertView;
RadioButton.OnClickListener listener = new RadioButton.OnClickListener()
@Override
public void onClick(View view)
RadioGroup group = (RadioGroup) view.getParent();
RadioButton radio = (RadioButton) view;
Score selected = getScore((Integer) radio.getTag());
if (selected.getQuestionNumber() == 11 && (radio.getId() == R.id.radioTop || radio.getId() == R.id.radioLow))
DialogFragment modal = new MomentOfTruthDialog(selected);
modal.show(activity.getFragmentManager(), "motModal");
selected.setTopScoreSelected(false);
selected.setMiddleScoreSelected(false);
selected.setLowScoreSelected(false);
switch (radio.getId())
case R.id.radioTop:
radio.setChecked(true);
selected.setTopScoreSelected(true);
selected.setScoreSelected(resources.getInteger(R.integer.TOP_SCORE_VALUE));
break;
case R.id.radioMiddle:
radio.setChecked(true);
selected.setMiddleScoreSelected(true);
selected.setScoreSelected(resources.getInteger(R.integer.MIDDLE_SCORE_VALUE));
break;
case R.id.radioLow:
radio.setChecked(true);
selected.setLowScoreSelected(true);
selected.setScoreSelected(resources.getInteger(R.integer.LOW_SCORE_VALUE));
break;
;
这是我的模板项目
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:layout_marginBottom="6dip"
android:background="@drawable/shadow"
android:orientation="vertical">
<TextView
android:id="@+id/txtName"
android:layout_
android:layout_
android:background="@drawable/back_for_header"
android:gravity="center"
android:padding="6dip"
android:textColor="#fff"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/radioGroupSurvey"
android:layout_
android:layout_>
<TextView
style="@style/Survey_Label_For_TextView"
android:text="@string/top_label" />
<RadioButton
android:id="@+id/radioTop"
style="@style/Survey_RadioButton"/>
<TextView
style="@style/Survey_Label_For_TextView"
android:text="@string/middle_label" />
<RadioButton
android:id="@+id/radioMiddle"
style="@style/Survey_RadioButton" />
<TextView
style="@style/Survey_Label_For_TextView"
android:text="@string/low_label" />
<RadioButton
android:id="@+id/radioLow"
style="@style/Survey_RadioButton"/>
</RadioGroup>
</LinearLayout>
【问题讨论】:
我的项目模板太大,会影响吗? 如果你在debug模式下运行,在onClick的开头下一个断点,然后按似乎不起作用的单选按钮,它是否到达断点? 我找不到getScore
函数或来自`Score selected = getScore((Integer) radio.getTag());`的连接逻辑;
感谢您抽出时间 Curly 和 Droidekas
感谢您抽出时间 Curly 和 Droidekas。卷曲,是的,但没有标记为检查为真,实际上当我得到这个单选值时真的检查为真
【参考方案1】:
在您的 RadioGroup 的行布局 xml 中添加以下属性
android:focusable="false"
【讨论】:
【参考方案2】:谢谢,我的问题解决了。
这是我现在的适配器:
public class ScoreAdapter extends BaseAdapter
private Activity activity;
private LayoutInflater inflater;
private List<Score> items;
private Resources resources;
public ScoreAdapter(Activity activity, List<Score> items)
this.activity = activity;
this.items = items;
this.inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resources = activity.getResources();
@Override
public int getCount()
return items.size();
@Override
public Object getItem(int location)
return items.get(location);
@Override
public long getItemId(int position)
return position;
public List<Score> getItems()
return items;
private static class ViewHolder
TextView name;
RadioGroup radioGroup;
RadioButton radioTop, radioMiddle, radioLow;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
ViewHolder holder;
if (convertView == null)
convertView = inflater.inflate(R.layout.item_survey, null, false);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.txtName);
holder.radioGroup = (RadioGroup) convertView.findViewById(R.id.radioGroupSurvey);
holder.radioTop = (RadioButton) convertView.findViewById(R.id.radioTop);
holder.radioMiddle = (RadioButton) convertView.findViewById(R.id.radioMiddle);
holder.radioLow = (RadioButton) convertView.findViewById(R.id.radioLow);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
Score item = (Score) getItem(position);
holder.name.setText(item.getQuestionName());
holder.radioGroup.setOnCheckedChangeListener(null);
holder.radioTop.setText(item.getTopDescription());
holder.radioTop.setChecked(item.isTopScoreSelected());
holder.radioMiddle.setText(item.getMiddleDescription());
holder.radioMiddle.setChecked(item.isMiddleScoreSelected());
holder.radioLow.setText(item.getLowDescription());
holder.radioLow.setChecked(item.isLowScoreSelected());
holder.radioGroup.setTag(position);
holder.radioGroup.setOnCheckedChangeListener(listener);
return convertView;
RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup radioGroup, int id)
RadioButton radio = (RadioButton)radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
Score selected = (Score) getItem((Integer) radioGroup.getTag());
boolean showMOT = false;
selected.setTopScoreSelected(false);
selected.setMiddleScoreSelected(false);
selected.setLowScoreSelected(false);
radio.setChecked(true);
radio.setSelected(true);
switch (id)
case R.id.radioTop:
showMOT = true;
selected.setTopScoreSelected(true);
selected.setScoreSelected(resources.getInteger(R.integer.TOP_SCORE_VALUE));
break;
case R.id.radioMiddle:
selected.setMiddleScoreSelected(true);
selected.setScoreSelected(resources.getInteger(R.integer.MIDDLE_SCORE_VALUE));
break;
case R.id.radioLow:
showMOT = true;
selected.setLowScoreSelected(true);
selected.setScoreSelected(resources.getInteger(R.integer.LOW_SCORE_VALUE));
break;
if (selected.getQuestionNumber() == 11 && showMOT)
showDialogMOT(selected);
;
private void showDialogMOT(Score score)
DialogFragment modal = new MomentOfTruthDialog(score);
modal.show(activity.getFragmentManager(), "motModal");
【讨论】:
以上是关于使用适配器中的 onClick 侦听器填充单选按钮组的 ListView的主要内容,如果未能解决你的问题,请参考以下文章
jQuery使用数据目标更改带有onClick侦听器的一组单选按钮标签的innerHtml
片段内 Listview 中动态视图的 onClick 侦听器
ViewHolder 中的 onClick 无权访问适配器中的单击项目