我试图将arraylist输出到可读的html或文本文件中。我认为这应该很简单,但它只是在逃避我。所以这就是。我有一个日志文件,当玩家被淘汰出局时会填充该日志文件。这是屏幕截图:
然后我点击了保存日志按钮将其导出到文件中。无论文件是html还是txt,输出都无法使用:
那么如何让我的html或文本文件显示在屏幕上的活动中呢?
谢谢, Hendo
这里是活动的代码......
import static com.feltinghendo.www.pokerdbtest.MainActivity.userList;
公共类GameLog扩展AppCompatActivity { private static final String TAG =" GameLog";
TextView gameLog;
ListView lvLog;
Button btnSave, btnClear;
private static ArrayList<String> bustOutLog = new ArrayList<>();
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_log);
gameLog = (TextView) findViewById(R.id.tvGameLog);
lvLog = (ListView) findViewById(R.id.lvLog);
btnSave = (Button) findViewById(R.id.btnSave);
btnClear = (Button) findViewById(R.id.btnClear);
gameLog.setText("Game Log");
Bundle extras = getIntent().getExtras();
if (extras != null) {
String bustLog = extras.getString("BUSTED");
Log.d(TAG, "onCreate: " + bustLog);
bustOutLog.add(bustLog);
}
lvLog.setChoiceMode(ListView.CHOICE_MODE_NONE);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, bustOutLog);
lvLog.setAdapter(arrayAdapter);
lvLog.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemLongClick: starts");
String undoBust = bustOutLog.get(position);
Log.d(TAG, "onItemLongClick: UNDO str = " + undoBust);
String[] cut = undoBust.split(" ");
String undo = cut[0];
Log.d(TAG, "onItemLongClick: remove player " + undo);
String sql = "UPDATE players SET active = 1 WHERE name = '" + undo + "'";
Log.d(TAG, "onItemLongClick: undo sql" + sql);
userList.execSQL(sql);
// MainActivity.userList.execSQL(&#34; UPDATE player SET active = 1 WHERE name =&#39;&#34; + undo +&#34;&#39;&#34;); < / p>
UserList.boughtIn.add(undo);
bustOutLog.remove(position);
arrayAdapter.notifyDataSetChanged();
return true;
}
});
arrayAdapter.notifyDataSetChanged();
UserList.spinnerArrayAdapter.notifyDataSetChanged();
}
public void saveLog(View view) {
// get permission - if we don't already have permission, we must request it from the user.
// Very quick solution - DO NOT JUST DO THIS IN PRODUCTION CODE! Refer to Section 10.
int hasWriteESPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (hasWriteESPermission == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "onCreate: permission granted");
} else {
Log.d(TAG, "onCreate: requesting permission");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
try {
FileOutputStream fos =
new FileOutputStream(
new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "gamelog.html"));
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(bustOutLog);
os.close();
Log.d(TAG, "saveLog: File written");
Toast.makeText(this, "File Written", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "saveLog: File Not Written");
Toast.makeText(this, "File Not Written", Toast.LENGTH_SHORT).show();
}
}
public void clearLog(View view) {
bustOutLog.clear();
arrayAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
这里没有代码,所以我们无法确定你的问题是什么,但我认为这是你的日志写作。因此,如果您将String存储在ArrayList中,您可以执行以下操作,在非常类似的情况下对我来说效果很好
FileWriter logger = new FileWriter("log.txt");
for(String line: listLog) { //listLog being
logger.write(line);
}
logger.close();
PS:您可以使用lambda表达式遍历数组列表,这是一个更好的实践。
答案 1 :(得分:0)
我不知道您的代码,但看起来您似乎缺少为输出提供某种格式。
创建一个StringBuffer。
遍历您的列表并附加到StringBuffer
StringBuffer toString()方法将为您提供可以输出的整个字符串。
答案 2 :(得分:0)
您可以尝试以下代码将ArrayList转换为String。
StringBuilder sb = new StringBuilder();
for (String s : list)
{
sb.append(s); sb.append("\n");
}
然后,您可以在将内容写入文件时使用 sb.toString()。
答案 3 :(得分:0)
我已经成功地走了这条路线。
File logFile = new File(basePath, "gamelog.txt");
FileWriter ofs = new FileWriter(logFile);
BufferedWriter outStream = new BufferedWriter(ofs);
for (int i=0; i < bustOutLog.size(); i++) {
Log.d(TAG, "onClick: writing " + bustOutLog.get(i));
outStream.write(bustOutLog.get(i));
outStream.write(String.format("\n"));
}
outStream.close();
ofs.close();
在我的一位教练的帮助下,我能够很好地完成这项工作。
我会说我必须使用Notepad ++正确读取文本文件(使用换行符),因为Windows Notepad无法识别文本中的换行符。我发现这很奇怪。
感谢您对此的所有帮助。我确实尝试使用StringBuilder,如此处所示。虽然它没有消除所有似乎放入文本文件的额外字符,但确实有效。我认为这是我的新代码工作。 : - )
谢谢大家! Hendo