亲宝软件园·资讯

展开

Android控件之ListView用法 Android控件之ListView用法实例详解

Ruthless 人气:0
想了解Android控件之ListView用法实例详解的相关内容吗,Ruthless在本文为您仔细讲解Android控件之ListView用法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,控件,ListView,下面大家一起来学习吧。

本文实例讲述了Android控件之ListView用法。分享给大家供大家参考。具体如下:

示例一:

在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android">
  <ListView android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:id="@+id/MyListView">
  </ListView>
</LinearLayout>

my_listitem.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_height="wrap_content"
  android:id="@+id/myListItem" 
  android:paddingBottom="3dip"
  android:paddingLeft="10dip">
  <TextView android:layout_height="wrap_content"
    android:layout_width="fill_parent" 
    android:id="@+id/itemTitle"
    android:textSize="20dip">
  </TextView>
  <TextView android:layout_height="wrap_content"
    android:layout_width="fill_parent" 
    android:id="@+id/itemText">
  </TextView>
</LinearLayout>

LsActivity类:

package com.ljq.ls;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class LsActivity extends Activity {
  private ListView list = null;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list = (ListView) findViewById(R.id.MyListView);
    //组织数据源
    List<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    for(int i=0;i<10;i++) {
      HashMap<String, String> map = new HashMap<String, String>();
      map.put("itemTitle", "This is Title");
      map.put("itemText", "This is text");
      mylist.add(map);
    }
    //配置适配器
    SimpleAdapter adapter = new SimpleAdapter(this, 
     mylist,//数据源 
     R.layout.my_listitem,//显示布局
     new String[] {"itemTitle", "itemText"}, //数据源的属性字段
     new int[] {R.id.itemTitle,R.id.itemText}); //布局里的控件id
    //添加并且显示
    list.setAdapter(adapter);
  }
}

运行结果如下图所示:

示例二:

目录结构

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程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论