如何将单选按钮选择到ArrayList中?

时间:2017-10-08 17:18:58

标签: java android arraylist

我有一个应用程序,其中我有一个学生列表(从SQLite获得)和单选按钮,说明学生是否在场。问题是我想获得列表中所有学生的状态(现在或不存在),以及他们的名字我似乎无法从列表中获得任何内容。

这是我的适配器:

import re
for index, s in df.text.iteritems():
    result = re.findall("(?<![@\w])@(\w{1,25})", s)
    print(','.join(result))

#CritCareMed
#CellCellPress
#gvwilson
#sciencemagazine
#MHendr1cks,nucAmbiguous

} 我有一个setOnCheckedChangeListener用于 EACH (包括当前和不在)单选按钮:

    public class ClassAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Student> atte;
private ClassStateAdapterListener listener;
private LayoutInflater mInflater;
private RadioGroup radioGroup;
public static ArrayList<String> selectedStates;


public ClassAdapter(Activity activity, List<Student> students,LayoutInflater inflater) {
    this.activity = activity;
    this.atte = students;
     selectedStates = new ArrayList<>();
    //selectedStates = new ArrayList<String>();
    for (int i = 0; i < atte.size(); i++) {
        selectedStates.add("error");
    }
    mInflater = inflater;
}

@Override
public int getCount() {
    return atte.size();
}

@Override
public Object getItem(int location) {
    return atte.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.class_list_item, null);

    radioGroup = (RadioGroup) convertView.findViewById(R.id.test_answer_options);
    final RadioButton attePresent = (RadioButton) convertView.findViewById(R.id.rep_present);
    RadioButton atteAbsent = (RadioButton) convertView.findViewById(R.id.rep_absent);

    TextView name = (TextView_Lato) convertView.findViewById(R.id.name);
    TextView sch_id = (TextView_Lato) convertView.findViewById(R.id.schoolId);
    TextView class_name = (Textview_lato_thin) convertView.findViewById(R.id.className);
    //TextView course_name = (Textview_lato_thin) convertView.findViewById(R.id.courseName);
    //atteState = selectedAnswerOption(radioGroup.getCheckedRadioButtonId());

    final Student student = atte.get(position);

    name.setText(student.getStud_nat_id());
    sch_id.setText(student.getSName());
    class_name.setText(student.getClassname());


    attePresent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked && selectedStates!= null && selectedStates.size() !=0){
                selectedStates.set(position, "present");
            }
        }
    });

    atteAbsent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked && selectedStates!= null && selectedStates.size() !=0)
                selectedStates.set(position, "absent");

        }
    });

    return convertView;
}

public interface ClassStateAdapterListener {
    public void getState(Student attendance);
}

我还有一个提交提交按钮,现在应该暂时保留学生列表和学生状态

attePresent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // supposed to set present values in ArrayList if Present RadioButton is checked
                if (isChecked && selectedStates!= null && selectedStates.size() !=0){
                    selectedStates.set(position, "present");
                }

            }
        });

该日志正在打印:

submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String message = "";
                // get the value of selected states from custom adapter
                for (int i = 0; i < ClassAdapter.selectedStates.size(); i++) {
                    message = message + "\n" + (i + 1) + " " + ClassAdapter.selectedStates.get(i);

                }

                Log.e("SSSSSSSSSSSSs", ClassAdapter.selectedStates.size()+"");
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
            }
        });

但我的名单目前有2名学生。我在这里错过了什么?请帮忙。

学生模特课:

10-08 18:52:51.900 9105-9105/**.**.*****.****** E/SSSSSSSSSSSSs: 0

提前致谢!

0 个答案:

没有答案