-1
我試圖創建一個SearchView只有一個簡單的ListView沒有圖像,它的工作。但是在我將圖像添加到列表視圖之後... SearchView不起作用。我是新手,所以請幫忙。提前致謝。如何在使用圖像的ListView的Actionbar中創建SearchView?
這是我的主要代碼
public class SecondActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ListView listview; SearchView sv; SearchManager searchManager;
ArrayAdapter<String> myAdapter;
String [] Topics = {"ABC","BCD","CDE","DEF","EFG","FGH","GHI","HIJ"};
CustomAdapter adapter;
String[] NOS;
int[] images = {R.drawable.logheader,R.drawable.logheader,R.drawable.logheader,R.drawable.logheader,R.drawable.logheader,R.drawable.logheader,R.drawable.logheader,R.drawable.logheader};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
adapter = new CustomAdapter(this,Topics,images);
listview = (ListView) findViewById(R.id.sell);
sv =(SearchView) findViewById(R.id.search_Action);
searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
myAdapter = adapter;
listview.setAdapter(adapter);
final Assigner assigner = new Assigner();
listview.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String Op = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(SecondActivity.this,Op, Toast.LENGTH_LONG).show();
NOS= assigner.rept(position);
Intent into = new Intent(SecondActivity.this,FourthActivity.class);
into.putExtra("well",NOS);
startActivity(into);
}}
);
}
boolean twice;
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
}
if (twice == true) {
Intent intt = new Intent(Intent.ACTION_MAIN);
intt.addCategory(Intent.CATEGORY_HOME);
intt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intt);
finish();
System.exit(0);
}
Toast.makeText(this, "Press BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
twice = false;
}
}, 3000);
twice = true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.second,menu);
sv = (SearchView) menu.findItem(R.id.search_Action).getActionView();
sv.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
sv.setQueryHint("Search any topic...");
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
myAdapter.getFilter().filter(newText);
return false;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.search_Action) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
}
else if (id == R.id.nav_share) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
安置自己在這裏的代碼更好的幫助 – Vickyexpert