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.
Related
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.
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
I'm trying to decrease the font size of the spinner, and I created an XML file called spinner_item.
spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#800080" />
method to load the spinner:
private void loadCustomServiceSpinner(String workRequestType) {
CustomServiceDBQueries csQueries = new CustomServiceDBQueries();
customServices = csQueries.selectCustomService(workRequestType);
String[] strCustomService = new String[customServices.size() + 1];
strCustomService[0] = "";
int i = 1;
for (CustomService cs : customServices) {
strCustomService[i] = cs.getCustomServiceName();
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.spinner_item, strCustomService);
adapter.setDropDownViewResource(R.layout.spinner_drop_default);
Spinner SpnCustomService = (Spinner) findViewById(R.id.SpnCustomService);
SpnCustomService.setAdapter(adapter);
}
I do not see any changes, the source may be 20 or 50 SP SP that does not change anything. Can someone help me?
I want to add AutoCompleteTextView in my application. I have one txt file in that there are more than 2000 records are present. I want to use it for AutoCompleteTextView. Normally for small data we use array as:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,dataArray);
AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
actv.setThreshold(1);
actv.setAdapter(adapter);
But now how to use txt file for AutoCompleteTextView. Any suggestion will be appreciated.
How are you separating elements in your text file? Assuming that you have a new elements on each line you can use this to convert the file to an array, the use the array adapter as you have mentioned above.
String[] arr= null;
List<String> items= new ArrayList<String>();
try
{
FileInputStream fstream = new FileInputStream("text1.txt");
DataInputStream data_input = new DataInputStream(fstream);
BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input));
String str_line;
while ((str_line = buffer.readLine()) != null)
{
str_line = str_line.trim();
if ((str_line.length()!=0))
{
items.add(str_line);
}
}
arr = (String[])items.toArray(new String[items.size()]);
}
create one string resource file like "string_autocompletearray" and define your array.
**res/values/string_autocompletearray.xml**
string-array name="autocomplete_array">
<item>AutoCompleteText 1 </item>
<item>AutoCompleteText 2 </item>
-------------------------------
-------------------------------
<item>AutoCompleteText N </item>
</string-array>
**Now find this array and set to adapter**
ArrayList<String> list = new ArrayList<String> (Arrays.asList(getResources().getStringArray(R.array.autocomplete_array)));
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