我一直坚持我的密码。。
我已经用edittexts创建了一个arraylist,用户在其中填充,我试图从那些值(双倍值)中获取值,进行一些计算,并用textview将它们显示给另一个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和TextView。
对于检索并使字符串加倍,我有这样的想法,但它不起作用:(
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)
有人能帮忙吗??我的问题是我看不到文本视图的结果。。。
谢谢您!!
2条答案
按热度按时间3phpmpom1#
这是由于
java.lang.NumberFormatException
,这意味着要将空字符串或非数字字符串转换为Double
.在循环中转换之前过滤字符串,如下所示:,
cwdobuhd2#
我有你的问题。您正在尝试将空字符串转换为双值。这就是为什么你得到numberformatexception。您的代码应该是这样的: