how to fill the data into spinner from textfile in android - 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

Related

Android how to trim sharedpreferences name

I'm getting all sharedpreferences created by my app and I insert them in a spinner but every sharedpreferences displayed like "example.xml" etc. I want to trim .xml extension let them just names like "example".
File prefsdir = new File(getApplicationInfo().dataDir,"shared_prefs");
if(prefsdir.exists() && prefsdir.isDirectory()){
String[] list = prefsdir.list();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1,list);
Spinner sp = (Spinner) findViewById(R.id.tum_ilaclar);
sp.setAdapter(adapter);
}
You can use something like this create a new method:
String[] getList(String[] list){
String values[] = new String[list.length];
for(int i=0; i<list.length; i++){
String value = list[i];
values[i]=value.contains(".xml")?value.replace(".xml",""):value;
}
return values;
}
Then in your code change :
String[] list = prefsdir.list();
to
String[] list = getList(prefsdir.list());
This will remove the .xml part.

How to add a default selected value in android dropdown

I have a dropdown which takes its values from a database, I then want to add a default value to the existing drop down values. In this case the default value would be the selected value.
StringBuilder strbuilder = new StringBuilder();
ArrayList<String> arr_list = new ArrayList<String>();
List<String> m = new ArrayList<String>();
//This get the data values from database
m = db.getData();
int ms = m.size();
String str = strbuilder.toString();
String[] str1 = str.split(",");
try {
if (ms > 1) {
//This is my default value
m.add("Default Value");
for (int i = 0; i < ms; i++) {
m.get(0);
}
} else {
m.get(0);
}
} catch (Exception e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, m);
dropdown.setAdapter(adapter);
The codes above is able to add the values from the database to the dropdown and the Default Value is added but it doesn't appear to be the selected value.
For instance if the values from the database are "Apple, Mango,Pineapple" and the default value is "ALL FRUITS".
EXPECTED DROPDOWN
ALL FRUITS
Apple
Mango
Pineapple
Please how can I do make the default value the selected value? Thanks in advance.
I think you need below code.
List<String> categories = new ArrayList<String>();
categories = db.getData();
categories.add(0,"your_default_value");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setSelection(0);
Remember when you want to add any value to the very first of Arraylist you just do
arraylist.add(0,"value");
This will add the value to the very first index of arraylist and push all other element down.
In case you need any other element to be selected not first one
Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
int i=categories.indexOf("Education");
spinner.setSelection(i);
If the default value is always going to be at the 0 index, just get a reference to your dropdown/spinner, then set:
spinner.setSelection(0);
And here is a previously answered question detailing how to set it by value, if you'd rather do that.
https://stackoverflow.com/a/4228121/3299157

How to set string from sqlite to Spinner in Android?

I have Spinner in my app and set String[] programmatic.
speciality = (Spinner) findViewById(R.id.general_specality_s);
specialityadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, speciality_list);
speciality.setAdapter(specialityadapter);
and get selected string like this speciality.getSelectedItem().toString(); and store in sqlite. Now I want to get that value from sqlite and need to set in Spinner1.
String SpinnerText=c.getString(c.getColumnIndex("speciality"));
How to set this string to speciality / Spinner ? Thanks in advance.
Fetch your data into an array and use an array adapter
String[] spinnerArray = your data goes here;
Spinner spinner = (Spinner)findViewById(R.id.yourspinnerid);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);

Using Multiple Spinners, But Only One Spinner Has All The Data

I have two spinners and two arrays. However, one spinner receives both arrays while the other receives no values from either of the two arrays. Note: I do not want to use radio buttons as the data is shortened for review.
final ArrayList<String> serialnums = new ArrayList<String>();
serialnums.add("576798");
serialnums.add("495874");
serialnums.add("345667");
serialnums.add("956345");
final ArrayList<String> carrys = new ArrayList<String>();
serialnums.add("R");
serialnums.add("L");
serialnums.add("F");
serialnums.add("B");
s1 = (Spinner) findViewById(R.id.spinnerSerial);
spinnerCarry = (Spinner) findViewById(R.id.spinnerCarry);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, serialnums );
ArrayAdapter<String> adapterCarrys = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, carrys );
s1.setAdapter(adapter);
spinnerCarry.setAdapter(adapterCarrys);
Note 2: Spinner s1 gets all the data
One spinner has all the data because you are assigning all the data to it. Change this:
final ArrayList<String> carrys = new ArrayList<String>();
serialnums.add("R");
serialnums.add("L");
serialnums.add("F");
serialnums.add("B");
to this:
final ArrayList<String> carrys = new ArrayList<String>();
carrys.add("R");
carrys.add("L");
carrys.add("F");
carrys.add("B");
You are adding "R" "L" "F" "B" to serialnums add it to carrys

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
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.

Categories

Resources