android ListView&&适配器使用

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 (Context context, int textViewResourceId, T[] objects)
Since: API Level 1

Constructor

Parameters
contextThe current context.
textViewResourceIdThe resource ID for a layout file containing a TextView to use when instantiating views.
objectsThe objects to represent in the ListView.
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 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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值