欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

如何在Android中使用ListView列表視圖

這篇文章給大家介紹如何在Android中使用ListView列表視圖,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

鐘山網(wǎng)站建設公司創(chuàng)新互聯(lián)公司,鐘山網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為鐘山成百上千家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設要多少錢,請找那個售后服務好的鐘山做網(wǎng)站的公司定做!

使用方法:

假設我們要轉(zhuǎn)的數(shù)據(jù)是一個Person對象數(shù)組

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 "點擊的聯(lián)系人為" + 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、接著你需要創(chuàng)建一個適配器MyAdapter類,這個適配器的作用時將你要展示的數(shù)據(jù)轉(zhuǎn)成可見格式也就時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) { //就像等到一個對象數(shù)組的某一個元素
  // 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會調(diào)用到這個方法,因為要把傳進去的對象數(shù)組的每個元素轉(zhuǎn)成view加入到listview中
 }
   
  }

3、然后要在xml中寫下你要轉(zhuǎn)成的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的適配器設置一下。調(diào)用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="聯(lián)系電話"
     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) { //就像等到一個對象數(shù)組的某一個元素
  // 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會調(diào)用到這個方法,因為要把傳進去的對象數(shù)組的每個元素轉(zhuǎn)成view加入到listview中
 }
   
  }
}

關于如何在Android中使用ListView列表視圖就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

本文題目:如何在Android中使用ListView列表視圖
當前網(wǎng)址:http://aaarwkj.com/article16/gjoogg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)頁設計公司網(wǎng)站內(nèi)鏈、App開發(fā)服務器托管、App設計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務器托管
亚洲一区二区三区免费在线视频| 日韩精品精美视频在线观看| 日韩免费视频一区二区三区免费 | 91九色国产在线播放| 国产精品免费观看在线国产 | 中文字幕加勒比东京热| 日韩专区欧美二区国产| 国产成人亚洲精品乱码| 欧美日韩在线不卡成人| 亚洲国产成人av精品精品国产自 | 精品久久人妻中文字幕免费| 最新中文字幕人妻少妇| 国产精品网站在线观看| 亚洲日本韩国在线免费| 亚洲午夜一区二区精品| 久亚洲精品色婷婷国产熟女| 久久精品国产av极品| 国自产偷精品不卡在线| 国产三级黄在线观看| 日韩三级在线观看av| 国产亚洲一区二区三区在线| 欧美精品在线高清观看| 日韩在线一区二区三区电影| 国产粉嫩一区二区三区在线观看 | 青青青在线视频观看华人| 久久久精品人妻免费网站| 国产精品99久久久久久| 黑人巨大欧美一区二区| 午夜激情视频在线网站| 色中文字幕人妻诱惑制服| 日韩精品中文乱码在线观看| 九九九热精品在线视频观看| av免费观看男人的天堂| 久久夜色精品国产高清不卡| 国产亚洲欧美精品久久久久| 日韩少妇人妻一区二区| 国产精品剧情在线播放| 亚洲综合久久五月天| 九九热在线视频观看最新| 久久婷婷综合激情亚洲| 精品少妇人妻久久av免费|