読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - ArrayAdapterでListView作成

 

activity_main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/list1"
android:divider="#f00"
android:dividerHeight="2px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:headerDividersEnabled="false"/>

<ListView
android:id="@+id/list2"
android:divider="#00f"
android:dividerHeight="2px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:headerDividersEnabled="false"/>
</LinearLayout>
 
 
array_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="24dp"
android:padding="10px"
android:shadowColor="#f0f"
android:shadowDx="5"
android:shadowDy="5"
android:shadowRadius="2">
</TextView>
 
 
checked_item.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:checkMark="@drawable/ok"
android:shadowColor="#f0f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"/>
 
MainActivity.java
package com.example.liguofeng.arrayadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// list1
ListView list1 = (ListView) findViewById(R.id.list1);
String[] arr1 = {"赤", "黒", "白"};
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.array_item, arr1);
// adapter設定
list1.setAdapter(adapter1);

//list2
ListView list2 = (ListView) findViewById(R.id.list2);
String[] arr2 = {"java", "hibernate", "spring", "android"};
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, R.layout.checked_item, arr2);
// adapter設定
list2.setAdapter(adapter2);
}
}