最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android開發(fā)教程之listview使用方法

 更新時(shí)間:2014年02月11日 11:29:51   作者:  
這篇文章主要介紹了android的listview使用方法,需要的朋友可以參考下

首先是布局文件,這里需要兩個(gè)布局文件,一個(gè)是放置列表控件的Activity對(duì)應(yīng)的布局文件 main.xml,另一個(gè)是ListView中每一行信息顯示所對(duì)應(yīng)的布局  list_item.xml    這一步需要注意的問題是ListView 控件的id要使用Android系統(tǒng)內(nèi)置的 android:id="@android:id/list"   [注意形式]

main.xml

復(fù)制代碼 代碼如下:

<?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

復(fù)制代碼 代碼如下:

<?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è)置,入下:

復(fù)制代碼 代碼如下:

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方法,為ListView設(shè)置適配器
        setListAdapter(adapter);       
    }
}



另外對(duì)點(diǎn)擊某一行作出響應(yīng)的方法是覆寫onListItemClick方法,根據(jù)返回的position(從0開始):

復(fù)制代碼 代碼如下:

@Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  // TODO Auto-generated method stub
  super.onListItemClick(l, v, position, id);
 }

相關(guān)文章

  • android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能

    android MediaRecorder實(shí)現(xiàn)錄屏?xí)r帶錄音功能

    這篇文章主要介紹了android MediaRecorder錄屏?xí)r帶錄音功能實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Android第三方文件選擇器aFileChooser使用方法詳解

    Android第三方文件選擇器aFileChooser使用方法詳解

    這篇文章主要介紹了Android第三方文件選擇器aFileChooser的使用方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • android實(shí)現(xiàn)篩選菜單效果

    android實(shí)現(xiàn)篩選菜單效果

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)篩選菜單效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Android實(shí)現(xiàn)自動(dòng)截圖腳本

    Android實(shí)現(xiàn)自動(dòng)截圖腳本

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自動(dòng)截圖腳本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android仿微信發(fā)朋友圈瀏覽圖片效果

    Android仿微信發(fā)朋友圈瀏覽圖片效果

    這篇文章主要介紹了Android仿微信發(fā)朋友圈瀏覽圖片效果的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Android中自定義標(biāo)題欄樣式的兩種方法

    Android中自定義標(biāo)題欄樣式的兩種方法

    這篇文章主要介紹了Android中自定義標(biāo)題欄樣式的兩種方法,同時(shí)講解了自定義標(biāo)題欄布局的實(shí)現(xiàn),需要的朋友可以參考下
    2014-07-07
  • Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例

    Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例

    這篇文章主要介紹了Android ContentProvider獲取手機(jī)聯(lián)系人實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • 全面解析Android之ANR日志

    全面解析Android之ANR日志

    不論從事安卓應(yīng)用開發(fā),還是安卓系統(tǒng)研發(fā),應(yīng)該都遇到應(yīng)用無響應(yīng)(簡稱ANR)問題,當(dāng)應(yīng)用程序一段時(shí)間無法及時(shí)響應(yīng),則會(huì)彈出ANR對(duì)話框,讓用戶選擇繼續(xù)等待,還是強(qiáng)制關(guān)閉。本文將帶你全面解析Android之ANR日志
    2021-06-06
  • Android 解決監(jiān)聽home鍵的幾種方法

    Android 解決監(jiān)聽home鍵的幾種方法

    這篇文章主要介紹了Android 解決監(jiān)聽home鍵的幾種方法的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android實(shí)現(xiàn)可收縮和擴(kuò)展的TextView

    Android實(shí)現(xiàn)可收縮和擴(kuò)展的TextView

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可收縮和擴(kuò)展的TextView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03

最新評(píng)論

大宁县| 融水| 枣强县| 博兴县| 泸定县| 湘西| 钟祥市| 宜君县| 潼关县| 吉安市| 宜宾县| 惠州市| 西盟| 吴忠市| 闸北区| 定远县| 富裕县| 新河县| 久治县| 探索| 轮台县| 浦北县| 厦门市| 玉龙| 库车县| 历史| 关岭| 云浮市| 仙居县| 东城区| 麻江县| 林甸县| 长武县| 虹口区| 大方县| 奉贤区| 上思县| 昌平区| 长泰县| 定结县| 赤壁市|