hi i have to developed one app..here i have to updated my spinner value in my mysql database..
for eg:....here i have to run the app first time always display Q only..here i have to update the status Q to C after i have to run the app means automatically displayed the status C....how can i develop this.why always displayed Q...how can i manage the code here.
here i have used below code.
private void createSpinnerDropDown() {
//get reference to the spinner from the XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
//Array list of animals to display in the spinner
List<String> list = new ArrayList<String>();
list.add("Q");
list.add("P");
list.add("F");
list.add("I");
list.add("C");
//create an ArrayAdaptar from the String Array
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
//set the view for the Drop down list
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//set the ArrayAdapter to the spinner
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
//attach the listener to the spinner
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
how can i develop this.
if(list.contains(DATABASE.FETCH()))
spinner.setSelection(list.indexOf(DATABASE.FETCH()));
the DATABASE.FETCH() is your method of fetching the data (C) from the database.
i don't know it will exactly you want .how i managed this,instead of save the selected item get only the selectedItemPosition
int selectedIndex = spinner.getSelectedItemPosition();
and when you want to set the spinner item from data base, just fetch the index, let say
int SavedIndex = FetchSavedSpinnerIndex();
now set your spinner's item as
spinner.setSelection(SavedIndex);
Related
I have one spinner that populates with my SQL data. This works well. I now want to make it into a dependant spinner where the spinner I already have becomes the second or dependant spinner. I have added another spinner in my MainActivity.java. and it sucessfully retrieves the required mySQL data
#Override
protected void onPostExecute(Void args) {
// Locate spinner1 in activity_main.xml
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
// Spinner adapter
List<String> typesofjobsunique = new ArrayList<>(new LinkedHashSet<>(typesofjobs));
spinner1.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item, typesofjobsunique));
Collections.sort(typesofjobsunique);
// Locate my spinner in activity_main.xml
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
// Spinner adapter
mySpinner
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
worldlist));
Collections.sort(worldlist);
What is the simplest coding approach to migrate from a single to a dependant spinner given the data retrieval itself works well for both spinners and I just want to make it dependant from the current independant format so
spinner1 typesofjobsunique only displays the
mySpinner worldlist
spinner choices from mySQL corresponding to that particular typeofjobunique.
Most of the questions on here relating to dependant spinners, are where the data is in strings.xml which is not very helpful with remote data.
I'm assuming I will need to split my one adapter into two separate adapters, but I am unclear how to proceeed.
From the way that Spinners work, if you want to implement dependence between them, you will need to use the current selection of one (spinner1) as the key for the list source of the other (mySpinner). You should implement an AdapterView.OnItemSelectedListener on spinner1 as recommended in the Spinner guide and set the list in mySpinner. See code below:
// Locate spinner1 in activity_main.xml
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
// Locate mySpinner in activity_main.xml
Spinner mySpinner = (Spinner) findViewById(R.id.my_spinner);
// Spinner adapter
List<String> typesofjobsunique = new ArrayList<>(new LinkedHashSet<>(typesofjobs));
spinner1.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item, typesofjobsunique));
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// **EDIT**
List<String> myWorldList = getWorldListForItem(parent.getItemAtPosition(pos));
Collections.sort(myWorldList);
mySpinner
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
myWorldList));
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
Collections.sort(typesofjobsunique);
The implementation for getWorldListForItem() should query your database in the desired way. A hash table could also be used.
Hope that this is useful.
EDIT
Based on your comments, the implementation for getWordListForItem() would look something like this:
List<String> getWordListForItem(String s) {
String json = queryMySQLDatabase(s);
List<String> results = parseJSONintoList(json);
return results;
}
Also, the AdapterView.getItemAtPosition(int pos) function returns a Java object of the type passed into the ArrayAdapter (which is a String in this case). Therefore getWordListForItem() takes a String argument.
As for the "Cannot resolve contructor ArrayAdaptor" error, please check the spelling (it's supposed to be ArrayAdapter not ArrayAdaptor). See ArrayAdapter documentation for more details... I've also fixed my implementation above to sort the list properly.
I'm creating application for the spinner. I have two activities in my application which is Spinner in second Activity. I'm trying to pass the string value from one Activity to another Activity and that string value put it into Spinner in another Activity.
I tried but I can't find proper solution.
Here is my spinner code
private String[] models = { " 207"," 308CC"," 308SW"," 508"," 3008"," 5008"," 308", " RCZ R"," 5008", " 207CC"};
Intent intent = getIntent();
modelString = intent.getExtras().getString("MODEL");
spinnerModels = (Spinner)findViewById(R.id.Model_spinnerTestDrive);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, models);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerModels.setAdapter(adapter_state);
int spinnerPosition = adapter_state.getPosition(spinnerModels);
spinnerModels.setSelection(spinnerPosition);
I have already put some value into spinner. If I set this value it should be displayed in spinner.
I'm not sure if you mean you want to make the String the current selection in the 2nd Activity's Spinner or if you want to add the String to the available options in the Spinner. Either way, I think you have to access the ArrayAdapter directly. See this answer and it might help you.
I'm trying to build and Android app:
How can I build a list on screen from an array and have the list be clickable?
with a value of the name to be able to use later stored in a String.
What I have now.
String game_list[] = {"game_0","game_2","game_3","game_3","game_4","game_5"};
for(int i=0; i<game_list.length; i++){
myText = myText + game_list[i] +"\n";
}
myTextView.setText("");
myTextView.append(myText);
Also you can use a spinner and adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, game_list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Here's a super simple example that I have used: http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html.
You basically need to make a listview object and add it to whatever view you happen to be using. Then you add "extends ListActivity" to the activity and add your list to the listactivity like so:
ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this,R.layout.file_row, this.directoryEntries);
ListView fileList = (ListView) findViewById(android.R.id.list);
fileList.setAdapter(directoryList);
Call:
protected void onListItemClick(ListView l, View v, int position, long id)
to associate actions with your listview elements
You can build that with ListView control in android and here very good tutorial about it to get more understand how to build it with the array or from any other data source like from database
http://www.vogella.com/tutorials/AndroidListView/article.html
feed be back in any thing not obvious for you
I am working on a project in which I need to dynamically add TextView and Spinner as well. I was able to add these two things dynamically from my program.
Now when I was trying to select some items in the Spinner, it was not selecting those items.
Does I need to do anything to make that item selected in Spinner?
for (Map.Entry<String, String> entry : mapColumns.entrySet()) {
spinnerArray = new ArrayList<String>();
final TextView rowTextView = new TextView(cont);
final Spinner spinner = new Spinner(cont);
rowTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
spinner.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
for(String s: entry.getValue().split(",")) {
System.out.println(s);
s = s.replaceAll("[^a-zA-Z0-9]+","");
spinnerArray.add(s);
}
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(cont, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
rowTextView.setText(entry.getKey());
rowTextView.setTypeface(null, Typeface.BOLD);
spinner.setAdapter(spinnerArrayAdapter);
layout.addView(rowTextView);
layout.addView(spinner);
}
Here mapColumns will hev Key-Value pair. So in the Spinner all the items are getting shown from the Value of that map.
Problem Statement:-
Now I need to make sure if anybody is selecting any items in the Spinner, it should get selected and visible to naked eye.
How can I do that based on my code. Thanks for the help.
Below is the image-
Try use this code :
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(con,
android.R.layout.simple_spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Add an OnItemSelectedListener to your spinner:
spinner.setAdapter(spinnerArrayAdapter);
// add the listener
spinner.setOnItemSelectedListener(this);
Then, implement the listener in your activity:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
Can anybody help me? I'm trying to create a ListView in Android, and I'm trying to load items into it using code (not using XML)
Here's the code that I have so far
tweetList = (ListView)this.findViewById(R.id.tweetListView);
TextView tv;
for(int i=0;i<20;i++)
{
tv = new TextView(this);
tv.setText("I'm a textView");
tweetList.addHeaderView(tv);
}
tweetList.invalidate();
What am I doing wrong? The items are not showing in runtime
EDIT: I changed the code as per the answers below and here's the code I have now
tweetList = (ListView)this.findViewById(R.id.tweetListView);
ArrayAdapter<TextView> aa = new ArrayAdapter<TextView>(this, R.id.tweetListView);
tweetList.setAdapter(aa);
TextView tv;
for(int i=0;i<20;i++)
{
tv = new TextView(this);
tv.setText("I'm a textView");
//tweetList.addHeaderView(tv);
aa.add(tv);
}
tweetList.invalidate();
I'm getting an exception now
11-10 01:32:16.002: ERROR/AndroidRuntime(867): android.content.res.Resources$NotFoundException: Resource ID #0x7f050030 type #0x12 is not valid
Why am I not able to add them dynamically now?
You need to use ArrayAdapter and add it to the ListView. Then just add elements to the ArrayAdapter dynamically.
For example:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
tweetList.setAdapter(arrayAdapter);
// ...
arrayAdapter.add("New Item");
The header view is just a header, its not the items in the list. Your listview receives its contents through an Adapter. The ListView does not keep Views for its children, but its the adaptor who creates them and fills them with the appropiate data when they need to be shown at the screen. Search for basic tutorials on how to do this, there are lots out there; and remember adapters are the key.