ListView及Adapter的使用

一、使用ArrayAdapter

其中ArrayAdapter的构造函数有如下几个,其中resource是指每个列表项的布局文件,objects是指列表项的数据源,此处通常指一个数组

ArrayAdapter(Context context, int resource)

ArrayAdapter(Context context, int resource, int textViewResourceId)

ArrayAdapter(Context context, int resource, T[] objects)    

ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)

)">ArrayAdapter(Context context, int resource, List<T> objects)

)">ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

Public Constructors


public ArrayAdapter (Context context, int resource)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a TextView to use when instantiating views.

public ArrayAdapter (Context context, int resource, int textViewResourceId)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId
The id of the TextView within the layout resource to be populated

public ArrayAdapter (Context context, int resource, T[] objects)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a TextView to use when instantiating views.

objects
The objects to represent in the ListView.

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId
The id of the TextView within the layout resource to be populated

objects
The objects to represent in the ListView.

public ArrayAdapter (Context context, int resource, List<T> objects)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a TextView to use when instantiating views.

objects
The objects to represent in the ListView.

public ArrayAdapter (Context context, int resource, int textViewResourceId, List<T> objects)

Added in API level 1

Constructor

Parameters

context
The current context.

resource
The resource ID for a layout file containing a layout to use when instantiating views.

textViewResourceId
The id of the TextView within the layout resource to be populated

objects
The objects to represent in the ListView.

1.MainActivity.java

package org.crazyit.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list1 = (ListView) findViewById(R.id.list1);
// 定义一个数组
String[] arr1 = { "孙悟空", "猪八戒", "牛魔王" };
// 将数组包装为ArrayAdapter
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>
(this, R.layout.array_item, arr1);
// 为ListView设置Adapter
list1.setAdapter(adapter1);
ListView list2 = (ListView) findViewById(R.id.list2);
// 定义一个数组
String[] arr2 = { "Java", "Hibernate", "Spring" , "Android" };
// 将数组包装为ArrayAdapter
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>
(this, R.layout.checked_item, arr2);
// 为ListView设置Adapter
list2.setAdapter(adapter2);
}
}
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 设置使用红色的分隔条 -->
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#f00"
android:dividerHeight="2px"
android:headerDividersEnabled="false"/>
<!-- 设置使用绿色的分隔条 -->
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#0f0"
android:dividerHeight="2px"
android:headerDividersEnabled="false"/>
</LinearLayout>
3.array_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
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:padding="10px"
android:shadowColor="#f0f"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"/>
4.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"/>

二、使用SimpleAdaper

SimpleAdapter的构造函数如下,其中resource为该列表项的布局资源,to为该布局资源中每一列的ID;data为该列表项的数据源,其中data的每一列表项,包含多列Map<String,?>,每一个Map为一列,Map中的每一个Key值均要在from中指出。


public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

Added in API level 1

Constructor

Parameters

context
The context where the View associated with this SimpleAdapter is running

data
A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"

resource
Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"

from
A list of column names that will be added to the Map associated with each item.

to
The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

1.MainActivity.java

package org.crazyit.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import static android.widget.AdapterView.OnItemClickListener;
import static android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

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


public class MainActivity extends Activity
{
private String[] names = new String[]
{ "虎头", "弄玉", "李清照", "李白"};
private String[] descs = 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]);
listItem.put("desc", descs[i]);
listItems.add(listItem);
}
// 创建一个SimpleAdapter
SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems,
R.layout.simple_item,
new String[] { "personName", "header" , "desc"},
new int[] { R.id.name, R.id.header , R.id.desc });
ListView list = (ListView) findViewById(R.id.mylist);
// 为ListView设置Adapter
list.setAdapter(simpleAdapter);

// 为ListView的列表项的单击事件绑定事件监听器
list.setOnItemClickListener(new OnItemClickListener()
{
// 第position项被单击时激发该方法
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
System.out.println(names[position]
+ "被单击了");
}
});
// 为ListView的列表项的选中事件绑定事件监听器
list.setOnItemSelectedListener(new OnItemSelectedListener()
{
// 第position项被选中时激发该方法
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
System.out.println(names[position]
+ "被选中了");
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});

}
}

2.main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 定义一个ListView -->
<ListView android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
3.simple_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_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"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 定义一个TextView,用于作为列表项的一部分。 -->
<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,用于作为列表项的一部分。 -->
<TextView android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:paddingLeft="10dp"/>
</LinearLayout>
</LinearLayout>

三、使用BaseAdapter

使用BaseAdapter最主要的及时重写其中提供的方法,以及布局文件通过其的getView实现。

package org.crazyit.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity
{
ListView myList;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView) findViewById(R.id.myList);
BaseAdapter adapter = new BaseAdapter()
{
@Override
public int getCount()
{
// 指定一共包含40个选项
return 10;
}
@Override
public Object getItem(int position)
{
return null;
}
// 重写该方法,该方法的返回值将作为列表项的ID
@Override
public long getItemId(int position)
{
return position;
}
// 重写该方法,该方法返回的View将作为列表框
@Override
public View getView(int position
, View convertView , ViewGroup parent)
{
// 创建一个LinearLayout,并向其中添加两个组件
LinearLayout line = new LinearLayout(MainActivity.this);
line.setOrientation(0);
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
TextView text = new TextView(MainActivity.this);
text.setText("第" + (position +1 ) + "个列表项");
text.setTextSize(20);
text.setTextColor(Color.RED);
line.addView(image);
line.addView(text);
// 返回LinearLayout实例
return line;
}
};

myList.setAdapter(adapter);
}
}
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/myList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
原文地址:https://www.cnblogs.com/ql698214/p/5246713.html