我是新的,所以很抱歉,如果这变得如此简单,我应该已经解决了我自己。我花了几天的时间思考它。我研究了一吨,并在这里搜索了很多其他的帖子,但没有成功。从Android中的对话框中的动态创建的edittext获取整数值抛出NumberFormatException

我有一个与DatePicker,Buttons,EditText字段和Spinners的对话框。我可以将我需要的所有东西从DB中存储的项目填充到对话框中。但是,当我尝试获取输入到某些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块,因为我想快速解决。

在从数据库获取值之前,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));

}

我通过它(0,-1),如果它是一个新项目,目前未存储在数据库中。你也许注意到我强迫它接受TYPE_CLASS_NUMBER。但我认为这个问题不存在。同样,如果我硬编码值,它将保存到数据库中,下次打开它时,它会在布局中动态创建EditText/Spinner行,并使用数据库中的值填充EditText。

所以...第一部分我认为,一些错误,et.findViewById(i);或放置= Integer.parseInt(et.getText()。toString());.如果没有填充数据库,“etcount”的值应为-1,每次从DB填充数据时,等等(etcount ++)等于0。

对不起,如果这是longwinded,想要具体。希望这是足够的信息!任何帮助将是伟大的!提前致谢。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