Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从ListView获取EditText值,进行double并进行计算_Java_Android_Listview - Fatal编程技术网

Java 从ListView获取EditText值,进行double并进行计算

Java 从ListView获取EditText值,进行double并进行计算,java,android,listview,Java,Android,Listview,我坚持使用我的代码 我已经用EditText创建了一个ArrayList,用户在其中填充,我试图从这些值(双倍值)中获取值,进行一些计算,并用TextView将它们显示给另一个ArrayList 这是我第一次使用这个代码 counter++; EditText ed; List<EditText> allEds = new ArrayList<EditText>(); TextView tv; List<TextView> allTvs = new Arra

我坚持使用我的代码

我已经用EditText创建了一个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);
counter++;
编辑文本;
List alled=new ArrayList();
文本视图电视;
List allTvs=new ArrayList();
ed=新编辑文本(Main_5.this);
添加(ed);
ed.setId(计数器);
ed.setLayoutParams(新的TableLayout.LayoutParams(0,ViewGroup.LayoutParams.WRAP_内容,0.5f));
线性布局。添加视图(ed);
ed.setInputType(InputType.TYPE_类_号| InputType.TYPE_号_标志_小数);
tv=新文本视图(主视图5.this);
所有电视。添加(电视);
tv.setId(计数器);
tv.setLayoutParams(新的TableLayout.LayoutParams(0,ViewGroup.LayoutParams.WRAP_内容,0.5f));
线性布局。添加视图(电视);
直到此部件工作并在按钮单击时创建EditText和TextView 对于检索并使字符串加倍,我有这样的想法,但它不起作用:(

Double[]doubles=新的Double[allEds.size()];
String[]strings=新字符串[allEds.size()];
对于(i=0;i


我的java文件是:

   < 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"
     />
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]));

                    }}}}});}}
public class Main_5扩展活动{
私人线路布局主线路布局;
int计数器=0,i=0;
编辑文本mEditText1、mEditText2、mEditText3;
文本视图mTextView;
按钮添加、删除;
双重计算值;
@凌驾
创建时的公共void(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=(按钮)findViewById(R.id.add);
mainLinearLayout=(LinearLayout)findViewById(R.id.LinearLayout);
add.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if((String.valueOf(mEditText1.getText()).length()=0)||
(String.valueOf(mEditText2.getText()).length()=0)||
(String.valueOf(mEditText3.getText()).length()=0)
Toast.makeText(getApplicationContext(),“填充字段”,Toast.LENGTH_LONG.show();
否则{
Double value1=Double.parseDouble(String.valueOf(mEditText1.getText());
Double value2=Double.parseDouble(String.valueOf(mEditText2.getText());
Double value3=Double.parseDouble(String.valueOf(mEditText3.getText());
计算值=(值2/3);
mTextView.setText(calculatedValue.toString());
计数器++;
LinearLayout LinearLayout=新的LinearLayout(主视图5.本图);
TableLayout.LayoutParams lp=新的TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_父项,ViewGroup.LayoutParams.WRAP_内容,1f);
linearLayout.setLayoutParams(lp);
linearLayout.setOrientation(linearLayout.HORIZONTAL);
编辑文本;
List alled=new ArrayList();
文本视图电视;
List allTvs=new ArrayList();
ed=新编辑文本(Main_5.this);
添加(ed);
ed.setId(计数器);
ed.setLayoutParams(新的TableLayout.LayoutParams(0,ViewGroup.LayoutParams.WRAP_内容,0.5f));
线性布局。添加视图(ed);
ed.setInputType(InputType.TYPE_类_号| InputType.TYPE_号_标志_小数);
tv=新文本视图(主视图5.this);
所有电视。添加(电视);
tv.setId(计数器);
tv.setLayoutParams(新的TableLayout.LayoutParams(0,ViewGroup.LayoutParams.WRAP_内容,0.5f));
线性布局。添加视图(电视);
mainLinearLayout.addView(linearLayout);
Double[]doubles=新的Double[allEds.size()];
String[]strings=新字符串[allEds.size()];
Double[]results=新的Double[allEds.size()];
String[]ResultString=新字符串[allTvs.size()];
对于(i=0;i0){
doubles[i]=Double.parseDouble(字符串[i]);
结果[i]=(双倍[i]*值2);
结果字符串[i]=Double.toString(结果[i]);
allTvs.get(i).setText(String.valueOf(results[i]));
}}}}});}}
(为了节省空间,我一次关闭了所有的门;p)

有人能帮忙吗?我的问题是我在文本视图上看不到结果…

谢谢!!

我明白了你的问题。你正在尝试将空字符串转换为双值。这就是为什么你会收到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]);
   }
}
Double[]doubles=新的Double[allEds.size()];
String[]strings=新字符串[allEds.size()];
对于(i=0;i0){
doubles[i]=Double.parseDouble(字符串[i]);
}
}

这是由于
java.lang.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();

//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);
}
Double[]doubles=新的Double[allEds.size()];
String[]strings=新字符串[allEds.size()];

对于(i=0;i您遇到的确切问题是什么?什么不起作用?您可以发布堆栈跟踪吗?我对第一个代码没有问题,我只是发布它来看看我做了什么…我对第二个代码有问题,我试图检索数据并使用它们..我不知道它是否正确..我的错误是is:
java.lang.NumberFormatException:无效双精度:“
位于java.lang.StringToReal.invalidReal(StringToReal.java:63)
位于java.lang.StringToReal.p
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);
}