java - 如何转换列表列出安卓
问题描述
有没有一种简单的方法可以将 List 类型从 String 更改为 Float 并将其也应用于其他列表?无需使代码复杂。我曾经使用 Python,所以 android 和 java 语法让我搞砸了很多事情
我有一个 csv 文件,其中第一列包含字符串值,其余列包含浮点值。当我调用 list.get(value_id) 时,我想从列表中获取数据作为浮点数
下面的代码:
package com.example.nick.calcy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Mix extends AppCompatActivity {
InputStream inputStream;
List<String> komponenty,em,bi,ws,ca,fs,na,L,M = new ArrayList<>();
String[] ids;
EditText et1,et2,et3,et4,et5,et6,et7;
Float sum;
Button buttonSum;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mix);
et1 = findViewById(R.id.editText11);
et2 = findViewById(R.id.editText12);
et3 = findViewById(R.id.editText13);
et4 = findViewById(R.id.editText14);
et5 = findViewById(R.id.editText15);
et6 = findViewById(R.id.editText16);
et7 = findViewById(R.id.editText17);
buttonSum = findViewById(R.id.button11);
/**Loading the input file*/
inputStream = getResources().openRawResource(R.raw.brojler);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
ids=csvLine.split(",");
try{
komponenty.add(ids[0]);
em.add(ids[1]);
bi.add(ids[2]);
ws.add(ids[3]);
ca.add(ids[4]);
fs.add(ids[5]);
na.add(ids[6]);
L.add(ids[7]);
M.add(ids[8]);
}catch (Exception e){
Log.e("Unknown error",e.toString());
}
}
}
catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}
/** button activity*/
buttonSum.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(et1.getText().toString().trim().length() == 0 ||
et2.getText().toString().trim().length() == 0 ||
et3.getText().toString().trim().length() == 0 ||
et4.getText().toString().trim().length() == 0 ||
et5.getText().toString().trim().length() == 0 ||
et6.getText().toString().trim().length() == 0 ||
et7.getText().toString().trim().length() == 0) {
CharSequence text = "Blank fields detected";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(getApplicationContext(),Summary.class);
Float x1 = Float.parseFloat(et1.getText().toString());
Float x2 = Float.parseFloat(et2.getText().toString());
Float x3 = Float.parseFloat(et3.getText().toString());
Float x4 = Float.parseFloat(et4.getText().toString());
Float x5 = Float.parseFloat(et5.getText().toString());
Float x6 = Float.parseFloat(et6.getText().toString());
Float x7 = Float.parseFloat(et7.getText().toString());
sum = x1+x2+x3+x4+x5+x6+x7;
i.putExtra("x1", x1);
i.putExtra("x2", x2);
i.putExtra("x3", x3);
i.putExtra("x4", x4);
i.putExtra("x5", x5);
i.putExtra("x6", x6);
i.putExtra("x7", x7);
if (sum == 100){
startActivity(i);
} else{
CharSequence text = "Sum must equal 100%";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
}}
});
}
}
我有 2 个想法
改变我阅读 csv 文件的方式,我试图这样做
Float ids2;
ids2 = Float.parseFloat(csvLine.split(","));
然后将其添加到准备好的列表中,但我搞砸了数据类型
第二个想法编写函数,它将简单地根据其 id 从列表中转换单个值的数据类型
private Float getFloat(List list, int id)
{
Object value;
String step2;
Float changed;
value = list.get(id);
step2 = value.toString();
changed = Float.parseFloat(step2);
return changed;
}
可悲的是,它在使用时使我的应用程序崩溃
09-03 17:42:39.166 18888-18888/com.example.nick.calcy E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
@Edit实际上我发现了一个问题,我没有正确填写列表,试图解决这个问题
@Edit 2 我无法从原始文件夹中获取数据,调试器显示:
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
I/chatty: uid=10083(u0_a83) com.example.nick.calcy expire 8 lines
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
立即使用该代码
package com.example.nick.calcy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Mix extends AppCompatActivity {
InputStream inputStream;
List<String> komponenty = new ArrayList<String>();
List<String> em = new ArrayList<String>();
String[] ids;
EditText et1,et2,et3,et4,et5,et6,et7;
Float sum,x11;
Button buttonSum;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mix);
et1 = findViewById(R.id.editText11);
et2 = findViewById(R.id.editText12);
et3 = findViewById(R.id.editText13);
et4 = findViewById(R.id.editText14);
et5 = findViewById(R.id.editText15);
et6 = findViewById(R.id.editText16);
et7 = findViewById(R.id.editText17);
buttonSum = findViewById(R.id.button11);
inputStream = getResources().openRawResource(R.raw.brojler);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null)
{
ids=csvLine.split(",");
try{
komponenty.add(ids[0]);
em.add(ids[1]);
}
catch (Exception e){
Log.e("Unknown error",e.toString());
}
}
}
catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}
/** button activity*/
buttonSum.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(et1.getText().toString().trim().length() == 0 ||
et2.getText().toString().trim().length() == 0 ||
et3.getText().toString().trim().length() == 0 ||
et4.getText().toString().trim().length() == 0 ||
et5.getText().toString().trim().length() == 0 ||
et6.getText().toString().trim().length() == 0 ||
et7.getText().toString().trim().length() == 0) {
CharSequence text = "Blank fields detected";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(getApplicationContext(),Summary.class);
Float x1 = Float.parseFloat(et1.getText().toString());
Float x2 = Float.parseFloat(et2.getText().toString());
Float x3 = Float.parseFloat(et3.getText().toString());
Float x4 = Float.parseFloat(et4.getText().toString());
Float x5 = Float.parseFloat(et5.getText().toString());
Float x6 = Float.parseFloat(et6.getText().toString());
Float x7 = Float.parseFloat(et7.getText().toString());
sum = x1+x2+x3+x4+x5+x6+x7;
x11 = 100*getFloat(em,0);
i.putExtra("x1", x1);
i.putExtra("x2", x2);
i.putExtra("x3", x3);
i.putExtra("x4", x4);
i.putExtra("x5", x5);
i.putExtra("x6", x6);
i.putExtra("x7", x7);
if (sum == 100){
startActivity(i);
} else{
CharSequence text1 = "Sum must equal 100%";
Toast.makeText(getApplicationContext(), text1, Toast.LENGTH_LONG).show();
}
}}
});
}
private Float getFloat(List<String> list, int id)
{
String value = list.get(id);
return Float.parseFloat(value);
}
}
解决方案
试试这个:循环遍历List<String>
每个项目并将其转换为浮点数,然后将其添加到List<Float>
.
我编写了这个函数,它接受List<String>
并返回一个List<Float>
. 我希望这个对你有用。
List<Float> getList(List<String> stringList)
{
List<Float> floatList = new ArrayList<>();
for (int i = 0; i < stringList.size(); i++)
{
Float number = Float.valueOf(stringList.get(i));
floatList.add(number);
}
return floatList;
}
然后,像这样调用函数:
List<Float> floats = getList(strings);
推荐阅读
- javascript - 在哪里定义 javascript 函数 html/js
- python-3.x - 在泛型类中调用类型参数的构造函数
- vue.js - 具有多维ajax数据行跨度的ant design vue表
- python - 简单的 matplotlib 问题 - 变量不起作用
- sqlite - 在 Qt 中,是否可以将数据库单元映射到小部件的属性
- flutter - “图像资源服务”捕获“无法加载资产错误”
- ios - 更改在 for 循环 Swift 中以编程方式创建的 UIButton 的图像
- flutter - 从 url 中删除 # 后,无法通过更改 url 在 Flutter Web 中导航
- mysql - 如何将 MySQL 数据库复制到同一服务器中的另一个数据库
- php - 为什么我在尝试让我的 PHP CLI 脚本删除毫无价值的空“相机胶卷”目录时总是收到“权限被拒绝”?