Android控件之ListView探究二

Android控件之ListView探究二

目录结构

main.xml布局文件

复制代码
<?xml version="1.0" encoding="utf-8"?> <!-- 使用相对布局 --> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:layout_marginLeft="30dip" android:textSize="20dip" android:id="@+id/id"/> <TextView android:layout_width="100dip" android:layout_height="wrap_content" android:layout_alignTop="@id/id" android:layout_toRightOf="@id/id" android:textSize="20dip" android:id="@+id/name"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@id/name" android:layout_toRightOf="@id/name" android:textSize="20dip" android:id="@+id/age"/> </RelativeLayout>
复制代码

实体JavaBean

复制代码
package com.ljq.domain; public class Person { private String id; private String name; private String age; public Person() { super(); } public Person(String id, String name, String age) { super(); this.id = id; this.name = name; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
复制代码

自定义适配器PersonAdapter

复制代码
package com.ljq.ls; import java.util.List; import com.ljq.domain.Person; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; /** * ListView加载adapter的过程 * * 1、先判断adapter有多少数据项,根据这个数据确定有多少个item * * 2、确定每个item里加载哪个view * * 3、在view里加载要显示的数据 * * @author jiqinlin * */ public class PersonAdapter extends ArrayAdapter{ private LayoutInflater layoutInflater = null; private List<Person> persons; public PersonAdapter(Context context, int textViewResourceId, List objects) { super(context, textViewResourceId, objects); layoutInflater = LayoutInflater.from(context); persons = objects; } /** * 获取adapter里有多少个数据项 */ @Override public int getCount() { return persons.size(); } @Override public Object getItem(int position) { return persons.get(position); } @Override public long getItemId(int position) { return position; } /** * 创建显示的数据界面 * * Adapter的作用就是ListView界面与数据之间的桥梁, * 当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View。 * 想过没有? 在我们的列表有1000000项时会是什么样的?是不是会占用极大的系统资源? */ @Override public View getView(int position, View convertView, ViewGroup parent) { /* // 优化前 ViewHolder holder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.main, null); holder.id = (TextView)convertView.findViewById(R.id.id); holder.name = (TextView)convertView.findViewById(R.id.name); holder.age = (TextView)convertView.findViewById(R.id.age); convertView.setTag(holder); holder.id.setText(persons.get(position).getId()); holder.name.setText(persons.get(position).getName()); holder.age.setText(persons.get(position).getAge()); return convertView; */ // 优化后 ViewHolder holder; if(convertView == null){ convertView = layoutInflater.inflate(R.layout.main, null); holder = new ViewHolder(); holder.id = (TextView)convertView.findViewById(R.id.id); holder.name = (TextView)convertView.findViewById(R.id.name); holder.age = (TextView)convertView.findViewById(R.id.age); convertView.setTag(holder); }else{ holder = (ViewHolder)convertView.getTag(); } holder.id.setText(persons.get(position).getId()); holder.name.setText(persons.get(position).getName()); holder.age.setText(persons.get(position).getAge()); return convertView; } /** * 界面上的显示控件 * * @author jiqinlin * */ private static class ViewHolder{ private TextView id; private TextView name; private TextView age; } }
复制代码

类LsActivity

复制代码
package com.ljq.ls; import java.util.ArrayList; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.Toast; import com.ljq.domain.Person; public class LsActivity extends ListActivity { private ArrayList<Person> persons = new ArrayList<Person>(); private PersonAdapter personAdapter = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initData(); personAdapter =new PersonAdapter(LsActivity.this, R.layout.main, persons); setListAdapter(personAdapter); registerForContextMenu(getListView()); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Person person = persons.get(position); Toast.makeText(LsActivity.this, person.getId()+":"+person.getName() +":"+person.getAge(), Toast.LENGTH_LONG).show(); return; } private void initData(){ persons.add(new Person("序号", "姓名", "年龄")); persons.add(new Person("1", "ljq1", "20")); persons.add(new Person("2", "ljq2", "20")); persons.add(new Person("3", "ljq3", "20")); persons.add(new Person("4", "ljq4", "20")); persons.add(new Person("5", "ljq5", "20")); persons.add(new Person("6", "ljq6", "20")); persons.add(new Person("7", "ljq7", "20")); persons.add(new Person("8", "ljq8", "20")); persons.add(new Person("9", "ljq9", "20")); } }
复制代码

运行结果


参考文档:

android常用控件

原文地址:https://www.cnblogs.com/chengliu/p/4130620.html