-
-
Save codeforfun-jp/926c9f6f5d38b56d6242853e421c5973 to your computer and use it in GitHub Desktop.
【Java】ListView with imageview and textview 3
This file contains hidden or 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[] 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