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.
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 working on an Activity where I need to start a new Activity at the click of a button based on the selected item in a spinner. I can't quite figure it out. This is what I have after clearing failed attempts.
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.user_type, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinner.equals("My String Here")){
startActivity(new Intent(Register.this, *****.class));
}else{
startActivity(new Intent(Register.this, *****.class));
}}
Am I better off just installing two buttons with their own proper syntax starting new activities separately?
If spinner.equals does not make any sense at all.
You should use position integer value to check your array data and then make a decision based on that value
First things first.
spinner.equals("My String Here")
spinner is of type Spinner whereas "My String Here" is a String. You are always sure that they will never be equated the same.
Next, try to make use of your position and adapter to be able to properly construct your condition.
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
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);
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.