0

リスト ビューにファイル データを入力しようとしています。ここでやらなければならないことは、あるアクティビティでファイルを読み取り、このファイル データを別のアクティビティのリスト ビューに渡すことです。2 番目のアクティビティのリスト ビューでこのデータを正常に渡しましたが、データが 1 行で表示されます。個々のデータが個々の行に表示されていません...これが私のコードです...

FirstActivity.java

String myData = "";
String strLine;
String listName = "" ;
FileOutputStream fos;
FileInputStream fstream;
DataInputStream in;
String[] SavedFiles;
BufferedReader br;


public void readFile(String file) throws IOException
    {
        try
            {
                fstream = openFileInput(file);
                in = new DataInputStream(fstream);
                br = new BufferedReader(new InputStreamReader(in));

            while ((strLine=br.readLine()) != null)   
            {
                myData +=(strLine);
                mapList.add(strLine);

            }
            //FinalList.arrFriends = mapList;

            in.close();

            }
            catch (FileNotFoundException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Intent in1 = new Intent(getBaseContext(), FinalList.class);
            in1.putStringArrayListExtra("Data", mapList);
            startActivity(in1);

        }

SecondActivity.java

public class FinalList extends Activity{

    ListView lvFinal;
    ArrayAdapter<String> adapterFriends;
    public static ArrayList<String> arrFriends = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.final_list);

        lvFinal = (ListView)findViewById(R.id.list2);
        Intent in = getIntent();
        arrFriends = in.getStringArrayListExtra("Data");
        adapterFriends = new ArrayAdapter<String>(getBaseContext(), R.layout.text, arrFriends);
        lvFinal.setAdapter(adapterFriends);
        adapterFriends.notifyDataSetChanged();

    }

}

私は何が欠けていますか?出力イメージはこちら。

ここに画像の説明を入力

4

3 に答える 3

0

ファイルをチェックして、各行の後に「\n」、「\r」、「\r\n」が続いていることを確認してください。

于 2013-08-05T11:32:44.990 に答える
0

最後にそれをやった...ファイルを単語ごとに読む...

public void readFile(String file) throws IOException
        {
            fstream = openFileInput(file);

            Scanner scanFile = new Scanner(new DataInputStream(fstream));
            ArrayList<String> words = new ArrayList<String>();

            String theWord;     
            while (scanFile.hasNext()){
                theWord = scanFile.next();
                words.add(theWord);
                Toast.makeText(getBaseContext(), theWord, 1000).show();
            }


                Intent in1 = new Intent(getBaseContext(), FinalList.class);
                in1.putStringArrayListExtra("Data", words);
                startActivity(in1);

            }
于 2013-08-06T05:05:23.820 に答える