Android ListView列表视图的使用方法

发布时间:2019-08-08 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Android ListView列表视图的使用方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

当你要将某个从数据库或者文件中获得相当大的数据,在界面中向用户展示的时候,由于定义一个个视图比较麻烦,AndROId中提供了类似于数组的控件CListView。

使用方法:

假设我们要转的数据是一个PErson对象数组

 package cn.zhuangzhihuang.mylist;  public class Person {  PRivate String name;  private String tel;      public Person(String name, String tel) {  super();  this.name = name;  this.tel = tel;  }      public String getName() {  return name;  }    public void setName(String name) {  this.name = name;  }    public String getTel() {  return tel;  }    public void setTel(String tel) {  this.tel = tel;  }    public String toString() {  return "点击的联系人为" + this.getName() +"/n话号码为" + this.getTel();  }   }
 Person[] DB = {      new Person("张三","18555555555"),      new Person("李四","18555555556"),      new Person("王五","18555555557"),      new Person("赵六","18555555558"),      new Person("邓七","18555555559")              };      List<Person> friend_List;     friend_List = new ArrayList<Person>(); for(int i=0;i<DB.length;i++) {  friend_List.add(DB[i]); } 

1、首先,你需要在XMl中加入一个listview控件:

 <ListView   android:id="@+id/data_view"   android:layout_width="match_parent"   android:layout_height="wrap_content" > </ListView>

2、接着你需要创建一个适配器MyAdapter类,这个适配器的作用时将你要展示的数据转成可见格式也就时View。

 class MyAdapter extends BaseAdapter {   @override  public int getCount() { //返回表的长度   // TODO Auto-generated method stub   return friend_List.size();  }   @Override  public Object getITem(int position) { //返回表的index位置的元组   // TODO Auto-generated method stub   return friend_List.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   View view = View.inflate(MainActivity.this, R.layout.item, null);   TextView tv_item_name = (TextView) view.findViewById(R.id.tv_item_name);   TextView tv_item_tel = (TextView) view.findViewById(R.id.tv_item_tel);   tv_item_name.setText(friend_List.get(position).getName());   tv_item_tel.setText(friend_List.get(position).getTel());   return view;   //初始化这个listview会调用到这个方法,因为要把传进去的对象数组的每个元素转成view加入到listview中  }       }

3、然后要在xML中写下你要转成的view的模板

 <&#63;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="horizontal" >      <TextView     android:id="@+id/tv_item_name"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:layout_weight="1"     android:gravity="center"     android:textSize="20sp"     />      <TextView     android:id="@+id/tv_item_tel"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:layout_weight="1"     android:gravity="center"     android:textSize="20sp"     />  </LinearLayout>

4、最后在MainActivity中把listview的适配器设置一下。调用setAdapter这个方法

 data_view.setAdapter(myAdapter);

Android代码:
xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent"   android:orientation="vertical" >      <LinearLayout     android:layout_width="match_parent"     android:layout_height="wrap_content"     >       <TextView      android:id="@+id/tv1"      android:layout_width="0dp"      android:layout_height="wrap_content"      android:layout_weight="1"      android:gravity="center"      android:text="姓名"      android:textSize="20sp"      />        <TextView      android:id="@+id/tv2"      android:layout_width="0dp"      android:layout_height="wrap_content"      android:layout_weight="1"      android:gravity="center"      android:text="联系电话"      android:textSize="20sp"      />        </LinearLayout>        <ListView      android:id="@+id/data_view"      android:layout_width="match_parent"      android:layout_height="wrap_content" >    </ListView>   </LinearLayout>

MainActivity:

 package cn.zhuangzhihuang.mylist;  import java.util.ArrayList; import java.util.List;  import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;  public class MainActivity extends Activity {   List<Person> friend_List;     @Override   protected void onCreate(Bundle savedInstancestate) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);          ListView data_view = (ListView) findViewById(R.id.data_view);          Person[] DB = {      new Person("张三","18555555555"),      new Person("李四","18555555556"),      new Person("王五","18555555557"),      new Person("赵六","18555555558"),      new Person("邓七","18555555559")              };          friend_List = new ArrayList<Person>();     for(int i=0;i<DB.length;i++) {      friend_List.add(DB[i]);     }          //自定义适配器     MyAdapter myAdapter = new MyAdapter();     data_view.setAdapter(myAdapter);          data_view.setOnItemClickListener(new OnItemClickListener() {    @Override   public void onItemClick(AdapterView<?> parent, View view,    int position, long id) {   // TODO Auto-generated method stub   String temp = friend_List.get((int)id).toString();      Toast.makeText(MainActivity.this, temp, 0).show();      }  });   }      class MyAdapter extends BaseAdapter {   @Override  public int getCount() { //返回表的长度   // TODO Auto-generated method stub   return friend_List.size();  }   @Override  public Object getItem(int position) { //返回表的index位置的元组   // TODO Auto-generated method stub   return friend_List.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   View view = View.inflate(MainActivity.this, R.layout.item, null);   TextView tv_item_name = (TextView) view.findViewById(R.id.tv_item_name);   TextView tv_item_tel = (TextView) view.findViewById(R.id.tv_item_tel);   tv_item_name.setText(friend_List.get(position).getName());   tv_item_tel.setText(friend_List.get(position).getTel());   return view;   //初始化这个listview会调用到这个方法,因为要把传进去的对象数组的每个元素转成view加入到listview中  }       } }

效果:

Android ListView列表视图的使用方法

android教程
脚本网站
android studio

脚本宝典总结

以上是脚本宝典为你收集整理的Android ListView列表视图的使用方法全部内容,希望文章能够帮你解决Android ListView列表视图的使用方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。