android開發教程之listview使用方法
首先是佈局文件,這裡需要兩個佈局文件,一個是放置列表控制項的Activity對應的佈局文件main.xml,另一個是ListView中每一行資訊顯示所對應的佈局 list_item.xml 這一步驟需要注意的問題是ListView 控制項的id要使用Android系統內建的android:id="@android:id/list" [注意形式]
main.xml
<?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="vertical" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="20dip"/> </LinearLayout>
list_item.xml
<?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/user_name" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:id="@+id/user_id" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout>
然後就設定MainActivity中的程式碼了:基本想法就是先將資料加入ArrayList中,然後在設定SimpleAdapter適配器完成設置,入下:
package com.example.android_newlistview; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.os.Bundle; import android.app.Activity; import android.app.ListActivity; import android.view.Menu; import android.widget.SimpleAdapter; public class MainActivity extends ListActivity { String[] from={"name","id"}; //这里是ListView显示内容每一列的列名 int[] to={R.id.user_name,R.id.user_id}; //这里是ListView显示每一列对应的list_item中控件的id String[] userName={"zhangsan","lisi","wangwu","zhaoliu"}; //这里第一列所要显示的人名 String[] userId={"1001","1002","1003","1004"}; //这里是人名对应的ID ArrayList<HashMap<String,String>> list=null; HashMap<String,String> map=null; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); //为MainActivity设置主布局 //创建ArrayList对象; list=new ArrayList<HashMap<String,String>>(); //将数据存放进ArrayList对象中,数据安排的结构是,ListView的一行数据对应一个HashMap对象, //HashMap对象,以列名作为键,以该列的值作为Value,将各列信息添加进map中,然后再把每一列对应 //的map对象添加到ArrayList中 for(int i=0; i<4; i++){ map=new HashMap<String,String>(); //为避免产生空指针异常,有几列就创建几个map对象 map.put("id", userId[i]); map.put("name", userName[i]); list.add(map); } //创建一个SimpleAdapter对象 SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.list_item,from,to); //调用ListActivity的setListAdapter方法,为ListView设置适配器 setListAdapter(adapter); } }
另外對點擊某一行作出回應的方法是覆寫onListClick方法,根據返回的position(從0開始):
@Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); }
更多android開發教學listview使用方法相關文章請關注PHP中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...
