2012-02-29 18 views
0

実際に私のアプリにはEditTextのボックスとTextViewがあります。このEditTextボックスには、ファイルに格納されているものがすべて格納されます。だから私は再び私のアプリを開いたときに格納されたテキストはTextViewに表示されます。すべて私のためにうまく動作します。しかし、私はロシア語で言語を変更するたびに、EditTextボックスをクリックして表示されるキーボードはロシア文字を含んでいます。今私が入力したテキストを保存して読むのと同じプロセスに従うと、それを読んでいる間に私は保存したテキストの代わりにTextViewにガーベッジの値を表示して表示します。だから私の質問は、なぜこれは私が他の言語のテキストを保存したときにのみ起こるのです。なぜなら、テキストを英語で保存するとうまく動作するからです。ファイルにデータを格納する際に使用したコードを以下に示します。別の言語のファイルベースにデータを書き込んで読み込む方法

書き込みファイルに注意して読むには、ファイルからノート

public void writeNotesToFile(Context c) 
{ 
    SharedPreferences preferencesWrite = c.getSharedPreferences("myPreferences", 0); 
    SharedPreferences.Editor editor = preferencesWrite.edit(); 

    // write notes count into the register 
    editor.putInt("notesCount", m_noteCount); 
    editor.commit(); 

    // write notes to the file 
    SimpleDateFormat sdFormater = new SimpleDateFormat("dd-MMM-yyyy", Locale.US); 
    File file = c.getFileStreamPath("Notes"); 

    if (m_noteCount > 0) 
    { 
     if(!file.exists()) 
     { 
      try 
      { 
       file.createNewFile(); 
      } 
      catch (Exception e) 
      { 
       Log.i("ReadNWrite, fileCreate()", "Exception e = " + e); 
      } 
     }  
     try 
     { 
      FileOutputStream writer = c.openFileOutput("Notes", Context.MODE_PRIVATE); 
      for (int i = 0; i < m_noteCount; i++) 
      { 
       String noteDate = sdFormater.format(m_arrNoteDate[i]); 
       writer.write(noteDate.getBytes()); 
       writer.write(" ".getBytes()); 
       writer.write(m_arrNoteString[i].getBytes()); 
       writer.write("~`".getBytes()); 

      } 
      writer.close(); 
     } 
     catch (Exception e) 
     { 
      Log.i("ReadNWrite, fileCreate()", "Exception e = " + e); 
     } 
    } 
    else 
    { 
     try 
     { 
      file.createNewFile(); 
     } 
     catch (Exception e) 
     { 
      Log.i("ReadNWrite, fileCreate()", "Exception e = " + e); 
     } 
    } 

} 

を登録し、登録

