A little context: I have a stable tutorials app on the market. Recently the users have been requesting some external sources where they can learn more. This question is regarding the solution I came up with.
I have an activity with a listview and a spinner on top.
The listview can be populated with up to 25 elements at once (should I make it lesser?) .... I want the spinner to have options like (1-25, 26-50, 51-75).
The default can be set to 1-25. Depending on what the user selects, I need to re-populate the listview accordingly. I reckon a two dimensional array would be the neatest. But I don't want to hinder the performance too.
Anyways, here's what I've done yet. I need to know how exactly should I update the listview elements in the Spinners OnClick.
Spinner spinner;
String path[] = {"Laptops","DesktopPC","Tablets","Add-Ons","Gaming"};
String Laptops[] = {"Dell","Hp","Apple"};
ListView lstView;
lstView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (Category.this, android.R.layout.simple_spinner_item, path );
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
lstView.setAdapter(lstAdapter);
//lstView.refreshDrawableState();
}
});
Assume that I have to switch between the two arrays depending on the spinner selection.
Thank you for reading! If you require any more information, kindly ask :-)
Related
I want to create an application with two Spinners. When you select one of the items inside the first spinner, the second spinner will get only a few items (depends on which item selected).
For example: in the first spinner I select "Mazda", and then on the second I will be able to see only Mazda's models, not BMW, Ford etc. Can I do something like this?
I tried to create a spinner without items, and set the entries of the spinner on the XML when item selected, but there is no method to do this.
I don't create Lists. I want to create string-array resources in my strings.xml, and give that array to the second spinner.
Of course it is doable and pretty straightforward. Spinners works with the model provider, which in Android it is called Adapter pattern. So what you can do is put in your first spinner an adapter that holds all the brands of your cars, and listen on the first spinner for changes using setOnItemSelectedListener.
When the item changes, you can create a new adapter instance for the second spinner with the only values that are valid in this case.
Try following code. I've organized the sample data in a HashMap but you can do it in your own way.
// hashmap object containing data of spinner1 as 'keys' with relevant
// data of spinner2 in List<String> object as 'values'
final Map<String, List<String>> data = new HashMap<>();
data.put("A", Arrays.asList("1","2","3","4"));
data.put("B", Arrays.asList("4", "5"));
data.put("C", Arrays.asList("6", "7", "8", "9"));
data.put("D", Arrays.asList("10", "11", "12"));
data.put("E", Arrays.asList("13", "14"));
// obtaining a string array containing keys(data of spinner1) of above hashmap
final String[] dataSpinner1 = new String[data.keySet().size()];
data.keySet().toArray(dataSpinner1);
// initializing an string type, ArrayAdapter for spinner1
// you will need to pass activity context, layout for the spinner item and
// spinner content(as string array) as arguments to create an array adapter
final ArrayAdapter<String> spinner1Adapter = new ArrayAdapter<String>(context, R.layout.spinner_layout, dataSpinner1);
spinner1.setAdapter(spinner1Adapter);
// setting listner for spinner1 to trigger when an spinner item is being
// clicked by the user
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// obtaining relevant data for spinner2
List<String> dataSpinner2 = data.get(dataSpinner1[position]);
// crating an setting array adapter for spinner2
ArrayAdapter<String> spinner2Adapter = new ArrayAdapter<String>(context, R.layout.spinner_layout, dataSpinner2);
spinner2.setAdapter(spinner2Adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I want to make a simple application that contains a listview and underneath a subitem to say something more than that button.
In this case big say "Hi" and "Sub Hi" in subitem.
I have read in places that you have to create a separate class but the codes that are difficult to get.
Is there a simple example where you can just walk array (as in my code) for each array, and then combine them to tell you the item?
i want something like this: ( do it with paint)
final String[] test={"Hi"};
String[] subtest={"Sub HI"};
ListView list = (ListView)findViewById(R.id.LV_lista);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, prueba);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, subprueba);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
String NombreHabilidad = prueba[position];
Toast.makeText(getApplicationContext(), "u click "+ NombreHabilidad, Toast.LENGTH_SHORT).show();
}
});
You need to use a ExpandableListView.
Try this tutorial.
I have one ListView and one Spinner. The Spinner is situated at the right top of the layout. My ListView contains some names (eg. country names) and the spinner has some alphabets (a-z). Suppose if I choose the letter "f" from the Spinner, my ListView must show the country names that starts only with the letter "f" . I want to sort my ListView by values from the Spinner?
You need to implement a Filter for your ListView. Everytime the OnItemSelectedListener of your Spinner gets called you need to filter the items. If you're not sure how to implement a filter in your ListView's adapter have a look at this: Filtering ListView with custom (object) adapter
I guess there won't be any way around implementing an own ListAdapter (it's easy).
Try to use the getFilter() method of the ListView adapter:
String[] filterL = { "a", "b", "c" }; //etc
//...
Spinner spin = (Spinner) findViewById(R.id.spinner1);
final ArrayAdapter<String> aspin = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filterL); //the adapter for the Spinner
aspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aspin);
final ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries); //the adapter for the list
setListAdapter(aa); //set the adapter for the list(if you extend LisActivity) or call setAdapter on the ListView element
//add the listener:
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
boolean status = false;
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
if (!status) {
status = true;
return;
}
aa.getFilter().filter(filterL[position]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
I have a question that, I want to load items in a spinner on ItemClick of another Spinner. Actually I two spinners, data is loaded into first spinner from json_parsing and I have to load data in second Spinner after selecting an item from first spinner, So, I don't know how it will implemented? Please suggest me the right solution.
Thanks in advance.
You can do it like this,
First time you data will be loaded in First and Second Spinner.
On Selection of item from First Spinner do this.
1.) Clear the previous ArrayList or Array whateven you have passed the
Second Spinner.
2.) Fill the ArrayList or Array of new data & Update the Second Spinnner using
adapter.notifyDataSetChanged();
second_spinner.setSelection(0);
First Set an OnItemClickListner for your First Spinner. In the OnItemClickListner Method first parse your XML. After completing XML parsing, set parsed data to the adapter and set that adapter with your second spinner
Set an OnItemClickListener on your first spinner that would prepare and set the adapter to the second spinner.
Here is a more complete code example:
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Cursor c1 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c1, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner1.setAdapter(adapter1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Cursor c2 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner2.setAdapter(adapter2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Cursor c_new = (create a new cursor);
adapter2.changeCursor(c_new);
adapter2.notifyDataSetChanged(); // this is important for notifying the UI
spinner2.setAdapter(adapter2);
}
});
What you do is set a listener to the first Spinner, there change the Cursor of the second Adapter to a new one, notify the UI and reset the Adapter of the second Spinner.
So, I have two spinners, let's call the first spinner Parent (account_type_spinner) and the second spinner Child (account_name_spinner). Please notice in the following code the ArrayAdapter initialization for the Child (account_name_spinner), I am feeding it a string array of account names I previously queried before the following lines of code (account_name_array):
//---define spinner objects as variables, assign adapters and listeners---
account_type_spinner = (Spinner) findViewById(R.id.account_type_spinner);
account_type_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_type_array);
account_type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
account_type_spinner.setAdapter(account_type_adapter);
account_type_spinner.setOnItemSelectedListener(new SpinnerSelectionListener());
account_name_spinner = (Spinner) findViewById(R.id.account_name_spinner);
account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array);
account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
account_name_spinner.setAdapter(account_name_adapter);
Selection in the Parent spinner triggers my "SpinnerSelectionListener" which is simply my class implementing OnItemSelectedListener. This class fires obviously each time a selection is made in the Parent spinner and the code looks like the following:
public class SpinnerSelectionListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String spinner_selection = parent.getItemAtPosition(pos).toString();
if(spinner_selection.contentEquals(INCOME)) {
//---grab Income type accounts from db and build array---
db.open();
account_name_array = db.getAccounts(INCOME);
account_name_adapter.notifyDataSetChanged();
dr_amount_textview.setVisibility(View.GONE);
dr_amount.setVisibility(View.GONE);
cr_amount_textview.setVisibility(View.VISIBLE);
cr_amount.setVisibility(View.VISIBLE);
db.close();
} else {
//---grab Expense type accounts from db and build array---
db.open();
account_name_array = db.getAccounts(EXPENSE);
account_name_adapter.notifyDataSetChanged();
cr_amount_textview.setVisibility(View.GONE);
cr_amount.setVisibility(View.GONE);
dr_amount_textview.setVisibility(View.VISIBLE);
dr_amount.setVisibility(View.VISIBLE);
db.close();
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
The code above should update the Child (account_name_spinner) with INCOME or EXPENSE accounts whenever the Parent's selection changes from INCOME to EXPENSE or vice versa (this is an accounting app). The updating of the Child's spinner list should be facilitated by the "account_name_adapter.notifyDataSetChanged();" however nothing is happening.
I looked into this issue further on StackOverflow and found that I must .clear() or .remove() items from my Child's ArrayAdapter (account_name_adapter) before the list will update, however, when I try "account_name_adapter.clear();" before notifying the ArrayAdapter I get an error that the operation is illegal. Any ideas what I am doing wrong?
You can just reassign the adapter after you got the updated array (account_name_array)
account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array);
account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
account_name_spinner.setAdapter(account_name_adapter);
EDIT: To the adapter you don't pass a reference, so when you update the array the adapter still have the same data. Calling .notifyDataSetChanged() do not update the adapter with the new array.