gpt4 book ai didi

android - 如何从 ListView 中获取 radio 组的值?

转载 作者:行者123 更新时间:2023-11-29 01:20:16 27 4
gpt4 key购买 nike

我正在开发 Android 考勤应用程序,老师可以在其中进行考勤。 Here is the image .我以 here 为例.但我没有明白,如何在单击“提交”按钮时获取单选按钮的值。

出勤.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;
}
}

选项.类

public class Option {
public String title;
int selectedId=-1;
public Option(){
super();
}

public Option( String title) {
super();
this.title = title;

}

}

日志

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) 

最佳答案

由于 ListView 重复使用 View ,因此您需要保持每个 View 相对于该 View (行)的位置。

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");
}
}
}
}

选项.java

public class Option {
String title;
int selectedId=-1;
//your constructor
}

关于android - 如何从 ListView 中获取 radio 组的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37254399/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com