我有一个包含搜索栏和搜索活动的主要活动。当我在搜索栏中输入一个字符串并按回车键时,就会调用搜索活动。我正在尝试从搜索活动更新主活动中的列表视图,但这不起作用。怎么了?谢谢
public class MainActivity extends Activity {
static List<String> valueList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
valueList = new ArrayList<String>();
MainActivity.valueList.add("eins");
MainActivity.valueList.add("zwei");
MainActivity.valueList.add("drei");
MainActivity.valueList.add("vier");
MainActivity.valueList.add("fünf");
MainActivity.valueList.add("sechs");
ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, MainActivity.valueList);
final ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{ }
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
}
public class SearchableActivity extends Activity {
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
android.util.Log.v("my_search", "onCreate()");
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
android.util.Log.v("my_search", "onNewIntent()");
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
android.util.Log.v("my_search", "handleIntent()");
android.util.Log.v("my_search", "Query: "+query);
TextView tv;
tv=(TextView) findViewById(R.id.textview_searchable);
tv.setText("Suche nach: "+query);
doMySearch(query);
}
}
public void doMySearch(String query)
{
List<String> valueList_new = new ArrayList<String>();
for(String s : MainActivity.valueList) {
if(s.contains(query)) {
android.util.Log.v("my_search", "gefunden: "+s);
valueList_new.add(s);
}
}
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, valueList_new);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
finish();
}
}