实际上是两个问题:与IF逻辑创建文件inbeded
主要问题: 我希望这不是真的,你不能简单的逻辑在写出一个文件! 我有一个光标在通常由onCreate调用的屏幕上加载动态列表。这样可行。现在,我需要写出“SD卡”作为备份。 (如果我的手表决定重置为第一天,我将重新加载 - 它也允许我从我的电脑添加具有键盘的条目)。
我决定最好的方法是调用现有的光标,但设置一个开关表明写出来。文件需要一个try-catch,所以我把它放在open,write和close中。 “作家”是未定义的。所以我把它放在一个“TRY”里面,如果没有括号的话,它就会起作用 - 没有“IF”。
但添加“IF(--SWITCH SET)”{--- writer.write(strBuRec); ...}“这需要{ - }现在再次作家是未定义的。
我当然希望我做了其他错误的事情(可能是愚蠢的)!我可以将代码复制到第二个游标,但不希望
第二个问题: 注意关闭游标(//cursor.close();)被注释掉了,这是因为如果我重新绘制屏幕或者在这种情况下,重新调用游标来写出我的文件如果我关闭它,我只能加载光标一次
注意:这是我的WIMMOne的一个简单的应用程序,所以它需要版本7.此代码是在一个片段,(坏的决定,但它在那里)
个非常感谢, 克拉克
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
{
Log.d("EventLst","0 LoadFin");
int iRecNo = 0;
iBuCnt = 0;
mAdapter.swapCursor(cursor);
//----------------------------------------
// if exporting, open the file
try
{
if (strRunBu == "Y")
{
FileWriter writer;
String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/Event";
File dir = new File(path);
Log.d("Eventfile","00 File:" + dir);
File flEvent = new File(dir, "EVENT.TXT");
boolean canIWrite = dir.canWrite();
Log.d("Eventfile","0 File:" + flEvent + "=" + canIWrite);
flEvent.createNewFile();
Log.d("Eventfile","1 File:" + flEvent);
writer = new FileWriter(flEvent);
}
// ------------------------------------------
// Insert dummy first record to serve as a label
//
String strBuRec = "";
strRecord.clear();
strRecord.add(0, "mm-dd-yy: Event name");
cursor.moveToFirst();
Log.d("EventLst","1 LoadFin DO");
// ----------------------------------------
// Read from cursor and add each record to list
while (cursor.isAfterLast() == false)
{
iRecNo = iRecNo + 1;
// - Table has 4 columns, read them into string array: strC
String strC[] = { (cursor.getString(0)), (cursor.getString(1)),
(cursor.getString(2)), (cursor.getString(3))
};
// - The fourth column is the date/time in milliseconds since
// January 1,1970
// convert to date in yyyy-mm-dd format
String strDateMil = (cursor.getString(3));
long lgDate = cursor.getLong(3);
Log.d("EventLst","4 LoadCSR:" + "I:" + iRecNo + "Ld:" + lgDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy");
String strDate = dateFormat.format(new Date(lgDate));
// - Concatenate date and event into one string, add to table
strRecord.add(iRecNo, strDate + ": " + strC[2]);
// - save record number for each event in strRecId
// - Records are sorted by date, so we need to save RowId to pass
// - to edit screen
strRecId.add((cursor.getString(0)));
//---------------------------
// if-creating export file, write a record
if (strRunBu == "Y")
{
dateFormat = new SimpleDateFormat("HH:mm");
String strTime = dateFormat.format(new Date(lgDate));
strBuRec = ((cursor.getString(1)) + "," + (cursor.getString(2))
+ "," + strDate + "," + strTime + "\r\n");
Log.d("EventLst","4 LoadCSR:" + "BU:" + strBuRec);
// ERROR: writer cannot be resolved ??????????
writer.write(strBuRec);
Log.d("Eventfile","4 File:" + "wrote");
}
strEventRec.add(iBuCnt, strBuRec);
iBuCnt = iBuCnt + 1;
cursor.moveToNext();
} // ----end of while loop
//------------------------------------
// COULD NOT CLOSE THE CURSOR?????
//cursor.close();
//------------------------------------
if (strRunBu == "Y")
{
// ERROR: writer cannot be resolved ???????????
writer.flush();
// ERROR: writer cannot be resolved ???????????
writer.close();
};
} //---> BACKTO try
catch (IOException e)
{
Toast.makeText(getActivity(), "Close ER"+ e,
Toast.LENGTH_SHORT).show();
}
Log.d("Eventfile","4 File:" + "Closed");
strRunBu = "N";
lstAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.event_row, R.id.text1, strRecord);
// * Call to SetListAdapter()informs ListFragment how to fill ListView
// * here use ArrayAdapter
setListAdapter(lstAdapter);
// Log.d("EventLst","8 LoadCSR:" + "ALLDONE");
}
该方法是不可思议的。 – 2013-05-02 02:05:18