Numair
Numair

Reputation: 1062

load spinner from text file android

i'm working on a project to fill spinner from text file in assets or sdcard. My code is

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);

and main.xml

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

Can anyone please help me to solve this issue? Thanks in advance

Upvotes: 1

Views: 4041

Answers (1)

Ant4res
Ant4res

Reputation: 1225

If your file is in the assets folder of you project, I think you have to do:

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);

Then you have to right click on the assets directory in Eclipse then choose Build Path -> Use as Source Folder.

Upvotes: 3

Related Questions