我创建了一个动态的android视图,并在其上添加了复选框和编辑文本,请检查以下代码。
在此代码中,'list2'的值为{a,b,c ....}
List<Integer> checkBoxIdList = new ArrayList<Integer>();
List<Integer> editTextIdList = new ArrayList<Integer>();
int id = 0;
int id1 = 0;
CheckBox ck;
TextView txtWt;
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(int i=0;i<list2.size();i++)
{
ck = new CheckBox(this);
ck.setId(id);
checkBoxIdList.add(id);
ck.setText(list2.get(i));
ll.addView(ck);
txtWt = new TextView(this);
txtWt.setText("Weight");
txtWt.setTag(list2.get(i));
txtWt.setVisibility(View.GONE);
ll.addView(txtWt);
edtWt = new EditText(this);
edtWt.setId(id1);
editTextIdList.add(id1);
ll.addView(edtWt);
id++;
id1++;
}
Button btnSubmit = new Button(this);
btnSubmit.setText("Submit");
ll.addView(btnSubmit);
this.setContentView(sv);
我正在尝试读取复选框的值和按钮单击上的编辑文本,如下所示
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < checkBoxIdList.size(); i++) {
CheckBox chk = (CheckBox)findViewById(checkBoxIdList.get(i));
String text = chk.getText().toString();
}
for(int i = 0; i < editTextIdList.size(); i++) {
try{
EditText edtWt = (EditText)findViewById(editTextIdList.get(i));
String text = edtWt.getText().toString();
Toast.makeText(getApplicationContext(), text,
Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_SHORT).show();
Log.e("pass 1", e.toString());
}
}
}
});
使用此功能时,我正确地将复选框的值设为a,b,c。但在阅读EditText时,我收到的错误就是这样
java.lang.ClassCastException: android.widget.CheckBox cannot be cast to android.widget.EditText
但我只是在填写EdiText之后阅读它。为什么会出现此错误?我该怎么解决?请帮帮我们。
答案 0 :(得分:1)
如评论中所述,您要为各种元素设置重复的ID。它是值的重要值,而不是用于设置id的变量。也就是说,您最好不要使用对视图的引用而不是ID。见下文:
List<CheckBox> checkBoxIdList = new ArrayList<CheckBox>();
List<EditText> editTextIdList = new ArrayList<EditBox>();
CheckBox ck;
TextView txtWt;
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(int i=0;i<list2.size();i++)
{
ck = new CheckBox(this);
checkBoxIdList.add( ck );
ck.setText(list2.get(i));
ll.addView(ck);
txtWt = new TextView(this);
txtWt.setText("Weight");
txtWt.setTag(list2.get(i));
txtWt.setVisibility(View.GONE);
ll.addView(txtWt);
edtWt = new EditText(this);
editTextIdList.add( edtWt );
ll.addView(edtWt);
}
Button btnSubmit = new Button(this);
btnSubmit.setText("Submit");
ll.addView(btnSubmit);
this.setContentView(sv);
然后直接访问它:
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < checkBoxIdList.size(); i++) {
String text = checkBoxIdList.get(i).getText().toString();
}
for(int i = 0; i < editTextIdList.size(); i++) {
try{
String text = editTextIdList.get(i).getText().toString();
Toast.makeText(getApplicationContext(), text,
Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_SHORT).show();
Log.e("pass 1", e.toString());
}
}
}
});