Spinner に基づいてリストビューにフィルターをかけたいと思います。
たとえば、私は ListView のコードを持っています
("Singapore Discovery Centre",
"Singapore Science Centre",
"Mint Museum",
"Singapore Art Museum",
"Army Museum").
私はスピナーのコードを持っています
("Centre",
"Singapore",
"Museum").
私がしたいことは次のとおりです。リストビューでシンガポールをクリックすると、すべての「シンガポール」が除外されます
("Singapore Discovery Centre",
"Singapore Science Centre",
"Singapore Art Museum" ).
私は私のこのコーディングを試しました、
教えていただけませんか、何が問題なのですか?
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();
}
}
ログキャット:
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