我是新手,如果结果如此简单以至于我应该自己解决的话,我很抱歉。我花了几天时间思考它。我在这里研究了很多并搜索了许多其他帖子,但没有成功。
我有一个包含 DatePicker、Buttons、EditText 字段和 Spinners 的对话框。我可以填充我需要的一切,从我的数据库中的存储项目到对话框。但是,当我尝试将数字输入到某些 EditText 字段时,它会抛出 NumberFormatException。我可以将值硬编码到要存储的变量中,并隐藏我尝试获取该值的尝试,它会运行良好。这些值被存储,当我再次打开对话框时,它们会填充到正确的区域。
这是部分代码-
EditText et = new EditText(getContext());
for(int i=0; i<=etcount; i++){
placed = -1; //reset for next iteration
//try to get number from edittext
et.findViewById(i);
placed = Integer.parseInt(et.getText().toString());
awake++;
/*
try {
//et.findViewById(i);
placed = Integer.parseInt(findViewById(i).toString());
//placed = Integer.parseInt(et.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}*/
我注释掉了 try 块,因为我想快速排除故障。
在从 DB 获取值之前,etcount 变量在 onCreate 初始化为 -1 值。如果数据库中存储了值,则它会递增 1,然后调用代码以将 EditText 和 Spinner 动态添加到布局中。EditText id 也设置为 etcount 的值。这是代码-
//this will be ran when +placement button pressed, or if there are items in db stored
//adds 2 rows to dialog, one for textview to label items, one row for edittext with number
//and spinner with what item it is
private void createTableRow(int numPlaced, int itmPlaced){
etcount++; //used to count how many edittext fields there are so that they can be saved later
spincount++; //to count how many spinner there are
tl = (TableLayout)findViewById(R.id.tableLayoutVisit); //the tablelayout name
//need to create row for textview, then another row for edittext and spinner
tr1 = new TableRow(this.getContext()); //table row 1, for textview
tr1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView tv = new TextView(this.getContext());
tv.setText("Amount Placed");
tv.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr1.addView(tv); //add textview to tablerow1
tl.addView(tr1, new TableLayout.LayoutParams( //add row to tablelayout
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr2 = new TableRow(this.getContext()); //tablerow2: edittext and spinner
tr2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
EditText et = new EditText(this.getContext());
et.setId(etcount);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
if(numPlaced!=0){ //if being populated from previous visit
et.setText(Integer.toString(numPlaced));
//et.setText("" +numPlaced);
}
//need to have listener to read data
et.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
Spinner spin = new Spinner(this.getContext());
spin.setId(spincount);
spinArray = getContext().getResources().getStringArray(R.array.itemplaced);
ArrayAdapter adapter = new ArrayAdapter(getContext(),
android.R.layout.simple_spinner_item, spinArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
if(itmPlaced!=-1){
spin.setSelection(itmPlaced); //assign correct value
}
spin.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr2.addView(et);
tr2.addView(spin);
tl.addView(tr2, new TableLayout.LayoutParams( //add row to tablelayout
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
如果它是一个新项目并且当前未存储在 DB 中,我会传递它 (0,-1)。您也可能注意到我强迫它接受 TYPE_CLASS_NUMBER。但我认为问题不存在。同样,如果我对值进行硬编码,它将保存到数据库中,下次我打开它时,它会在布局中动态创建 EditText/Spinner 行,并用数据库中的值填充 EditText。
所以......我认为第一部分有问题,et.findViewById(i); 或放置 = Integer.parseInt(et.getText().toString());。如果从 DB 中没有填充任何内容,则“etcount”的值应为 -1,每次从 DB 中填充某些内容时,“etcount”的值应为 0 etc(etcount++)。
对不起,如果这是冗长的,想具体一点。希望这是足够的信息!任何帮助都会很棒!!!提前致谢。