Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created July 25, 2022 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeforfun-jp/205ffe1c116d4270586e3e67459302fd to your computer and use it in GitHub Desktop.
Save codeforfun-jp/205ffe1c116d4270586e3e67459302fd to your computer and use it in GitHub Desktop.
【Java】ListView with one textview 5
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.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String message = data[i] + "をクリックしました。";
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment