-
-
Save codeforfun-jp/8a853b129bc94e80631502506a13b7ab to your computer and use it in GitHub Desktop.
【Java】ListView with two textview 2
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 = { | |
{"とり", "「とり」の説明文が入ります。"}, | |
{"しか", "「しか」の説明文が入ります。"}, | |
{"きつね", "「きつね」の説明文が入ります。"}, | |
{"かば", "「かば」の説明文が入ります。"}, | |
{"ライオン", "「ライオン」の説明文が入ります。"}, | |
{"パンダ", "「パンダ」の説明文が入ります。"}, | |
{"ひつじ", "「ひつじ」の説明文が入ります。"}, | |
}; | |
ArrayList<Map<String, String>> listData = new ArrayList<>(); | |
for (String[] datum : data) { | |
Map<String, String> item = new HashMap<>(); | |
item.put("name", datum[0]); | |
item.put("detail", datum[1]); | |
listData.add(item); | |
} | |
// ListViewにデータをセットする | |
ListView list = findViewById(R.id.list); | |
list.setAdapter(new SimpleAdapter( | |
this, | |
listData, | |
android.R.layout.simple_list_item_2, | |
new String[] {"name", "detail"}, | |
new int[] {android.R.id.text1, android.R.id.text2} | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment