麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > Android > 正文

Android ListView列表視圖的使用方法

2019-10-21 21:26:41
字體:
來源:轉載
供稿:網友

前言

當你要將某個從數據庫或者文件中獲得相當大的數據,在界面中向用戶展示的時候,由于定義一個個視圖比較麻煩,Android中提供了類似于數組的控件–ListView。

使用方法:

假設我們要轉的數據是一個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的模板

<?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,列表視圖

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 懂色av懂色aⅴ精彩av | 成人男女啪啪免费观看网站四虎 | 日韩字幕在线观看 | 一级在线免费观看视频 | 手机av免费电影 | asian超清日本肉体pics | aaaaa国产欧美一区二区 | 青青草华人在线 | a黄在线观看 | 一分钟免费观看完整版电影 | 色播亚洲 | 青青操精品 | 日韩黄色免费电影 | 亚洲第一精品在线 | 欧美精品一区二区性色 | 欧美一级黄色免费看 | 91视频久久 | av电影在线播放 | 五月天影院,久久综合, | 98国内自拍在线视频 | 中文字幕天堂在线 | 蜜桃精品视频 | 午夜看毛片 | 久久精品性视频 | 黄色片网站在线免费观看 | 视频一区 在线 | 欧美日韩免费在线观看视频 | 九色在线78m | 久久中出 | 国产剧情在线观看一区二区 | 91av资源在线 | 神秘电影91| 日本爽快片100色毛片视频 | 三人弄娇妻高潮3p视频 | 国产又粗又爽又深的免费视频 | 永久免费黄色大片 | 黄色免费小网站 | 亚洲午夜精品视频 | 激情小说色 | 欧美aaa| 亚洲国产视频网 |