简体   繁体   中英

how to fill the data into spinner from textfile in android

I am working on android offline data management. I am creating a text file where all the data of the main server will be store and after that I read it.

How can I fill textfile to a Spinner ?

        String[] str = new String[totalLine];


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

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

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

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

        spinner.setAdapter(adapter);

Edit:

From your previous comment what i got is your data is in Mumbar.Noida format

Do this

 String[] str = response.toString().split(","); 

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

            spinner.setAdapter(adapter);

this will populate spinner like

Mumbai
Noida

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM