我有一個包含搜索欄和搜索活動的主要活動。當我在搜索欄中輸入一個字符串並按回車時,搜索活動被調用。我正在嘗試從search-activity更新main-activity中的listview,但這不起作用。怎麼了? THX無法更新其他活動的ListView
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();
}
}
什麼不工作? – Szymon
我希望doMySearch用valueList_new的內容填充listview。但列表視圖仍然是相同的:( – NoobieNoob
如果u使用的另一項活動只是爲了更新主要活動,那麼爲什麼不ü使用onActivityResult –