如何从自定义列表视图单元格中获取单选按钮值

时间:2014-10-16 14:43:06

标签: java android

我正在尝试从自定义列表视图单元格中获取数据,我有单选按钮,我正在尝试从中获取数据我该怎么做

我的自定义列表代码:

private class customList extends ArrayAdapter<String> {
public customList(Context context, int resource, List<String> objects) {
    super(context, resource, objects);

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = layoutInflater.inflate(R.layout.custom_list , parent , false);
    //ParseObject parseObject = new ParseObject("Questions");
    //getParseData = new GetParseData(parseObject ,strQuestion);
    // getParseData.getQuestion(position);

    TextView tv = (TextView)row.findViewById(R.id.tvNameC);
    tv.setText(nameList.get(position).toString());

    final RadioGroup rg = (RadioGroup)row.findViewById(R.id.rg);


    ParseQuery<ParseObject> query = ParseQuery.getQuery("test");
    query.whereExists("arr");
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject>List, ParseException e) {
            if (e == null) {

                strList = List.get(position).getList("arr");

                Log.e("Array Is " , strList.toString());

                final RadioButton[] radioButtons = new RadioButton[strList.size()];
                for(int i = 0 ; i < radioButtons.length ; i++)
                {
                    radioButtons[i] = new RadioButton(getApplicationContext());
                    radioButtons[i].setText(strList.get(i).toString());
                    radioButtons[i].setTextColor(Color.BLACK);
                    rg.addView(radioButtons[i]);
                }

                idForMethod = rg.getCheckedRadioButtonId();
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });


    return row ;

}

3 个答案:

答案 0 :(得分:0)

您可以在适配器中创建一个返回数据的方法。例如,您可以使用所选状态单选按钮填充列表。

public List<boolean> getSelectedBoxes() {
    return selectedIds;
}

然后,我假设您使用变量adapter声明了此CustomAdapter,您可以在活动中检索此项,如下所示;

adapter.getSelectedBoxes();

答案 1 :(得分:0)

你可以从适配器获取数据:

 myListView =(ListView)findViewById(R.id.list);

        //name of the class adapter with params
        adapter=new LazyAdapter(this, mStrings,nombre);
        myListView.setAdapter(adapter);
       //event of click in listview
        myListView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {

                Toast.makeText(getApplicationContext(), adapter.data[arg2], Toast.LENGTH_SHORT).show();

            }
        });

就我而言,当我点击列表中的一个元素时,我会看到class.attribute [id / positon]

希望有所帮助

答案 2 :(得分:0)

 for (int i = 0; i < Listview.getAdapter().getCount(); i++) {

 View view = Listview.getAdapter().getView(i,Listview.getChildAt(i), Listview);

// View view = Listview.getChildAt(i);

     TextView ref = (TextView)view.findViewById(R.id.tv_1);

     EditText text = (EditText) view.findViewWithTag ("et"+i); 

             hmref.put("reference", ref.getText().toString());
             Log.d("reference " + i, ref.getText()+"");
             hmref.put("amount", text.getText().toString());
             Log.d("amounts " + i, text.getText().toString());
             sendlist.add(hmref);
             }