我有一个包含搜索栏和搜索活动的主要活动。当我在搜索栏中输入一个字符串并点击return时,将调用search-activity。我试图从搜索活动更新main-activity中的列表视图,但这不起作用。怎么了? THX
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();
}
}
答案 0 :(得分:1)
我确定这个选项不是最好的方法,但我是如何解决它的。
尝试使用您要显示的值保留MainActivity中的属性(public static List
)。
现在,每当活动到达前台时设置你的apadter:
@Override
public void onResume(){
adapter = new ExpandListAdapter(MainActivity.this, valueList);
lv.setAdapter(adapter);
super.onResume();
}
所以你只需要从SearchableActivity编辑你的valueList,ListView就会自动更新。
如果您不喜欢公共属性,则可以实现包含Protected static List
的superActivity。这两项活动都应该从超级继承。
但无论如何,你不能直接从MainActivity内部处理意图吗?
答案 1 :(得分:0)
试试这段代码
public void doMySearch(String query)
{
List<String> valueList_new = new ArrayList<String>();
for (int i = 0; i < MainActivity.valueList.size(); i++) {
if((valueList.get(i).contains(query)))
{
valueList_new.add(valueList.get(i));
}
}
}
答案 2 :(得分:0)
您无法通过新的其他适配器更新列表视图。
ListView只有1个适配器,这是第一次为listview设置dapter
要更新列表视图,您需要更新适配器的数据。
参考:Update ListView items from another class