如何从列表视图中获取无线电组的值?
How to get values of radio group from listview?
我正在开发Android考勤应用程序,老师可以在其中进行考勤。Here is the image. I have taken example from here。但我没有明白,如何在单击“提交”按钮时获取单选按钮的值。
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.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.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.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)
尝试在 Custom Arrayadapter 中这样实现
radio[i].setOnCheckedChangeListener(...)
和内部侦听器将 value(boolean)
存储在某个变量中,并包含 getter 函数来检索它。
你需要这样实现单选按钮的监听器
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
然后将选中按钮的状态保存在你喜欢的数据结构中。
之后,当您按下提交按钮时,您可以读取您的数据结构并发送您的 checked/unchecked 数据。
希望对您有所帮助!
由于 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
}
我正在开发Android考勤应用程序,老师可以在其中进行考勤。Here is the image. I have taken example from here。但我没有明白,如何在单击“提交”按钮时获取单选按钮的值。
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.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.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.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)
尝试在 Custom Arrayadapter 中这样实现
radio[i].setOnCheckedChangeListener(...)
和内部侦听器将 value(boolean)
存储在某个变量中,并包含 getter 函数来检索它。
你需要这样实现单选按钮的监听器
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
然后将选中按钮的状态保存在你喜欢的数据结构中。 之后,当您按下提交按钮时,您可以读取您的数据结构并发送您的 checked/unchecked 数据。
希望对您有所帮助!
由于 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
}