gpt4 book ai didi

java - android从2个不同的文件读取

转载 作者:行者123 更新时间:2023-12-02 12:07:23 26 4
gpt4 key购买 nike

在我的Android应用程序中,我尝试从2个不同的文件设置2个 TextView 名称,但由于某种原因,第一个 TextView 被设置为第二个文件信息“成分2”,而第二个 TextView 根本没有显示?我设置和打开文件的方式是否有问题?

public class GroceriesActivity extends AppCompatActivity {
public TextView groceryname1, groceryname2, groceryname3, groceryname4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_groceries);


groceryname1 = (TextView) findViewById(R.id.grocery1);
groceryname2 = (TextView) findViewById(R.id.grocery2);
groceryname3 = (TextView) findViewById(R.id.grocery3);
groceryname4 = (TextView) findViewById(R.id.grocery4);

String message;
String message2;
FileInputStream fis1 = null;
FileInputStream fis2 = null;
FileInputStream fis3 = null;
FileInputStream fis4 = null;
try {
fis1 = openFileInput("Ingredient1");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
InputStreamReader isr = new InputStreamReader(fis1);
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
try {
while ((message = br.readLine()) != null) {
sb.append(message);
}
} catch (IOException e1) {
e1.printStackTrace();
}
groceryname1.setText(sb.toString());
try {
fis1.close();
} catch (IOException e) {
e.printStackTrace();
}

try {
fis2 = openFileInput("Ingredient2");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

InputStreamReader isr2 = new InputStreamReader(fis2);
BufferedReader br2 = new BufferedReader(isr2);
StringBuffer sb2 = new StringBuffer();
try {
while ((message2 = br2.readLine()) != null) {
sb2.append(message2);
}
} catch (IOException e1) {
e1.printStackTrace();
}
groceryname2.setText(sb2.toString());
try {
fis2.close();
} catch (IOException e) {
e.printStackTrace();
}


}
}

所以作为tldr,我试图将textview1设置为成分1,将textview2设置为成分2,但现在textview1被设置为成分2,而textview2没有被更改

编辑:修复该错误后,我现在将 textview1 和 textview2 设置为第一个成分

最佳答案

尝试改变

sb.append(message2);

sb2.append(message2);

关于java - android从2个不同的文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46799969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com