我坚持使用我的代码..
我创建了一个带有EditTexts的ArrayList,其中用户填充并且我试图从那些(这是双打)中获取值,进行一些计算并使用TextViews将它们显示给另一个ArrayList。
这是我第一次使用此代码...
counter++;
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
TextView tv;
List<TextView> allTvs = new ArrayList<TextView>();
ed = new EditText(Main_5.this);
allEds.add(ed);
ed.setId(counter);
ed.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f));
linearLayout.addView(ed);
ed.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL);
tv = new TextView(Main_5.this);
allTvs.add(tv);
tv.setId(counter);
tv.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f));
linearLayout.addView(tv);
在此部分工作之前,在按钮的单击上创建EditText和TextViews。
对于检索并使字符串加倍,我认为这一点,但它不起作用:(
Double[] doubles = new Double[allEds.size()];
String[] strings = new String[allEds.size()];
for(i=0;i<allEds.size();i++){
strings[i] = allEds.get(i).getText().toString();
doubles[i] = Double.parseDouble(strings[i]);
}
任何人都可以帮助我吗?谢谢!!
更新的
更改后,我发布了我的代码,如果有人能找到问题..
我的xml文件是:
< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"<br/>
xmlns:tools="http://schemas.android.com/tools" <br/>android:layout_width="match_parent"<br/>
android:layout_height="match_parent" <br/>android:paddingLeft="@dimen/activity_horizontal_margin"<br/>
android:paddingRight="@dimen/activity_horizontal_margin"<br/>
android:paddingTop="@dimen/activity_vertical_margin"<br/>
android:paddingBottom="@dimen/activity_vertical_margin" <br/>tools:context=".MainActivity"><br/>
<EditText
android:layout_width="130dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/txtInput"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="130dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/txtInput2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="130dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/txtInput3"
android:layout_below="@+id/delete"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView"
android:layout_alignBaseline="@+id/txtInput3"
android:layout_alignBottom="@+id/txtInput3"
android:layout_alignLeft="@+id/delete"
android:layout_alignStart="@+id/delete"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/txtInput3">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"/>
</ScrollView>
<Button
android:id="@+id/add"
android:layout_width="60dp"
android:layout_height="60dp"
android:text=""
android:background="@android:drawable/ic_menu_add"
android:tint="#91d1be"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp"
android:layout_below="@+id/txtInput"
android:layout_alignRight="@+id/txtInput3"
android:layout_alignEnd="@+id/txtInput3"
/>
我的java文件是:
public class Main_5 extends Activity {
private LinearLayout mainLinearLayout;
int counter=0, i=0;
EditText mEditText1,mEditText2,mEditText3;
TextView mTextView;
Button add,delete;
Double calculatedValue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_5);
mEditText1 = (EditText)findViewById(R.id.txtInput);
mEditText2 = (EditText)findViewById(R.id.txtInput2);
mEditText3 = (EditText)findViewById(R.id.txtInput3);
mTextView = (TextView)findViewById(R.id.textView);
add = (Button) findViewById(R.id.add);
mainLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((String.valueOf(mEditText1.getText()).length() == 0) ||
(String.valueOf(mEditText2.getText()).length() == 0) ||
(String.valueOf(mEditText3.getText()).length() == 0))
Toast.makeText(getApplicationContext(), "Fill the fields", Toast.LENGTH_LONG).show();
else {
Double value1 = Double.parseDouble(String.valueOf(mEditText1.getText()));
Double value2 = Double.parseDouble(String.valueOf(mEditText2.getText()));
Double value3 = Double.parseDouble(String.valueOf(mEditText3.getText()));
calculatedValue = (value2 / value3);
mTextView.setText(calculatedValue.toString());
counter++;
LinearLayout linearLayout = new LinearLayout(Main_5.this);
TableLayout.LayoutParams lp = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
linearLayout.setLayoutParams(lp);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
TextView tv;
List<TextView> allTvs = new ArrayList<TextView>();
ed = new EditText(Main_5.this);
allEds.add(ed);
ed.setId(counter);
ed.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f));
linearLayout.addView(ed);
ed.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL);
tv = new TextView(Main_5.this);
allTvs.add(tv);
tv.setId(counter);
tv.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f));
linearLayout.addView(tv);
mainLinearLayout.addView(linearLayout);
Double[] doubles = new Double[allEds.size()];
String[] strings = new String[allEds.size()];
Double[] results = new Double[allEds.size()];
String[] resultStrings = new String[allTvs.size()];
for(i=0;i<allEds.size();i++){
strings[i] = allEds.get(i).getText().toString();
if(strings[i].length()>0){
doubles[i] = Double.parseDouble(strings[i]);
results[i] = (doubles[i]*value2);
resultStrings[i] = Double.toString(results[i]);
allTvs.get(i).setText(String.valueOf(results[i]));
}}}}});}}
(我以较少的空间立即关闭; P)b
谁能帮忙?我的问题是我无法在TextViews上看到结果......
谢谢!!
答案 0 :(得分:0)
我遇到了你的问题。您正在尝试将空字符串转换为double值。这就是你获得NumberFormatException的原因。你的代码应该是这样的:
Double[] doubles = new Double[allEds.size()];
String[] strings = new String[allEds.size()];
for(i=0;i<allEds.size();i++){
strings[i] = allEds.get(i).getText().toString();
if(strings[i].length()>0){
doubles[i] = Double.parseDouble(strings[i]);
}
}
答案 1 :(得分:0)
归因于java.lang.NumberFormatException
,这意味着您要将空数字或非数字字符串转换为Double
。
在循环中转换之前过滤字符串as,
Double[] doubles = new Double[allEds.size()];
String[] strings = new String[allEds.size()];
for(i=0;i<allEds.size();i++){
strings[i] = allEds.get(i).getText().toString();
//Say no to NumberFormatException
if(strings[i] == null) {
doubles[i] = 0.0;
} else if(strings[i].trim().length < 1){
doubles[i] = 0.0;
} else {
doubles[i] = Double.parseDouble(strings[i]);
}
//now set the textview text
allTvs.get(i).setText(doubles[i] + ""); //Yeah, + ""
/* If the textView text didn't change for some reasons (which I don't know)
then get the textView and set its text then replace it with the one in
list
e.g:/
*/
TextView tv = allTvs.get(i);
tv.setText(doubles[i] + "");
allTvs.add(i, tv);
}