-
-
Save codeforfun-jp/81b23d007bcfe2ed3032fec9609b8332 to your computer and use it in GitHub Desktop.
【Java】ListView with searchview 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// データを用意 | |
String[] data = { | |
"あだち", "いのうえ", "うえき", "うえだ", "うえと", "うちだ", | |
"うちの", "うつみ", "うの", "えとう", "おおた" | |
}; | |
// ListViewにデータをセットする | |
ListView list = findViewById(R.id.list); | |
list.setAdapter(new ArrayAdapter<>( | |
this, | |
android.R.layout.simple_list_item_1, | |
data | |
)); | |
// テキストフィルターを有効にする | |
list.setTextFilterEnabled(true); | |
SearchView searchView = findViewById(R.id.search); | |
searchView.setOnQueryTextListener( | |
new SearchView.OnQueryTextListener() { | |
// 入力テキストに変更があったとき | |
@Override | |
public boolean onQueryTextChange(String s) { | |
if (s.equals("")) { | |
list.clearTextFilter(); | |
} else { | |
list.setFilterText(s); | |
} | |
return false; | |
} | |
// 検索ボタンを押したとき | |
@Override | |
public boolean onQueryTextSubmit(String s) { | |
return false; | |
} | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment