To implement a SearchView for a simple ListView showing a String list in Sketchware, follow the steps given below.
1. Create a new project in Sketchware.
2. In VIEW area add a ListView listview1.
3. Switch On AppCompat and Design.
4. Create a String List list.
2. In VIEW area add a ListView listview1.
3. Switch On AppCompat and Design.
4. Create a String List list.
5. Create a more block
6. In the more block extra, use an add source directly block and put codes to declare a SearchView searchview and an ArrayAdapter adapter. After that add the SearchView as an OptionsMenu item.
}
android.support.v7.widget.SearchView searchview;
ArrayAdapter<String> adapter;
@Override
public boolean onCreateOptionsMenu( Menu menu) {
menu.add(0,0,0,"search").setActionView(searchview).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
{
android.support.v7.widget.SearchView searchview;
ArrayAdapter<String> adapter;
@Override
public boolean onCreateOptionsMenu( Menu menu) {
menu.add(0,0,0,"search").setActionView(searchview).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
{
7. In onCreate event, add items to the String List list.
8. After adding items to the list use add source directly blocks and put following codes:
i. Define the ArrayAdapter and set it as adapter of ListView.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listview1.setAdapter(adapter);
ii. Define SearchView and add it to Toolbar.
searchview = new android.support.v7.widget.SearchView(this);
iii. Set OnQueryTextListener for the SearchView.
searchview.setOnQueryTextListener(new android.support.v7.widget.SearchView.OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
String text = newText;
adapter.getFilter().filter(text);
return false;
}
});
9. Save and run the project. You will see the search icon on the Toolbar. You can click on it and search the ListView.
ad
0 Comments