我想在RatingBar
的帮助下找到用户选择的一系列值的平均值。每次用户按下Button
时,我都想将RatingBar
的值写入文件。但是当我尝试保存该值时,新值将覆盖文件中的旧值而不是附加值。我宁愿将文件保存在内部存储器中。
之后,我想获取所有值并将它们放入ArrayList
。并计算所有给定值的平均值。
我是从C#开始的,所以WriteLine()
和ReadLine()
很容易,但writeline
不存在(至少,我没有在我的研究中找到)和某些情况,readline
已被弃用。
我试着这样做:
private void writeMyArray(double rate){
try{
FileWriter fileWritter = new FileWriter("test3",true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(Double.toString(rate));
bufferWritter.close();
}catch(IOException e){
e.printStackTrace();
}
private void readMyArray(ArrayList<String> list){
try {
InputStream inputStream = openFileInput("test3.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
while (true) {
String line = reader.readLine();
if (line == null) {
list.add("");
break;
}
list.add(line);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
当我按下按钮时,使用以下代码:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rating = ratingbar.getRating();
writeToFile(Double.toString(rating));
writeMyArray(ratingbar.getRating());
button.setText(getText(R.string.obrigado) + "!" + readFromFile() + arraydays.get(0));// + media(Double.parseDouble(readMyArray()), Integer.parseInt(stackread())));
//button.setText(getText(R.string.obrigado)+"!");
//ratingbar.setEnabled(false);
// button.setEnabled(false);
}
});
我收到此logcat错误:
07-06 10:54:44.180 29840-29840/com.example.emilio.notification E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.emilio.notification, PID: 29840
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.example.emilio.notification.MainActivity$2.onClick(MainActivity.java:116)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
写/读文件方法用于其他目的但工作正常,因此问题在于我的数组代码。
其他文件代码
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("estrela.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = openFileInput("estrela.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
答案 0 :(得分:2)
我假设你在这里寻找三种方法。一个将double
附加到文件,另一个将数字读取到ArrayList<Double>
,第三个计算平均评分。
写:
public void writeToFile(double rate) throws IOException {
FileOutputStream fout;
fout = new FileOutPutStream("myfile.txt", true);
new PrintStream(fout).println(rate);
fout.close();
}
读:
public List<Double> readFromFile() throws IOException {
List<Double> rateList = new ArrayList<Double>();
Scanner s = new Scanner(new FileInputStream("myfile.txt"), "utf-8");
while (s.hasNextLine()) {
rateList.add(Double.parseDouble(s.nextLine()));
}
return rateList;
}
查找平均评分:
public double findAverageRating(List<Double> rateList) {
double averageRating = 0;
for (double r : rateList)
averageRating += r;
return averageRating / rateList.size();
}
这意味着您可以在按钮侦听器中执行以下操作:
writeToFile(ratingbar.getRating());
button.setText(findAverageRating(readFromFile()));
答案 1 :(得分:0)
你的Logcat输出已经告诉你,出了什么问题。您拨打arraydays.get(0)
,而 arraydays 似乎的大小为0,但您正试图从中获取第一个值。您将使用list.add(line)
附加到名为 list 的变量。这就是我所理解的。你的代码是不可读的。