我想在文件上存储特定数据,操作完成但不是按要求! 代码:
if (value1 != null && value1.trim().length() > 0 && value2 != null && value2.trim().length() > 0)
{
float q1=Float.parseFloat(vol.getText().toString());
float q2=Float.parseFloat(kil.getText().toString());
float x=((q1 / q2)* 100);
String y= Float.toString(x);
cons.setText(y);
SimpleDateFormat format = new SimpleDateFormat("dd/MM");
String date = format.format(new Date());
data = date + " : " + y + " L/100km\n" + value1 + " L "+ value2 + " KM\n";
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
if (data != "" )
{
String fileName = getResources().getString(R.string.fileName);
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context, fileDir + fileName, data);
data = "";
}
在我的ListView中我有(例子):17/03 : 8.5L/100km
但我想拥有这个(正如我在代码中所做的那样!):17/03 : 8.5L/100km 20L 300KM
。
WriteSettings():
public class myIO {
public static void WriteSettings(Context context, String nom, String data) {
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try {
fOut = context.openFileOutput(nom, Context.MODE_APPEND);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
osw.close();
fOut.close();
} catch (Exception e) {
Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
} finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
Toast.makeText(context, "Echec", Toast.LENGTH_SHORT).show();
}
}
}
有什么问题?
答案 0 :(得分:0)
public void updatelv(Activity activity) {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
s = myIO.ReadFilePerLine(getApplicationContext(), fileDir+fileName);
ListView L = (ListView) findViewById(R.id.lv);
L.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, s[0]));
}
显示时请执行以下操作:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.histo);
context = getApplicationContext();
activity = this;
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."
+ preferences.getString("marque", "") + ".";
s = myIO.ReadFilePerLine(getApplicationContext(), fileDir + fileName);
**updatelv(this);**
所以? :\