0

我正在尝试用我的文件数据填充列表视图。我在这里要做的是,我必须在一个活动中读取文件并将此文件数据传递到另一个活动的列表视图中。我已在第二个活动的列表视图中成功传递了这些数据,但数据显示在单行中。个别数据没有出现在个别行中......这是我的代码......

第一活动.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

终于完成了...逐字阅读文件...

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 回答
0

检查您的文件并确保每一行后跟 '\n'、'\r'、"\r\n"

于 2013-08-05T11:32:44.990 回答
-1

也许是因为你没有使用好的风格来展示它。您是否尝试过以这种方式使用 ArrayAdapter:

new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, <your_list>)` 
于 2013-08-05T11:03:09.557 回答