I have a Spinner object that contains an array of strings populated with the 50 states in the United States.
I also have some more arrays that contain lists of cities for the different states. What I am wanting to do is to populate the dependent spinner with the array of cities from the state that was selected by the other spinner.
For exampe:
First spinner selected - Alaska
Second spinner - Anchorage, Fairbanks, Ketchikan, Kodiak.
Another example:
First spinner selected - Florida
Second spinner - Key West, Tallahassee.
The problem is that I'm not sure how to populate the dependent spinner based on the state spinner selected.
The Array Adapter that I am using accepts an integer value that references the array that is used to populate the spinner.
I don't know the exact API calls you're using, but here's the general approach I'd use:
class CitySpinnerFactory {
Map<String,Spinner> stateToCitySpinner = new HashMap<String,Spinner>();
public void map(String state, Spinner citiesForState) {
stateToCitySpinner.put(state,citiesForState);
}
public Spinner spinnerForState(String state) {
return stateToCitySpinner.get(state);
}
}
And later you might do this to populate the cities and states
Spinner states = new Spinner();
Spinner cities = null;
CitySpinnerFactory cityFactory = new CitySpinnerFactory();
states.add("Alabama");
cities = new Spinner();
cities.add("Birmingham"); // city in AL
cities.add("Mobile"); // city in AL
cityFactory.map("Alabama",cities);
states.add("Alaska");
cities = new Spinner();
cities.add("Juneau"); // city in AK
cities.add("Anchorage");
cityFactory.map("Alaska",cities);
// ... add other states
And later you might do this to get the cities out
String state = states.spin(); // making up method to get a random state...
Spinner cities = cityFactory.spinnerForState(state);
String city = cities.spin();
Related
Users location is already saved in database. Here, Im retrieving state and city from database, then saved in strings. That is ok. Now, how can I set state string value in a spinner and then city string value in a 2nd spinner?
String state = "prov";
String city = "cityy";
When I try
ArrayAdapter<String> adaptersta = new ArrayAdapter<String>(uploadpostj.this, android.R.layout.simple_spinner_item, Collections.singletonList(state));
spinnerstate.setAdapter(adaptersta);
and
ArrayAdapter<String> adaptercityy = new ArrayAdapter<String>(uploadpostj.this, android.R.layout.simple_spinner_item, Collections.singletonList(city));
spinnercity.setAdapter(adaptercityy);
Then if I just try to set only city value, then it's ok, or try to set only state value, again ok. However, when I try to set both values, then only state value is set and city is empty. How can I set both string values in both spinners?
After state is selected by user, you should add cities of that state to arraylist of that state and show it in city spinner.
So I have arraylist modelData that populates a recyclerview using sqlite database in some activity.!
Now in my MainActivity I want an string arraylist of names from the modelData !
that's what did so far..
// inside the onCreate of MainActivity
//code ..
db = new DBHandler(this, null, null, 1);
modelData = new ArrayList<AzkarModel>();
modelData = db.getDataFromDB();
for (AzkarModel o : modelData) {
ArrayList<String> names = new ArrayList<>();
names.add(o.getName());
}
for (int i = 0; i< modelData.size();i++){
names.add(modelData.get(i).getName());
Log.i("The List Log", names);
}
Two problems
1) [FIXED] The names arraylist is showing the same element twice at first and end
I/ The List Log: [Mike, John, Sam, Nora, Mike]
2) The arraylist names doesn't get updated..! when I add/edit/delete from the recycler and go back to the MainActivity I don't see the new changes unless I close the app then open it again..! I can't use notifyDataSetChanged since there's no adapter here.!
Once you have an array list object, populate it inside a loop
ArrayList<String> modelNames = new ArrayList<>();
for (AzkarModel model : modelData) {
modelNames.add(model.getName());
}
Now you have an array list with model names inside. Concerning the second question, please use an recyclerview adapter here you can find a nice tutorial.
I fill a spinner via json from a webservice. The spinner gets populated, a selection of index 0 is set, the onitemselected method fires and launches another method to populate a list view based on the value in the spinner.
The problem appears when I refill the spinner with different data. Spinner gets filled, selection of index 0 is set, onitemselected fires and when I output the selected item it shows the previous item at the 0 index. spinneritems.get grabs the old data rather than the newly filled data.
Here is filling the spinner
for (int i = 0; i < recordsArray.length(); i++) {
JSONObject record = recordsArray.getJSONObject(i);
Map<String, String> datum = new HashMap<String, String>(2);
datum.put("code", record.getString("id") + " - " + record.getString("heading"));
datum.put("description", record.getString("body"));
spinneritems.add(datum);
adapter.notifyDataSetChanged();
spinner.setSelection(0);
}
it leads to the firing of
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
populateList(page);
}
which then calls a method with
final Spinner spinner = (Spinner)findViewById(R.id.comSpinner);
String spinnerItem = spinneritems.get(spinner.getSelectedItemPosition()).get("code");
It all works fine the first time it runs, but when I perform an action that changes the spinner contents the method at the end still grabs the old data for some reason
Solved it. Before setting the adapter initially I do the following
final Spinner spinner = (Spinner) findViewById(R.id.comSpinner);
spinneritems.clear();
adapter.notifyDataSetChanged();
spinner.setAdapter(adapter);
I have an ArrayList<Model> mList
and a Model class that looks like
public class Model{
String value;
String name;
String dob;
//getters & setters
}
I have view pager which has 5 fragments. mList holds the full list, ie value = All. which I am showing in the 1st fragment. I want to filter/sort for the other fragments. For instance, 2nd should have only value = k ,for 3rd value = B... what is the best way to get a sorted list and pass list.. for now I am doing
ArrayList<Model> newlist = new ArrayList<Model>();
for{int i =0 ;i> mlist.size();i++}{
if(mList.getvalue.equal("k")){
newList.add(mList(i))
}
}
and passing newlist to listview
but now how can I update the value in mList when the user clicks on one row because that row will have the value newList position...
Basically I have some condition for mList say user have to click one item for k,b etc that I want to check.
1) What is a good way to get sorted/filter list in different fragments?
2) How can I update the newlist value of the item clicked in mList?
Hi I am just looking for some advice on how to approach adding 2 seperate edittext values to a hash map.
Basically, I have the name of an item in one edittext, and the price of the item in the next one.
I want to click add and for both values to be stored together.
My aim is to add these details to a spinner and when its selected I can show the value that is associated with it.
I am just looking for some direction if that is possible, I am able to add one hash map entry to a spinner but I am having trouble adding two connected values at once
Why not creating a new class Item in which you put the two attributes the name of the item and it price, and then create a hash map of this class.
Your class should look like this:
public class Item{
private String itemName;
private String itemPrice;//or put an integer instead.
public Item (String n, String p){
this.itemName = n;
this.itemPrice =p;
}
EditText itemNameInput, itemPriceInput;
HashMap<Integer, String> inputMap = new HashMap<>();
public void onAddClicked() {
String itemName = itemNameInput.getText().toString();
String itemPrice = itemPriceInput.getText().toString();
inputMap.put(itemNameInput.getId(), itemName);
inputMap.put(itemPriceInput.getId(), itemPrice);
// iterate over hashmap -> arrayadapter backing spinner
}
But why do you need a hashmap? You want to be able to lookup the EditTexts after the spinner is clicked? You're probably better off just concatenating the name and price (String spinnerItemOne = itemName + " " + itemPrice;) then feeding that to your spinner's array adapter. You can use the hashmap if you need, for instance, to send focus back to the itemNameInput. I just used the id since I don't know more about what you're trying to do: you can use the actual edit texts as keys as well (just don't leak them). On a final note, that it matters to much if you are only doing this pair, but you might use a SparseArray instead of the hashmap for more efficiency.