从文本文件android加载微调器

时间:2012-04-17 06:56:39

标签: android spinner text-files fill

我正在开发一个项目,用于从资产或SD卡中的文本文件中填充微调器。我的代码是

BufferedReader in = new BufferedReader(new FileReader("product.txt"));

        String line = in.readLine();
        int index = 0;
        while (line != null) {

            str[index++] = line;
            line = in.readLine();
        }

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_spinner_item, str);

        spinner.setAdapter(adapter);

和main.xml

<Spinner
       android:id="@+id/spinner"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
 />

有人可以帮我解决这个问题吗?提前致谢

1 个答案:

答案 0 :(得分:3)

如果您的文件位于项目的资产文件夹中,我认为您必须这样做:

Vector<String>str=new Vector<String>();
BufferedReader in = new BufferedReader(new InputStreamReader(getAssets().open("product.txt"));

    String line = in.readLine();
    int index = 0;
    while (line != null) {

        str.add(line);
        line = in.readLine();
    }

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter adapter = new ArrayAdapter(this,
            android.R.layout.simple_spinner_item, str);

    spinner.setAdapter(adapter);

然后你必须右键单击Eclipse中的assets目录,然后选择Build Path - &gt;用作源文件夹。