基于数组的ListView
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/books" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="books" >
<item>Test0</item>
<item>Test1/item>
<item>Test2</item>
<item>Test3</item>
</string-array>
</resources>
如果窗口仅需要显示列表,可以让Activity继承ListActivity,传入Adapter呈现列表
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] arr = {"Test0", "Test1", "Test2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, arr);
setListAdapter(adapter);
}
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
ArrayAdapter:简单,易用的Adapter,通常用于将数组或List集合的多个值包装成多个列表项
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@id/listView" />
ListView listView = (ListView) findViewById(R.id.listView);
//定义用于为列表项提供数据的数组
String[] array = new String[]{"Test0","Test1","Test2"};
//将数组包装为Adapter
ArrayAdater<String> adapter = new ArrayAdapter<String>(this,
R.layout.item,//资源ID,组件将作为ArrayAdapter的列表项组件
array);//数组或list,负责为多个列表项提供数据
//为ListView设置Adapter
listView.setAdapter(afdapter);
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
SimpleAdapter用于实现比较复杂的ListView
public class MainActivity extends AppCompatActivity {
private String[] names = new String[]{"张三", "李四", "王五"};
private String[] descs = new String[]{"zhangsan", "lisi", "wangwu"};
private int[] imageIds = new int[]{
R.drawable.a,
R.drawable.b,
R.drawable.c};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_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,
//List<>类型的集合对象,集合中的每个Map<>对象生成一个列表项
listItems,
//指定界面布局的资源文件ID
R.layout.simple_item,
//String[]类型参数,决定提取Map<>对象中哪些key对应的value来生成列表项
new String[]{"personName", "header", "desc"},
//int[]类型的参数,决定填充哪些组件
new int[]{R.id.name, R.id.header, R.id.desc}
);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(simpleAdapter);
//设置点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),names[position]+"被单击",Toast.LENGTH_SHORT).show();
}
});
//设置选中事件
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),names[position]+"被选中",Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
<?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="match_parent">
<ImageView android:layout_width="50dp" android:layout_height="50dp" android:paddingLeft="10dp" android:id="@+id/header" />
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#f0f" android:paddingLeft="10dp" android:id="@+id/name" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/desc" android:textSize="14dp" android:paddingLeft="10dp" />
</LinearLayout>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
BaseAdapter adapter = new BaseAdapter() {
//该方法的返回值控制Adapter含有多少个列表项
@Override
public int getCount() {
//指定一共包含40个列表项
return 40;
}
//该方法的返回值返回第position处的列表项的内容
@Override
public Object getItem(int position) {
return null;
}
//该方法返回值返回第position处的列表项ID
//重写该方法,将返回值作为列表项的ID
@Override
public long getItemId(int position) {
return position;
}
//该方法的返回值返回第position处的列表项组件
//重写该方法,将返回的View作为列表框
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout linearLayout = new LinearLayout(MainActivity.this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(R.mipmap.ic_launcher);
TextView textView = new TextView(MainActivity.this);
textView.setText("第"+(position+1)+"个列表项");
textView.setTextSize(20);
textView.setTextColor(Color.RED);
linearLayout.addView(imageView);
linearLayout.addView(textView);
return linearLayout;
}
};
listView.setAdapter(adapter);
}
}