所以我需要帮助,我不知道什么是我没有任何错误。只是不工作 所以我得到了一个EditText(TextInput)你在那里写的东西例如“马铃薯”,当你按下Button(save和addButton)应该在TextInput中取什么并将它放入SD卡然后ListView(TextOutput)应该读取从该文件夹中显示它所说的内容。希望你们明白
我正在使用的代码(已更新)
public File file = new File(Environment.getExternalStorageDirectory() + "/accelerometer.html");
public void onCreate1(Bundle savedInstanceState) {
ListView lv = (ListView)findViewById(R.id.textOutput);
final ArrayAdapter<String> arrAdap = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
lv.setAdapter(arrAdap);
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
try {
arrAdap.addAll(load());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
arrAdap.notifyDataSetChanged();
}
});
}
public void save() {
String textInput = mTextInput.getText().toString().trim();
if(textInput.equals("")) {
return;
}
}
@SuppressWarnings("unchecked")
public ArrayList<String> load() throws IOException{
FileInputStream fis = null;
ObjectInputStream ois = null;
ArrayList<String> returnlist = null;
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
returnlist = (ArrayList<String>) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fis != null)
fis.close();
if(ois != null)
ois.close();
}
return returnlist;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
提前感谢所有帮助:)
答案 0 :(得分:0)
在bufferWritter.flush();
bufferWritter.write(TextImput);
编辑:
试试这段代码:
private File file = new File(Environment.getExternalStorageDirectory() + "/accelerometer.html");
@Override
public void onCreate(Bundle savedInstanceState) {
ListView lv = (ListView)findViewById(R.id.textOutput);
final ArrayAdapter<String> arrAdap = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
lv.setAdapter(arrAdap);
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save();
arrAdap.add(load());
arrAdap.notifyDataSetChanged();
}
});
}
public void save() {
String textInput = mTextInput.getText().toString().trim();
if(textInput.equals("")
return;
AddButton.setOnClickListener(new OnClickListener() { //i don't know what this should do
@Override //you set the same text to the same textview
public void onClick(View arg0) { //-> does nothing
mTextInput.setText(mTextInput.getText());
}
});
ArrayList<String> input = new ArrayList<>();
input.add(textInput);
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(input);
oos.flush();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if(fos != null)
fos.close();
if(oos != null)
oos.close();
}
}
public ArrayAdapter<String> load(){
FileInputStream fis;
ObjectInputStream ois;
ArrayList<String> returnlist;
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
returnlist = (ArrayList<String>) ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fis != null)
fis.close();
if(ois != null)
ois.close();
}
return returnlist;
}
它只是编码出我的想法。没有经过测试。
也许你必须在finally语句中处理一些IOExceptions,但只是在它周围添加另一个try {...} catch(){...}。
我还添加了评论。