亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2014年02月11日 11:29:51   作者:  
這篇文章主要介紹了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設置主布局
        //創(chuàng)建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>();       //為避免產生空指針異常,有幾列就創(chuàng)建幾個map對象
            map.put("id", userId[i]);
            map.put("name", userName[i]);
            list.add(map);
        }

        //創(chuàng)建一個SimpleAdapter對象
        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.list_item,from,to);
        //調用ListActivity的setListAdapter方法,為ListView設置適配器
        setListAdapter(adapter);       
    }
}



另外對點擊某一行作出響應的方法是覆寫onListItemClick方法,根據返回的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進階Hook攔截系統實例化View過程實現App換膚功能

    Android進階Hook攔截系統實例化View過程實現App換膚功能

    這篇文章主要為大家介紹了Android進階Hook攔截系統實例化View過程實現App換膚功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • Android Activity中onStart()和onResume()的區(qū)別分析

    Android Activity中onStart()和onResume()的區(qū)別分析

    這篇文章主要介紹了Android Activity中onStart()和onResume()的區(qū)別,結合Activity的四種狀態(tài)簡單分析了Android Activity中onStart()和onResume()方法的作用,并補充說明了Activity中六個常用函數,需要的朋友可以參考下
    2016-01-01
  • 詳解Android的四大應用程序組件

    詳解Android的四大應用程序組件

    這篇文章主要介紹了Android的應用程序組件的相關資料,幫助大家更好的理解和使用Android,感興趣的朋友可以了解下
    2021-01-01
  • Android百度地圖定位、顯示用戶當前位置

    Android百度地圖定位、顯示用戶當前位置

    這篇文章主要為大家詳細介紹了Android百度地圖定位、顯示用戶當前位置,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Android自定view畫圓效果

    Android自定view畫圓效果

    這篇文章主要為大家詳細介紹了Android自定view畫圓效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android實現滑動屏幕切換圖片

    Android實現滑動屏幕切換圖片

    這篇文章主要為大家詳細介紹了Android實現滑動屏幕切換圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 詳解Android之圖片加載框架Fresco基本使用(二)

    詳解Android之圖片加載框架Fresco基本使用(二)

    本篇文章主要介紹了Android之圖片加載框架Fresco基本使用,可以實現進度條和圖片縮放等內容,有興趣的可以了解一下。
    2016-12-12
  • 詳解Android過濾emoji表情正則表達式

    詳解Android過濾emoji表情正則表達式

    這篇文章主要介紹了Android過濾emoji表情正則表達式,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • Android 代碼一鍵實現銀行卡綁定功能

    Android 代碼一鍵實現銀行卡綁定功能

    這篇文章主要介紹了Android 代碼一鍵實現銀行卡綁定功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Android4.4新增函數訪問外部存儲

    Android4.4新增函數訪問外部存儲

    這篇文章主要介紹了Android4.4新增函數訪問外部存儲的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-10-10

最新評論