问题
I would like to ask a very general question. " How to filter based on spinner?" Meaning, there's few option in spinner ("education", "musuem", "restaurant") Upon selecting "museum" it will show me a list of musuem. Is there such a way to do it? I've those data retrieved, just that, I would like to know whether spinner can do this function.
I've google it but doesn't seems to find the answer that I wanted thus, would like to seek for advice whether is there any such sources.
CHANGES IN QUESTION, SOMEHOW SIMILAR
If I've this coding, and wanted to retrieve either "Museum", "Singapore", or "Centre", How should I edit in my code? Meaning, upon click on the selection in spinner, it will change.
MainActivity.java public class MainActivity extends ListActivity {
String[] Category = {
"Singapore discovery Centre",
"Singapore Science Centre",
"Mint Museum",
"Singapore Art Museum",
"Army Museum"
};
String [] keywords = {
"Centre",
"Musuem",
"Singapore",
};
Spinner s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GridView
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category));
//SpinnerView
s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, keywords);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
int index = s1.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You have seleted item :" + keywords[index] , Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?>arg0) {}
});
}
public void onListItemClick(ListView parent, View v, int position,long id)
{
Toast.makeText(this, "You have selected " + Category[position], Toast.LENGTH_SHORT).show();
}
}
回答1:
first of all you should implement OnItemSelectedListener
.
Spinner sp;
String[] strarr={"education", "musuem", "restaurant"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(filename.this,android.R.layout.simple_spinner_item,strarr);
sp=(Spinner) findViewById(R.id.spinner1);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(this);
you should override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position =sp.getSelectedItemPosition();
//things to do
}
and
public void onNothingSelected(AdapterView<?> arg0) {
//things to do
}
回答2:
Yes there is a Listener named OnItemSelectedListener which tells you which item selected in the spinner so you can find the selected item id, then filter your adapter or whatever data structure you have with that selected Item.
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
})
});
来源:https://stackoverflow.com/questions/17906019/does-android-have-this-function-how-to-filter-based-on-spinner