android開(kāi)發(fā)教程之listview使用方法
首先是布局文件,這里需要兩個(gè)布局文件,一個(gè)是放置列表控件的Activity對(duì)應(yīng)的布局文件 main.xml,另一個(gè)是ListView中每一行信息顯示所對(duì)應(yīng)的布局 list_item.xml 這一步需要注意的問(wèn)題是ListView 控件的id要使用Android系統(tǒng)內(nèi)置的 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>
然后就設(shè)置MainActivity中的代碼了:基本思想就是先將數(shù)據(jù)添加到ArrayList中,然后在設(shè)置SimpleAdapter適配器完成設(shè)置,入下:
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顯示內(nèi)容每一列的列名
int[] to={R.id.user_name,R.id.user_id}; //這里是ListView顯示每一列對(duì)應(yīng)的list_item中控件的id
String[] userName={"zhangsan","lisi","wangwu","zhaoliu"}; //這里第一列所要顯示的人名
String[] userId={"1001","1002","1003","1004"}; //這里是人名對(duì)應(yīng)的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設(shè)置主布局
//創(chuàng)建ArrayList對(duì)象;
list=new ArrayList<HashMap<String,String>>();
//將數(shù)據(jù)存放進(jìn)ArrayList對(duì)象中,數(shù)據(jù)安排的結(jié)構(gòu)是,ListView的一行數(shù)據(jù)對(duì)應(yīng)一個(gè)HashMap對(duì)象,
//HashMap對(duì)象,以列名作為鍵,以該列的值作為Value,將各列信息添加進(jìn)map中,然后再把每一列對(duì)應(yīng)
//的map對(duì)象添加到ArrayList中
for(int i=0; i<4; i++){
map=new HashMap<String,String>(); //為避免產(chǎn)生空指針異常,有幾列就創(chuàng)建幾個(gè)map對(duì)象
map.put("id", userId[i]);
map.put("name", userName[i]);
list.add(map);
}
//創(chuàng)建一個(gè)SimpleAdapter對(duì)象
SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.list_item,from,to);
//調(diào)用ListActivity的setListAdapter方法,為L(zhǎng)istView設(shè)置適配器
setListAdapter(adapter);
}
}

另外對(duì)點(diǎn)擊某一行作出響應(yīng)的方法是覆寫(xiě)onListItemClick方法,根據(jù)返回的position(從0開(kāi)始):
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
}
- Android動(dòng)態(tài)繪制餅狀圖的示例代碼
- 手把手教你用Android自定義餅狀圖
- 安卓(Android)開(kāi)發(fā)之自定義餅狀圖
- MPAndroidChart開(kāi)源圖表庫(kù)的使用介紹之餅狀圖、折線(xiàn)圖和柱狀圖
- android TextView設(shè)置中文字體加粗實(shí)現(xiàn)方法
- android listview優(yōu)化幾種寫(xiě)法詳細(xì)介紹
- android WebView加載html5介紹
- android imageview圖片居中技巧應(yīng)用
- Android TextView設(shè)置背景色與邊框的方法詳解
- Android使用自定義View實(shí)現(xiàn)餅狀圖的實(shí)例代碼
相關(guān)文章
Android進(jìn)階Hook攔截系統(tǒng)實(shí)例化View過(guò)程實(shí)現(xiàn)App換膚功能
這篇文章主要為大家介紹了Android進(jìn)階Hook攔截系統(tǒng)實(shí)例化View過(guò)程實(shí)現(xiàn)App換膚功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android Activity中onStart()和onResume()的區(qū)別分析
這篇文章主要介紹了Android Activity中onStart()和onResume()的區(qū)別,結(jié)合Activity的四種狀態(tài)簡(jiǎn)單分析了Android Activity中onStart()和onResume()方法的作用,并補(bǔ)充說(shuō)明了Activity中六個(gè)常用函數(shù),需要的朋友可以參考下2016-01-01Android百度地圖定位、顯示用戶(hù)當(dāng)前位置
這篇文章主要為大家詳細(xì)介紹了Android百度地圖定位、顯示用戶(hù)當(dāng)前位置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android實(shí)現(xiàn)滑動(dòng)屏幕切換圖片
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)滑動(dòng)屏幕切換圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08詳解Android過(guò)濾emoji表情正則表達(dá)式
這篇文章主要介紹了Android過(guò)濾emoji表情正則表達(dá)式,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06Android 代碼一鍵實(shí)現(xiàn)銀行卡綁定功能
這篇文章主要介紹了Android 代碼一鍵實(shí)現(xiàn)銀行卡綁定功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android4.4新增函數(shù)訪(fǎng)問(wèn)外部存儲(chǔ)
這篇文章主要介紹了Android4.4新增函數(shù)訪(fǎng)問(wèn)外部存儲(chǔ)的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10