我有一个包含搜索视图的操作栏。当我想通过单击搜索按钮从列表视图中搜索某些内容时,操作栏会在左侧显示一个后退按钮。如果我在获取搜索列表后单击此后退按钮,搜索视图和后退按钮消失但列表视图仅显示搜索列表而非原始列表。
public class ActivityThree extends AppCompatActivity{
FloatingActionButton fab;
DBHelper myDb;
ArrayList<AccountDetails> rList;
ListView listView;
RemainderAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
fab = (FloatingActionButton) findViewById(R.id.fab);
listView = (ListView) findViewById(R.id.listView);
myDb = new DBHelper(this);
showViews();
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(ActivityThree.this, ActivityFour.class));
}
});
}
private void showViews() {
rList = new ArrayList<AccountDetails>();
rList = myDb.getAllRows();
if (rList.size() != 0) {
adapter = new RemainderAdapter(this, rList);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
} else {
Toast.makeText(this,"There is no item in the list",Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_item,menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
adapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
adapter.getFilter().filter(query);
invalidateOptionsMenu();
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==R.id.changePwd){
startActivity(new Intent(this, PwdChange.class));
return true;
}
if(id==R.id.about){
startActivity(new Intent(this, ActivityAbout.class));
return true;
}if(id==R.id.share){
try
{ Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "ABCD");
String sAux = "\nLet me recommend you this application\n\n";
i.putExtra(Intent.EXTRA_TEXT, sAux);
startActivity(Intent.createChooser(i, "Choose any one"));
}
catch(Exception e)
{
}
}
if(id==R.id.exit){
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
这是在点击searchView's
后退按钮时显示祝酒词的工作代码。
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu_item,menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
MenuItem searchItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(MainActivity.this," Back Button Clicked",Toast.LENGTH_LONG).show();
showViews();
return true;
}
});
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
adapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
adapter.getFilter().filter(query);
invalidateOptionsMenu();
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return super.onCreateOptionsMenu(menu);
}