阅读《Android 从入门到精通》(23)——列表视图
Posted SweetLoverFT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阅读《Android 从入门到精通》(23)——列表视图相关的知识,希望对你有一定的参考价值。
列表视图(ListView)
android.widget.ViewGroup;
android.widget.AdapterView;
android.widget.ListView;
继承于 AdapterView 类,通常需要和列表适配器 ListAdapter 配合起来使用
ListView 类方法
ListView 示例
完整工程:http://download.csdn.net/detail/sweetloveft/9426612
整个工程和 GridView 相比没有变化,主要就是布局的 activity_main.xml 发生了变化,另一个就是 GridView 变成了 ListView。下面我们说一下 Adapter 分类:
(1)数组适配器 ArrayAdapter:常用于显示一行文字;
(2)简单游标适配器 SimpleCursorAdapter:专门用于数据库的适配器,可以将数据库中的内容以列表的形式显示;
(3)简单适配器 SimpleAdapter:扩充性强、灵活度高,可以通过该适配器定义各种效果;
(4)基本适配器 BaseAdapter:提供了适配器的基本操作。
1.MainActivity.java
package com.sweetlover.activity;
import java.util.ArrayList;
import java.util.List;
import com.sweetlover.listview.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity
class SelfAdapter extends BaseAdapter
private class Grider
TextView textView;
private Context context = null;
private List<String> lstGrid = null;
private LayoutInflater layoutInflater = null;
public SelfAdapter(Context context)
super();
this.context = context;
public void setList(List<String> lstGrid)
this.lstGrid = lstGrid;
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount()
// TODO Auto-generated method stub
return lstGrid.size();
@Override
public Object getItem(int position)
// TODO Auto-generated method stub
return lstGrid.get(position);
@Override
public long getItemId(int position)
// TODO Auto-generated method stub
return position;
@Override
public View getView(int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
Grider gridHolder = null;
if (convertView != null)
gridHolder = (Grider) convertView.getTag();
else
gridHolder = new Grider();
convertView = layoutInflater.inflate(R.layout.grid, null);
gridHolder.textView = (TextView) convertView
.findViewById(R.id.textView);
convertView.setTag(gridHolder);
gridHolder.textView.setText(lstGrid.get(position));
return convertView;
private ListView listView = null;
private List<String> lstGrid = null;
private SelfAdapter gridAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i;
listView = (ListView)findViewById(R.id.listView);
lstGrid = new ArrayList<String>();
gridAdapter = new SelfAdapter(this);
for (i = 1; i <= 15; i++)
lstGrid.add("Grid_" + i);
gridAdapter.setList(lstGrid);
listView.setAdapter(gridAdapter);
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
3.grid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="8dp" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="@string/content_description"
android:src="@drawable/icon" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/imageView" />
</RelativeLayout>
4.AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sweetlover.listview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.sweetlover.activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
以上是关于阅读《Android 从入门到精通》(23)——列表视图的主要内容,如果未能解决你的问题,请参考以下文章