android ListView&&适配器使用
2012-03-06 21:33
323 查看
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
listview.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" >
<TextView
android:id="@+id/personName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView
android:id="@+id/personAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView
android:id="@+id/personAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView
android:id="@+id/personEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
MyProject1Activity.java
package com.zsc.lian;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MyProject1Activity extends Activity {
private ListView listView = null;
Person[] person = new Person[] {
new Person("张飞", 38, "zhangfei@gmail.com", "燕山"),
new Person("张辽", 36, "zhangliao@sina.com", "雁门"),
new Person("张角", 51, "zhangjiao@gmail.com", "钜鹿"),
new Person("张三丰", 200, "sanfeng@gmail.com", "辽东"),
new Person("张牙舞爪", 25, "5zhao@gmail.com", "冀州"),
new Person("张灯结彩", 25, "5zhao@gmail.com", "冀州"),
new Person("张唑啉", 25, "5zhao@gmail.com", "冀州"),
new Person("张大民", 25, "5zhao@gmail.com", "冀州"),
new Person("张牙舞爪", 25, "5zhao@gmail.com", "冀州") };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listview);
ListAdapt listAdapt = new ListAdapt(this, R.id.listview, person);
listView.setAdapter(listAdapt);
}
}
person.java
package com.zsc.lian;
public class Person {
private String personName = null;
private int personAge;
private String personAddress = null;
private String personEmail = null;
public Person(String personName, int personAge, String personAddress,
String personEmail) {
this.personName = personName;
this.personAge = personAge;
this.personAddress = personAddress;
this.personEmail = personEmail;
}
public String getPersonName() {
return this.personName;
}
public int getPersonAge() {
return this.personAge;
}
public String getPersonAddress() {
return this.personAddress;
}
public String getpersonEmail() {
return this.personEmail;
}
public String toString() {
return this.personName + " " + this.personAge + " "
+ this.personAddress + " " + this.personEmail;
}
}
ListAdapt.java
package com.zsc.lian;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListAdapt extends ArrayAdapter {
private LayoutInflater mylayoutInflater = null;
// private HoldText holdText = new HoldText();
public ListAdapt(Context context, int listview, Person[] person) {
super(context, listview, person);
this.mylayoutInflater = LayoutInflater.from(context);
}
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
view = mylayoutInflater.inflate(R.layout.listview, null);
}
HoldText holdText = null;
if (holdText == null) {
holdText = new HoldText();
holdText.textView1 = (TextView) view.findViewById(R.id.personName);
holdText.textView2 = (TextView) view.findViewById(R.id.personAge);
holdText.textView3 = (TextView) view
.findViewById(R.id.personAddress);
holdText.textView4 = (TextView) view.findViewById(R.id.personEmail);
view.setTag(holdText);
} else {
holdText = (HoldText) view.getTag();
}
Person person = (Person) getItem(position);
holdText.textView1.setText(person.getPersonName());
holdText.textView2.setText(String.valueOf(person.getPersonAge()));
holdText.textView3.setText(person.getPersonAddress());
holdText.textView4.setText(person.getpersonEmail());
return view;
}
public class HoldText {
TextView textView1 = null;
TextView textView2 = null;
TextView textView3 = null;
TextView textView4 = null;
}
}
首先这里有几个知识点需要讲解一下
第一:在这段程序中用到了适配器,但是这是自己实现的一个适配器,该适配器继承了android中的ArrayAdapter,于是查找了一下官方文档,看到里面有ArrayAdapter这样的一个构造方法
public ArrayAdapter
Since:
API Level 1
Constructor
Parameters
context是当前上下文,
textViewResourceld是你的布局文件的ID号,
objects是你要放在ListView中的数据对象。
所以代码中:ListAdapt listAdapt = new ListAdapt(this, R.id.listview, person);这句估计很容易理解。
接着,
ListAdapt这个自己实现的类中,继承了android中的ArrayAdapter类,再喵一下官方文档,
To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override
如果想要给View上添加自己的数据,那么需要重写getView(int,view,viewGroup)。
View中有一个方法public void setTag(Object tag)
Sets the tag associated with this view.把tag标签关联到View上,有setTag(),自然就有getTag()方法。
好了,这个程序暂时说到这里,继续学习android!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
listview.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" >
<TextView
android:id="@+id/personName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView
android:id="@+id/personAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView
android:id="@+id/personAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView
android:id="@+id/personEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
MyProject1Activity.java
package com.zsc.lian;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MyProject1Activity extends Activity {
private ListView listView = null;
Person[] person = new Person[] {
new Person("张飞", 38, "zhangfei@gmail.com", "燕山"),
new Person("张辽", 36, "zhangliao@sina.com", "雁门"),
new Person("张角", 51, "zhangjiao@gmail.com", "钜鹿"),
new Person("张三丰", 200, "sanfeng@gmail.com", "辽东"),
new Person("张牙舞爪", 25, "5zhao@gmail.com", "冀州"),
new Person("张灯结彩", 25, "5zhao@gmail.com", "冀州"),
new Person("张唑啉", 25, "5zhao@gmail.com", "冀州"),
new Person("张大民", 25, "5zhao@gmail.com", "冀州"),
new Person("张牙舞爪", 25, "5zhao@gmail.com", "冀州") };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listview);
ListAdapt listAdapt = new ListAdapt(this, R.id.listview, person);
listView.setAdapter(listAdapt);
}
}
person.java
package com.zsc.lian;
public class Person {
private String personName = null;
private int personAge;
private String personAddress = null;
private String personEmail = null;
public Person(String personName, int personAge, String personAddress,
String personEmail) {
this.personName = personName;
this.personAge = personAge;
this.personAddress = personAddress;
this.personEmail = personEmail;
}
public String getPersonName() {
return this.personName;
}
public int getPersonAge() {
return this.personAge;
}
public String getPersonAddress() {
return this.personAddress;
}
public String getpersonEmail() {
return this.personEmail;
}
public String toString() {
return this.personName + " " + this.personAge + " "
+ this.personAddress + " " + this.personEmail;
}
}
ListAdapt.java
package com.zsc.lian;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListAdapt extends ArrayAdapter {
private LayoutInflater mylayoutInflater = null;
// private HoldText holdText = new HoldText();
public ListAdapt(Context context, int listview, Person[] person) {
super(context, listview, person);
this.mylayoutInflater = LayoutInflater.from(context);
}
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
view = mylayoutInflater.inflate(R.layout.listview, null);
}
HoldText holdText = null;
if (holdText == null) {
holdText = new HoldText();
holdText.textView1 = (TextView) view.findViewById(R.id.personName);
holdText.textView2 = (TextView) view.findViewById(R.id.personAge);
holdText.textView3 = (TextView) view
.findViewById(R.id.personAddress);
holdText.textView4 = (TextView) view.findViewById(R.id.personEmail);
view.setTag(holdText);
} else {
holdText = (HoldText) view.getTag();
}
Person person = (Person) getItem(position);
holdText.textView1.setText(person.getPersonName());
holdText.textView2.setText(String.valueOf(person.getPersonAge()));
holdText.textView3.setText(person.getPersonAddress());
holdText.textView4.setText(person.getpersonEmail());
return view;
}
public class HoldText {
TextView textView1 = null;
TextView textView2 = null;
TextView textView3 = null;
TextView textView4 = null;
}
}
首先这里有几个知识点需要讲解一下
第一:在这段程序中用到了适配器,但是这是自己实现的一个适配器,该适配器继承了android中的ArrayAdapter,于是查找了一下官方文档,看到里面有ArrayAdapter这样的一个构造方法
public ArrayAdapter
(Context context, int textViewResourceId, T[] objects)
Since:API Level 1
Constructor
Parameters
context | The current context. |
---|---|
textViewResourceId | The resource ID for a layout file containing a TextView to use when instantiating views. |
objects | The objects to represent in the ListView. |
textViewResourceld是你的布局文件的ID号,
objects是你要放在ListView中的数据对象。
所以代码中:ListAdapt listAdapt = new ListAdapt(this, R.id.listview, person);这句估计很容易理解。
接着,
ListAdapt这个自己实现的类中,继承了android中的ArrayAdapter类,再喵一下官方文档,
To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override
getView(int, View, ViewGroup)to return the type of view you want.
如果想要给View上添加自己的数据,那么需要重写getView(int,view,viewGroup)。
View中有一个方法public void setTag(Object tag)
Sets the tag associated with this view.把tag标签关联到View上,有setTag(),自然就有getTag()方法。
好了,这个程序暂时说到这里,继续学习android!
相关文章推荐
- Android定制ListView的界面(使用继承自ArrayAdapter的自定义适配器)--《第一行代码Android》学习笔记
- Android菜鸟练习第十六课 RefreshListView的使用配合万能适配器(本篇文章自用)
- Android中ListView常用的两种适配器ArrayAdapter和SimpleAdapter使用
- Android-ListView适配器BaseAdapter的使用和优化
- Android ListView控件的使用(自定义适配器 + ListView + 解决OOM )
- Android开发适配器系列:ListView使用自定义适配器(显示所有的联系人)
- ListView - 基本使用方法&适配器封装(参考鸿神)
- Android listview&gridview getview 方法多次调用问题解决方法 并附上单页加载实现问卷调查适配器源代码
- Android组件ExpandableListView及其适配器,线程中的runOnUiThread()的使用
- Android异步下载网络图片&android解析xml文件的方式&使用Adapter为ListView提供数据
- 【Android开发】自定义ListView,使用通用适配器,并实现ListView上的每一项和每一项上的按钮等控件同时监听
- android_listView_适配器Adapter的使用
- Android ListView适配器Adapter的使用,item的复用和ViewHolder优化
- Android自定义适配器和ListView的点击事件相结合的使用
- Android:控件ListView列表项与适配器结合使用
- android之ListView中使用的适配器CursorAdapter
- android适配器及监听点击和滚动在ListView中的使用
- android中ListView与适配器的简单使用
- Android中ListView 控件与 Adapter 适配器如何使用?
- <Android>使用ScrollView 实现 ListView 的下拉刷新