我在listview中有一个网格格式的单选按钮,我试图获取相对于行和列的radiobutton的值,但在Toast中获取空值。为什么呢?
代码:
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
//RadioButton rb=(RadioButton) findViewById(checkedId);
//Toast.makeText(context, rb.getText(), Toast.LENGTH_SHORT).show();
for (int i = 0; i < group.getChildCount(); i++) {
btn = (RadioButton) group.getChildAt(i);
int t = group.getId();
System.out.println(t);
if (btn.getId() == checkedId) {
text1 = btn.getText().toString();
Toast.makeText(context, "You selected : " + text1, Toast.LENGTH_SHORT).show();
return;
}
}
}
});
完整代码:
RadioGroupAdapter.java
public class RadioGroupAdapter extends ArrayAdapter<Option> {
Context context;
int layoutResourceId;
Option data[] = null;
private RadioButton btn;
private String text1;
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[3];
for(int i=0; i<3; 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
holder.group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
//RadioButton rb=(RadioButton) findViewById(checkedId);
//Toast.makeText(context, rb.getText(), Toast.LENGTH_SHORT).show();
for (int i = 0; i < group.getChildCount(); i++) {
btn = (RadioButton) group.getChildAt(i);
int t = group.getId();
System.out.println(t);
if (btn.getId() == checkedId) {
text1 = btn.getText().toString();
Toast.makeText(context, "You selected : " + text1, Toast.LENGTH_SHORT).show();
return;
}
}
}
});
}
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;
}
}
MainActivity.java
public class MainActivity extends Activity {
private ListView listView1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Option weather_data[] = new Option[]
{
new Option("Heading1"),
new Option("Heading2"),
new Option("Heading3"),
new Option("Heading4"),
new Option("Heading5"),
new Option("Heading6"),
new Option("Heading7"),
new Option("Heading8"),
new Option("Heading9"),
new Option("Heading10"),
new Option("Heading11")
};
RadioGroupAdapter adapter = new RadioGroupAdapter(this,
R.layout.listitem, weather_data);
listView1 = (ListView)findViewById(R.id.list);
listView1.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Option.java
public class Option {
public String title;
public Option(){
super();
}
public Option( String title) {
super();
this.title = title;
}
}
答案 0 :(得分:1)
我知道你的错误。 看看这个
text1 = btn.getText().toString();
Toast.makeText(context, "You selected : " + text1,Toast.LENGTH_SHORT).show();
你想从btn.getText()。toString获取文本。但是btn没有任何数据,所以你无法从中获取文字。
也许我知道你的尝试,你想得到被检查的身份证明,对吗? 你应该把它改成
text1 = btn.getId().toString();
希望能帮到你
答案 1 :(得分:1)
您的单选按钮没有设置文本。这就是当您从单选按钮执行getText()
时它变为空的原因。
因此,通过调用btn.setText("yes")
为您的单选按钮设置一些值,并检索该值以识别其状态被检查的单选按钮。