1

我正在做一个项目来从资产或 sdcard 中的文本文件中填充微调器。我的代码是

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"
 />

谁能帮我解决这个问题?提前致谢

4

1 回答 1

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 中的资产目录,然后选择 Build Path -> Use as Source Folder。

于 2012-04-17T08:51:54.947 回答