我正在开发Android考勤应用程序,其中教师可以参加考试。Here is the image。我从here开始做了一些例子。但是我没有得到,当点击SUBMIT按钮时如何获得单选按钮的值。
Attendance.class
public class Attendance extends Activity {
private ListView listView1;
Button submit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.attendance);
submit = (Button) findViewById(R.id.testbutton);
Option weather_data[] = new Option[]
{
new Option("Student1"),
new Option("Student2"),
new Option("Student3"),
new Option("Student4"),
new Option("Student5"),
new Option("Student6")
};
RadioGroupAdapter adapter = new RadioGroupAdapter(this,
R.layout.listitem, weather_data);
listView1 = (ListView)findViewById(R.id.list);
listView1.setAdapter(adapter);
}
}
RadioGroupAdapter.class
public class RadioGroupAdapter extends ArrayAdapter<Option> {
Context context;
int layoutResourceId;
Option data[] = null;
public RadioGroupAdapter(Context context, int layoutResourceId,
Option[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MatrixHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MatrixHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.heading);
holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
final RadioButton[] rb = new RadioButton[2];
for(int i=0; i<2; i++){
rb[i] = new RadioButton(context);
//rb[i].setButtonDrawable(R.drawable.single_radio_chice);
rb[i].setId(i);
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight=1.0f;
params.setMargins(5, 0, 5, 10);
holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
}
row.setTag(holder);
} else {
holder = (MatrixHolder) row.getTag();
}
Option option = data[position];
holder.txtTitle.setText(option.title);
return row;
}
static class MatrixHolder {
TextView txtTitle;
RadioGroup group;
int position;
}
}
option.class
public class Option {
public String title;
int selectedId=-1;
public Option(){
super();
}
public Option( String title) {
super();
this.title = title;
}
}
logcat的
05-17 13:17:13.254 5141-5141/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.pitechnologies.teacher, PID: 5141
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.pitechnologies.teacher.Option[] org.pitechnologies.teacher.RadioGroupAdapter.getItems()' on a null object reference
at org.pitechnologies.teacher.Attendance.onClick(Attendance.java:40)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
当ListView重新使用视图时,您需要保持每个视图相对于该视图(行)的位置。
<强> RadioGroupAdapter.java 强>
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MatrixHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MatrixHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.heading);
holder.group = (RadioGroup) row.findViewById(R.id.radio_group);
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
int pos = (int) radioGroup.getTag();
data[pos].selectedId = radioGroup.getCheckedRadioButtonId();
}
});
row.setTag(holder);
} else {
holder = (MatrixHolder) row.getTag();
}
Option option = data[position];
holder.txtTitle.setText(option.title);
holder.group.setTag(position);
if (option.selectedId != -1) {//check at-least one is selected
holder.group.check(option.selectedId);
}
return row;
}
//get all items attached to adapter
public Option[] getItems() {
return data;
}
<强> Attendance.java 强>
public void onClick(View view) {
if (view.getId() == R.id.submit) {
Option[] items = adapter.getItems();
for (Option opt : items) {
if (opt.selectedId != -1) {
Log.i("TAG", "Title " + opt.title + " Selected " + (opt.selectedId == R.id.radio_a ? "A" : "P"));
} else {
Log.i("TAG", "Title " + opt.title + " Not Selected Any Button");
}
}
}
}
<强> Option.java 强>
public class Option {
String title;
int selectedId=-1;
//your constructor
}
答案 1 :(得分:0)
尝试在Custom Arrayadapter中实现这样的功能
radio[i].setOnCheckedChangeListener(...)
和内部侦听器将value(boolean)
存储在某个变量中,并包含用于检索它的getter函数。
答案 2 :(得分:0)
您需要以这种方式在单选按钮上实现侦听器
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
然后将选中按钮的状态保存在您喜欢的数据结构中。 之后,当您按下提交按钮时,您可以读取数据结构并发送已检查/未检查的数据。
我希望这有帮助!