Skip to content

Instantly share code, notes, and snippets.

@codeforfun-jp
Created August 2, 2022 10:55
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/926c9f6f5d38b56d6242853e421c5973 to your computer and use it in GitHub Desktop.
Save codeforfun-jp/926c9f6f5d38b56d6242853e421c5973 to your computer and use it in GitHub Desktop.
【Java】ListView with imageview and textview 3
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// データを用意
String[] names = {
"とり", "しか", "きつね", "かば", "ライオン", "パンダ", "ひつじ"
};
String[] details = {
"「とり」の説明文が入ります。", "「しか」の説明文が入ります。",
"「きつね」の説明文が入ります。", "「かば」の説明文が入ります。",
"「ライオン」の説明文が入ります。", "「パンダ」の説明文が入ります。",
"「ひつじ」の説明文が入ります。"
};
int[] images = {
R.drawable.bird, R.drawable.deer, R.drawable.fox, R.drawable.hippo,
R.drawable.lion, R.drawable.panda, R.drawable.sheep
};
ArrayList<Map<String, Object>> listData = new ArrayList<>();
for (int i=0; i < names.length; i++) {
Map<String, Object> item = new HashMap<>();
item.put("name", names[i]);
item.put("detail", details[i]);
item.put("image", images[i]);
listData.add(item);
}
// ListViewにデータをセットする
ListView list = findViewById(R.id.list);
list.setAdapter(new SimpleAdapter(
this,
listData,
R.layout.list_item,
new String[] {"name", "detail", "image"},
new int[] {R.id.name, R.id.detail, R.id.image}
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment