Actually two questions:
The main question: I hope it isn't true that you can't have simple logic in writing out a file!!! I have a cursor to load a dynamic list on the screen normally invoked by onCreate. That works. Now, I need to write out to the "sdCard" as a back-up. (If my watch decides to reset to day one, I will reload - it also allows me to add entries from my PC which has a keyboard.)
I decided the best way is to call the existing cursor but set a switch to indicate to write it out. Files require a try - catch, so I put it around the open, the write, and the close. The "writers" are undefined. So I put it all inside one "TRY" that worked if there are no brackets - no "IF"s.
But add "IF (--SWITCH SET)" {---writer.write(strBuRec); ..}" which requires {--} now again the writer is undefined.
I sure hope I am doing something else wrong (probably something stupid)! I can copy the code into a second cursor, but prefer not to.
Second question: Note the close cursor (//cursor.close();) is commented out. This is because if I repaint the screen or in this case, re-invoke the cursor to write out my file, I get cursor closed. I can only load the cursor once if I close it.
Note: This is a simple app for my WIMMOne so it needs version 7. This code is in a fragment, (bad decision, but it's there).
Many thanks, Clark
@Override
public void onLoadFinished(Loaderloader, 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(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");
}
.
stackoverflow.comm
No comments:
Post a Comment