實際上是兩個問題:與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