ListView
6585 ワード
ListViewを作成するには、次の2つの方法があります.はListViewを直接使用して作成される. ActivityにListActivity を継承させる
ListViewはAutoComplete、Spinner(ドロップダウンリスト)と同様に、表示用のリスト項目が必要であり、コンテンツアダプタを使用する必要があります.
xml
MainActivity ActivityにListActivity を継承させる
xml
MainActivity
simpleAdapter
xml
MainActivity
ListViewはAutoComplete、Spinner(ドロップダウンリスト)と同様に、表示用のリスト項目が必要であり、コンテンツアダプタを使用する必要があります.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- -->
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="@array/books"
android:divider="@drawable/red"
android:headerDividersEnabled="false"
/>
<!-- ArrayAdapter ListView -->
<ListView
android:id="@+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@drawable/green"
/>
</LinearLayout>
MainActivity
package org.crazyit.listview;
import org.crazyit.listview.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class ArrayAdapterList extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list2 = (ListView)findViewById(R.id.list2);
//
String[] arr ={" " , " " , " "};
// ArrayAdapter
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this , android.R.layout.simple_list_item_1 , arr);
// ListView Adapter
list2.setAdapter(arrayAdapter);
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000ff"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
MainActivity
package org.crazyit.listactivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class ListActivityTest extends ListActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// //
// setContentView(R.layout.main);
String[] arr = { " ", " ", " " };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, arr);
//
setListAdapter(adapter);
}
}
simpleAdapter
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- List -->
<ListView android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<!-- ImageView, 。 -->
<ImageView android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
/>
<!-- TextView, 。 -->
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
/>
</LinearLayout>
MainActivity
package org.crazyit.simpleadapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
/**
* Description:
* <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee [email protected]
* @version 1.0
*/
public class SimpleAdapterTest extends Activity
{
private String[] names = new String[]
{ " ", " ", " ", " "};
private int[] imageIds = new int[]
{ R.drawable.tiger , R.drawable.nongyu
, R.drawable.qingzhao , R.drawable.libai};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// List ,List Map
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < names.length; i++)
{
Map<String, Object> listItem = new HashMap<String, Object>();
listItem.put("header", imageIds[i]);
listItem.put("personName", names[i]);
listItems.add(listItem);
}
// SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this
, listItems
, R.layout.main
, new String[]{ "personName", "header" }
, new int[]{R.id.name , R.id.header});
ListView list = (ListView)findViewById(R.id.mylist);
// ListView Adapter
list.setAdapter(simpleAdapter);
}
}