我想根据 Spinner 过滤到列表视图中。
例如,我有一个 ListView 的代码,它是
("Singapore Discovery Centre",
"Singapore Science Centre",
"Mint Museum",
"Singapore Art Museum",
"Army Museum").
我有一个 Spinner 的代码
("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