1

私は辞書アプリを開発しています。アプリにはお気に入りボタンがあり、ユーザーは次のことができます。

  • 短くクリックして、現在表示されている単語をお気に入りリストに追加します。
  • 長押しすると、お気に入りリスト (追加された単語) が表示されます。

これまでのところ、次のようにコーディングしました。

更新されたコード:

//Writing lines to myFavourite.txt
    btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite);
    btnAddFavourite.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Writing the content
                try {
                // opening myFavourite.txt for writing
                  OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myFavourite.txt", MODE_APPEND));
                // writing the ID of the added word to the file
                  out.write(mCurrentWord);  
                // closing the file
                  out.close();
                } catch (java.io.IOException e) {
                  //doing something if an IOException occurs.
                }
                Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
                toast.show();
            }
        });

    //Reading lines from myFavourite.txt
    btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() {         

            @Override
            public boolean onLongClick(View v) {

             //trying opening the myFavourite.txt
               try {
             // opening the file for reading
                InputStream instream = openFileInput("myFavourite.txt");

             // if file the available for reading
                if (instream != null) {
            // prepare the file for reading
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader buffreader = new BufferedReader(inputreader);

            String line;

            // reading every line of the file into the line-variable, on line at the time
            try {
                while ((line = buffreader.readLine()) != null) {
                  // do something with the settings from the file
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

          }

          // closing the file again
          try {
            instream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        } catch (java.io.FileNotFoundException e) {

          // ding something if the myFavourite.txt does not exits
        }
        return false;

        }});
        }

ただし、上記のコード行では [お気に入り] ボタンは機能しません。

ファイルmyFavourite.txtは (Eclipse の data/data/my_project/files に) 存在しますが、最近追加された単語が 1 つしか含まれていません。また、お気に入りボタンを長押しするとアプリが強制終了します。

私は何を間違っていましたか?皆さんがこの問題を解決するのを手伝ってくれたら、とても感謝しています。どうもありがとうございました。

========

編集

ご助力ありがとうございます。コメントとプロンプトを反映するようにコードを更新しました。ここまでで、いくつかの改善が行われました: お気に入りの単語は、word2 word2 word3 ... のようにファイルmyFavourite.txtに書き込まれました(新しい行に表示したいのですが)。

ただし、[お気に入り] ボタンを長押ししても、お気に入りリストは読み込まれません。

実際、私の目的は、アプリ内でお気に入りリストを読み込めるようにし、ユーザーが単語を選択して再度検索できるようにすることです。

助けてくれて本当にありがとうございます。

4

2 に答える 2

1

この行で

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myFavourite.txt",0));

ストリームを作成するたびにファイルが既に存在する場合は、ファイルを上書きしています。あなたがしたいことは、0 の代わりに MODE_APPEND を渡すことです。ドキュメンテーションを見てください。

長いクリックに関しては、これらの行

if (instream) {

while (( line = buffreader.readLine())) {

コンパイルさえすべきではありません。あなたが望むのはおそらく次のようなものです

if (instream.ready()) {

while ((line = buffreader.readLine()) != null) {
    // Use this line
}
于 2011-11-06T10:03:17.820 に答える
0

Androidでテキストファイルを読むのが簡単なこの例を見てください。

ファイルに保管する必要があります。

res/raw/myFavourite.txt .

あなたの問題と同じです。AndroidでSDカードからテキストファイルを読み取るにはどうすればよいですか?

別の簡単なチュートリアル。テキスト ファイルの読み方。 http://android-er.blogspot.com/2010/07/display-text-file-in-resraw_01.html

于 2011-11-06T10:06:13.063 に答える