0

其实有两个问题:

主要问题:我希望您在写出文件时不能有简单的逻辑,这不是真的!!!我有一个光标来加载通常由 onCreate 调用的屏幕上的动态列表。这样可行。现在,我需要写出“sdCard”作为备份。(如果我的手表决定重置到第一天,我将重新加载 - 它还允许我从有键盘的 PC 添加条目。)

我决定最好的方法是调用现有的光标,但设置一个开关来指示将其写出。文件需要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");

   }
4

1 回答 1

1

和定义范围{-}您需要确保在正确的范围内定义变量。即不在if范围内,而是包含if.

在我看来,你在那个 try-catch 块中的内容太多了!

另外:这条线if (strRunBu == "Y")有一个经典的菜鸟错误,将字符串与==-进行比较if (strRunBu.equals("Y"))

于 2013-05-02T01:54:49.183 回答