這篇文章給大家介紹如何在Android中使用ListView列表視圖,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
鐘山網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,鐘山網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為鐘山成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營(yíng)銷網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的鐘山做網(wǎng)站的公司定做!
使用方法:
假設(shè)我們要轉(zhuǎn)的數(shù)據(jù)是一個(gè)Person對(duì)象數(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 "點(diǎn)擊的聯(lián)系人為" + this.getName() +"\n電話號(hào)碼為" + this.getTel(); } }
Person[] DB = { new Person("張三","18555555555"), new Person("李四","18555555556"), new Person("王五","18555555557"), new Person("趙六","18555555558"), new Person("鄧七","18555555559") }; Listfriend_List; friend_List = new ArrayList (); for(int i=0;i 1、首先,你需要在xml中加入一個(gè)listview控件:
2、接著你需要?jiǎng)?chuàng)建一個(gè)適配器MyAdapter類,這個(gè)適配器的作用時(shí)將你要展示的數(shù)據(jù)轉(zhuǎn)成可見(jiàn)格式也就時(shí)View。
class MyAdapter extends BaseAdapter { @Override public int getCount() { //返回表的長(zhǎng)度 // 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) { //就像等到一個(gè)對(duì)象數(shù)組的某一個(gè)元素 // 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; //初始化這個(gè)listview會(huì)調(diào)用到這個(gè)方法,因?yàn)橐褌鬟M(jìn)去的對(duì)象數(shù)組的每個(gè)元素轉(zhuǎn)成view加入到listview中 } }3、然后要在xml中寫下你要轉(zhuǎn)成的view的模板
4、最后在MainActivity中把listview的適配器設(shè)置一下。調(diào)用setAdapter這個(gè)方法
data_view.setAdapter(myAdapter);Android代碼:
xml: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 { Listfriend_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 (); for(int i=0;i 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() { //返回表的長(zhǎng)度 // 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) { //就像等到一個(gè)對(duì)象數(shù)組的某一個(gè)元素 // 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; //初始化這個(gè)listview會(huì)調(diào)用到這個(gè)方法,因?yàn)橐褌鬟M(jìn)去的對(duì)象數(shù)組的每個(gè)元素轉(zhuǎn)成view加入到listview中 } } } 關(guān)于如何在Android中使用ListView列表視圖就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享標(biāo)題:如何在Android中使用ListView列表視圖
網(wǎng)頁(yè)網(wǎng)址:http://weahome.cn/article/gjoogg.html