I would like to filter into listview based on Spinner.
For example, I've a code for ListView which is
("Singapore Discovery Centre",
"Singapore Science Centre",
"Mint Museum",
"Singapore Art Museum",
"Army Museum").
I've a code for Spinner which is
("Centre",
"Singapore",
"Museum").
What I would like to have is: Upon clicking Singapore, in the listview, it will filter out all the "Singapore"
("Singapore Discovery Centre",
"Singapore Science Centre",
"Singapore Art Museum" ).
I tried out this coding of mine,
Could you be guide me, what is wrong with it?
MainActivity.java
public abstract class MainActivity extends ListActivity implements OnItemSelectedListener {
String[] Category = {
"Singapore discovery Centre",
"Singapore Science Centre",
"Mint Museum",
"Singapore Art Museum",
"Army Museum"
};
String [] keywords = {
"Centre",
"Musuem",
"Singapore",
};
Spinner s1;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listView1);
s1 = (Spinner) findViewById(R.id.spinner1);
s1.setOnItemSelectedListener(this);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category));
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, keywords);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
listview.setFilterText(Category[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
listview.setFilterText("");
}
public void onListItemClick(ListView parent, View v, int position,long id)
{
Toast.makeText(this, "You have selected " + Category[position], Toast.LENGTH_SHORT).show();
}
}
Logcat:
07-29 14:43:30.429: E/AndroidRuntime(30599): FATAL EXCEPTION: main
07-29 14:43:30.429: E/AndroidRuntime(30599): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.spinnertest/com.example.spinnertest.MainActivity}: java.lang.InstantiationException: com.example.spinnertest.MainActivity
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.os.Looper.loop(Looper.java:130)
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.app.ActivityThread.main(ActivityThread.java:3691)
07-29 14:43:30.429: E/AndroidRuntime(30599): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 14:43:30.429: E/AndroidRuntime(30599): at java.lang.reflect.Method.invoke(Method.java:507)
07-29 14:43:30.429: E/AndroidRuntime(30599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
07-29 14:43:30.429: E/AndroidRuntime(30599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
07-29 14:43:30.429: E/AndroidRuntime(30599): at dalvik.system.NativeStart.main(Native Method)
07-29 14:43:30.429: E/AndroidRuntime(30599): Caused by: java.lang.InstantiationException: com.example.spinnertest.MainActivity
07-29 14:43:30.429: E/AndroidRuntime(30599): at java.lang.Class.newInstanceImpl(Native Method)
07-29 14:43:30.429: E/AndroidRuntime(30599): at java.lang.Class.newInstance(Class.java:1409)
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.app.Instrumentation.newActivity(Instrumentation.java:1024)
07-29 14:43:30.429: E/AndroidRuntime(30599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
07-29 14:43:30.429: E/AndroidRuntime(30599): ... 11 more