使用适配器中的onClick侦听器使用单选按钮组填充ListView

时间:2015-02-18 00:45:44

标签: android android-listview android-adapter

我用我的适配器填充我的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_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"
android:background="@drawable/shadow"
android:orientation="vertical">

<TextView
    android:id="@+id/txtName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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_width="match_parent"
    android:layout_height="wrap_content">

    <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>

2 个答案:

答案 0 :(得分:0)

在行布局xml

的RadioGroup中添加以下属性
    android:focusable="false"

答案 1 :(得分:0)

谢谢,我解决了我的问题。

这是我的适配器:

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");
}
}