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

liguofeng29’s blog

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

AndroidのUI - SimpleAdapterで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/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/showClicked"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
 
 
simple_item.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="horizontal">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#f0f"
android:paddingLeft="10dp"
/>
<TextView android:id="@+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:paddingLeft="10dp"
/>
</LinearLayout>
</LinearLayout>
 
MainActivity.java
package com.example.liguofeng.simpleadapter;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

private String[] names = {"Java", "Android", "Spring", "Linux"};
private String[] details = {"Java is a programming language",
"Android is a OS",
"Spring is a framework",
"Linux is a OS."};

private int[] images = {
R.drawable.java,
R.drawable.android,
R.drawable.spring,
R.drawable.linux,
};

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

// MAP作成
List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", names[i]);
map.put("detail", details[i]);
map.put("image", images[i]);

itemList.add(map);
}

// SimpleAdapter作成
SimpleAdapter simple = new SimpleAdapter(
this, // CONTEXT
itemList, // リストの要素
R.layout.simple_item, // LayoutのID
new String[]{"name", "detail", "image"}, // キー
new int[]{R.id.name, R.id.detail, R.id.image} // Layout内コンポーネントID
);

ListView list = (ListView) findViewById(R.id.list);

// SimpleAdapter設定
list.setAdapter(simple);

final TextView textView1 = (TextView) findViewById(R.id.showClicked);
// ListViewのITEMクリックイベント
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView1.setText(names[position] + "がクリックされました");
}
});
}
}