【问题标题】:How to fetch Selected value from the RadioGroup in the Customized Listview如何从自定义列表视图中的 RadioGroup 中获取选定值
【发布时间】:2013-04-11 09:46:14
【问题描述】:

在我的应用程序中,我正在开发一个应用程序学生排名系统。

Here,是我之前问过的链接。现在我的要求已经满足了,但我无法访问其中的数据..

public class ListMobileActivity extends ListActivity {
static final String[] MOBILE_OS = new String[] { "001 AAAA", "002 BBBB",
        "003 CCCC", "004 DDDD"};
static final String[] MOBILE_OS1 = new String[] { "AAAA", "BBBB",
    "CCCC", "DDDD"};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //setListAdapter(new ArrayAdapter<String>(this, R.layout.list_mobile,
    //      R.id.label, MOBILE_OS));

    setListAdapter(new MobileArrayAdapter(this, MOBILE_OS));
}

}

适配器是,..

public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_mobile, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label1);
    TextView textView1 = (TextView) rowView.findViewById(R.id.label);
    RadioGroup radioSexGroup=(RadioGroup) rowView.findViewById(R.id.radioSex);
    int selectedId = radioSexGroup.getCheckedRadioButtonId();
    RadioButton radioSexButton= (RadioButton) rowView.findViewById(selectedId);;
    textView.setText(values[position]);
    textView1.setText(String.valueOf(position+1));
    // Change icon based on name
    String s = values[position];
    System.out.println(s);
    return rowView;
}
}

我可以对这个单选按钮执行任何操作..

现在我的输出屏幕如下...

但我无法获得特定学生在每一行上选择了哪个单选按钮,请帮助我实现这一点。我需要使用...更改适配器吗?

【问题讨论】:

    标签: android listview listadapter baseadapter


    【解决方案1】:

    使用数组列表保存选择

    List<Integer> sexGrp=new ArrayList<Integer>();
    

    在适配器中使用这个监听器

    radioSexGroup.setTag(position);
                    radioSexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
                    {
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            sexGrp.add(Integer.parseInt(group.getTag().toString()), checkedId);
    
                        }
                    });
    

    【讨论】:

    • 我在哪里可以实现这个代码..!
    • 在适配器的 getView() 中。不要忘记从checkedId获取RadioButton的元素位置。
    【解决方案2】:

    在你的getView方法中写这个循环,这里arrayChekedCheckBox是你所有检查的checkbox的集合

            for (int i = 0; i < arrayListOfStudent.size(); i++) {
                if (i==postion){
                    arrayChekedCheckBox.add(postion) ;
                    break;
                }
            }
    

    【讨论】:

    • 数据是动态数据,我在运行时获取数组中的数据。我已经为我的应用程序展示了一个示例输入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2017-03-03
    相关资源
    最近更新 更多