public void readNotesFromFile(Context c) 
{  
    SharedPreferences preferencesRead = c.getSharedPreferences("myPreferences", 0); 

    // Reads notes count from the register 
    m_noteCount = preferencesRead.getInt("notesCount", 0); 

    // Reads notes from file 
    String note = ""; 
    char nextCharacter; 
    int count = 0, ch; 
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US); 

    File file = c.getFileStreamPath("Notes"); 
    if (m_noteCount > 0) 
    { 
     if(file.exists()) 
     { 
      try 
      { 
       FileInputStream fin = c.openFileInput("Notes"); 
       while((ch = fin.read()) != -1) 
       { 
        nextCharacter = (char)ch; 
        if (nextCharacter == '~') 
        { 
         ch = fin.read(); 
         nextCharacter = (char)ch; 
         if (nextCharacter == '`') 
         { 
          int i=note.indexOf(" "); 
          String temp = note.substring(0, i); 
          try 
          { 
           m_arrNoteDate[count] = formatter.parse(temp); 
          } 
          catch (Exception e) 
          { 
           // To handle dates saved before the file was written in Local.US format. 
           // This code can be removed after few releases and all user have migrated.       
           SimpleDateFormat defFormatter = new SimpleDateFormat("dd-MMM-yyyy"); 
           m_arrNoteDate[count] = defFormatter.parse(temp); 
          } 
          m_arrNoteString[count] = note.substring(i + 1); 
          count++; 
          note = ""; 
         } 
        } 
        else 
        { 
         note = note + nextCharacter; 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Log.i("ReadNWrite, readFile()", "Exception e = " + e); 

      } 
     } 
    } 
} 
+0

格納されている単語(ロシア語)がキーボードから入力したものと同じであることをファイルにチェックインしましたか? –

+0

yehファイルを格納するときに、キーボードから入力したのと同じ文字に格納されています。読み込み中だけで – AndroidDev

+0

がこのtv.setText(Html.fromHtml(your_text_from_file).toString())を試しています。それが動作するかどうか私に知らせてください。 –

答えて

0

問題

public void writeNotesToFile(Context c) 
{ 
    SharedPreferences preferencesWrite = c.getSharedPreferences("myPreferences", 0); 
    SharedPreferences.Editor editor = preferencesWrite.edit(); 

    // write notes count into the register 
    editor.putInt("notesCount", m_noteCount); 
    editor.commit(); 

    // write notes to the file 
    SimpleDateFormat sdFormater = new SimpleDateFormat("dd-MMM-yyyy", Locale.US); 
    File file = c.getFileStreamPath("Notes"); 

    if (m_noteCount > 0) 
    { 
     if(!file.exists()) 
     { 
      try 
      { 
       file.createNewFile(); 
      } 
      catch (Exception e) 
      { 
       Log.i("ReadNWrite, fileCreate()", "Exception e = " + e); 
      } 
     }  
     try 
     { 
         //convert to UTF format while Writing Data 
      FileOutputStream fos = new FileOutputStream(file); 
      Writer writer = new OutputStreamWriter(fos, "UTF8"); 

       // FileOutputStream writer = c.openFileOutput("Notes", Context.MODE_PRIVATE); 

      for (int i = 0; i < m_noteCount; i++) 
      { 
       String noteDate = sdFormater.format(m_arrNoteDate[i]); 
       writer.write(noteDate); 
       writer.write(" "); 
       writer.write(m_arrNoteString[i]); 
       writer.write("~`"); 

      } 
      writer.close(); 
     } 
     catch (Exception e) 
     { 
      Log.i("ReadNWrite, fileCreate()", "Exception e = " + e); 
     } 
    } 
    else 
    { 
     try 
     { 
      file.createNewFile(); 
     } 
     catch (Exception e) 
     { 
      Log.i("ReadNWrite, fileCreate()", "Exception e = " + e); 
     } 
    } 

} 

public void readNotesFromFile(Context c) 
{  
    SharedPreferences preferencesRead = c.getSharedPreferences("myPreferences", 0); 

    // Reads notes count from the register 
    m_noteCount = preferencesRead.getInt("notesCount", 0); 

    // Reads notes from file 
    String note = ""; 
    char nextCharacter; 
    int count = 0, ch; 
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US); 

    File file = c.getFileStreamPath("Notes"); 
    if (m_noteCount > 0) 
    { 
     if(file.exists()) 
     { 
      try 
      { 
            //convert to UTF format while Reading Data 
       FileInputStream fis = new FileInputStream(file); 
       InputStreamReader fin = new InputStreamReader(fis, "UTF8"); 

       // FileInputStream fin = c.openFileInput("Notes"); 

       while((ch = fin.read()) != -1) 
       { 
        nextCharacter = (char)ch; 
        if (nextCharacter == '~') 
        { 
         ch = fin.read(); 
         nextCharacter = (char)ch; 
         if (nextCharacter == '`') 
         { 
          int i=note.indexOf(" "); 
          String temp = note.substring(0, i); 
          try 
          { 
           m_arrNoteDate[count] = formatter.parse(temp); 
          } 
          catch (Exception e) 
          { 
           // To handle dates saved before the file was written in Local.US format. 
           // This code can be removed after few releases and all user have migrated.       
           SimpleDateFormat defFormatter = new SimpleDateFormat("dd-MMM-yyyy"); 
           m_arrNoteDate[count] = defFormatter.parse(temp); 
          } 
          m_arrNoteString[count] = note.substring(i + 1); 
          count++; 
          note = ""; 
         } 
        } 
        else 
        { 
         note = note + nextCharacter; 
        } 
       } 
      } 
      catch (Exception e) 
      { 
       Log.i("ReadNWrite, readFile()", "Exception e = " + e); 

      } 
     } 
    } 
} 
に解決 更新されたコード
+0

あなたの答えを受け入れてください! –

関連する問題