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

Android開(kāi)發(fā)之ListView、GridView 詳解及示例代碼

 更新時(shí)間:2016年08月30日 08:59:30   投稿:lqh  
本文主要介紹Android開(kāi)發(fā)之ListView、GridView,這里整理了相關(guān)資料及簡(jiǎn)單示例代碼,幫助大家學(xué)習(xí)參考,有需要的小伙伴可以參考下

    ListView與GridView是Android開(kāi)發(fā)中的常用控件,它們和Adapter配合使用能夠?qū)崿F(xiàn)很多界面效果。下面分別以實(shí)例說(shuō)明ListView、GridView的用法。

       1.ListView的Android開(kāi)發(fā)實(shí)例

       ListView 是android開(kāi)發(fā)中最常用的控件之一,一般構(gòu)成列表包括三個(gè)元素,ListView:用來(lái)展示列表的視圖、Adapter:數(shù)據(jù)與視圖連接的橋梁、Data:具體的數(shù)據(jù)包括字符串 、圖片或者控件。

       適配器一般有以下幾種類型:

       ArrayAdapter:Android中最簡(jiǎn)單的一種適配器,專門用于列表控件。只顯示一行數(shù)據(jù)。

       SimpleAdapter:此適配器有最好的擴(kuò)充性,可以自定義出各種效果。經(jīng)常使用靜態(tài)數(shù)據(jù)填充列表。

       CursorAdapter:通過(guò)游標(biāo)向列表提供數(shù)據(jù)。

       ResourceCursorAdapter:這個(gè)適配器擴(kuò)展了CursorAdapter,知道如何從資源創(chuàng)建視圖。

       SimpleCursorAdapter:這個(gè)適配器擴(kuò)展了ResourceCursorAdapter,從游標(biāo)中得列創(chuàng)建 TextView/ImageView視圖。下面獲取通訊錄的示例:

XML/HTML代碼

<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  android:orientation="vertical"   
  android:layout_width="fill_parent"   
  android:layout_height="fill_parent"   
  android:background="@drawable/bg"   
     
  >   
  <ListView    
  android:id="@+id/contacts_list"   
  android:layout_width="fill_parent"   
  android:layout_height="fill_parent"   
  >   
  </ListView>   
</LinearLayout>

Java代碼

package net.csdn.blog.androidtoast;   
   
import java.util.ArrayList;   
import java.util.HashMap;   
import java.util.Map;   
   
import android.app.Activity;   
import android.database.Cursor;   
import android.os.Bundle;   
import android.provider.ContactsContract;   
import android.view.View;   
import android.widget.AdapterView;   
import android.widget.ListAdapter;   
import android.widget.ListView;   
import android.widget.SimpleAdapter;   
import android.widget.Toast;   
   
public class MainActivity extends Activity {   
     
   ListView   mListView;   
   ArrayList<Map<String, String>> listData;   
        
  static final String NAME = "name";   
  static final String NUMBER = "number";   
  /** Called when the activity is first created. */   
  @Override   
  public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);   
    getContacts();   
  }   
   
  /**  
   * 獲取聯(lián)系人列表  
   */   
  private void getContacts() {   
    mListView=(ListView) findViewById(R.id.contacts_list);   
    listData = new ArrayList<Map<String, String>>();   
    //獲取數(shù)據(jù)庫(kù)Cursor   
    Cursor cur=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);   
    startManagingCursor(cur);   
    while (cur.moveToNext()) {   
     Map<String, String> mp = new HashMap<String, String>();   
     long id = cur.getLong(cur.getColumnIndex("_id"));   
     Cursor pcur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
         null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + Long.toString(id), null, null);   
            
     // 處理多個(gè)號(hào)碼的情況   
     String phoneNumbers = "";   
     while (pcur.moveToNext()) {   
        String strPhoneNumber = pcur.getString(pcur.getColumnIndex(   
           ContactsContract.CommonDataKinds.Phone.NUMBER));   
           phoneNumbers += strPhoneNumber + ":";   
     }   
        phoneNumbers += "\n";   
        pcur.close();   
               
        String name = cur.getString(cur.getColumnIndex("display_name"));   
        mp.put(NAME, name);   
        mp.put(NUMBER, phoneNumbers);   
        listData.add(mp);   
    }   
    cur.close();   
        
    // 建立一個(gè)適配器去查詢數(shù)據(jù)   
    ListAdapter adapter = new SimpleAdapter(this, listData, android.R.layout.simple_list_item_2,   
        new String[]{NAME, NUMBER},   
        new int[] {android.R.id.text1, android.R.id.text2});   
    mListView.setAdapter(adapter);   
    //為listView添加事件監(jiān)聽(tīng)   
    mListView.setOnItemSelectedListener(new ListView.OnItemSelectedListener(){   
      @Override   
      public void onItemSelected(AdapterView<?> parent, View view,   
          int position, long id) {   
        Toast.makeText(getApplicationContext(), "當(dāng)前所在行為:"+Long.toString(parent.getSelectedItemId()+1), 1).show();   
      }   
   
      @Override   
      public void onNothingSelected(AdapterView<?> parent) {   
        // TODO Auto-generated method stub   
           
      }   
         
    });   
  }   
}  

       2.GridView的Android開(kāi)發(fā)實(shí)例

       GridView 網(wǎng)格視圖,用于顯示多行多列。直接上示例:

XML/HTML代碼

<?xml version="1.0" encoding="utf-8"?>   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  android:orientation="vertical"   
  android:layout_width="fill_parent"   
  android:layout_height="fill_parent"   
  android:background="@drawable/bg"   
  >   
  <GridView   
    android:id="@+id/gridview"   
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:numColumns="3"/>   
       
</LinearLayout>  
XML/HTML代碼
<?xml version="1.0" encoding="utf-8"?>   
   
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  android:orientation="vertical" android:layout_width="fill_parent"   
  android:layout_height="fill_parent" android:scrollbars="vertical">   
   <ImageView     
        android:layout_height="100dip"     
        android:id="@+id/ItemImage"     
        android:layout_width="80dip"    
        android:src="@drawable/png1"    
        android:layout_gravity="center_horizontal"/>    
            
     <TextView     
        android:layout_width="wrap_content"     
        android:layout_height="wrap_content"    
        android:layout_gravity="center"    
        android:id="@+id/ItemText" />    
</LinearLayout>  

Java代碼

package net.csdn.blog.androidtoast;   
   
import java.util.ArrayList;   
import java.util.HashMap;   
   
import android.app.Activity;   
import android.os.Bundle;   
import android.view.Gravity;   
import android.view.View;   
import android.widget.AdapterView;   
import android.widget.GridView;   
import android.widget.SimpleAdapter;   
import android.widget.Toast;   
   
public class MainActivity extends Activity {   
  /** Called when the activity is first created. */   
  //定義圖片整型數(shù)組   
  private int[] mImages={   
      R.drawable.png1,   
      R.drawable.png2,   
      R.drawable.png3,   
      R.drawable.png4,   
      R.drawable.png5,   
      R.drawable.png6,   
      R.drawable.png7,   
      R.drawable.png8,   
      R.drawable.png9   
   
  };   
  @Override   
  public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);   
       
    //實(shí)例化GridView   
    GridView mGridView=(GridView) findViewById(R.id.gridview);   
    // 生成動(dòng)態(tài)數(shù)組,并且傳入數(shù)據(jù)   
    ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();   
       
    for (int i = 0; i < 9; i++) {   
      HashMap<String, Object> map = new HashMap<String, Object>();   
      map.put("ItemImage", mImages[i]);// 添加圖像資源的ID   
      map.put("ItemText", "NO." + String.valueOf(i+1));// 按序號(hào)做ItemText   
      lstImageItem.add(map);   
    }   
    //構(gòu)建一個(gè)適配器   
    SimpleAdapter simple = new SimpleAdapter(this, lstImageItem, R.layout.gridviewitem,   
        new String[] { "ItemImage", "ItemText" }, new int[] {R.id.ItemImage, R.id.ItemText });   
    mGridView.setAdapter(simple);   
    //添加選擇項(xiàng)監(jiān)聽(tīng)事件   
    mGridView.setOnItemClickListener(new GridView.OnItemClickListener(){  
      @Override   
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {   
        Toast toast=Toast.makeText(getApplicationContext(), "你選擇了"+(position+1)+"號(hào)圖片", 1);   
        toast.setGravity(Gravity.BOTTOM, 0, 0);   
        toast.show();   
      }   
         
    });   
       
  }   
} 

       看了這兩個(gè)Android開(kāi)發(fā)實(shí)例,相信大家對(duì)ListView、GridView的使用有了一定掌握了。大家還可以使用它們和Adapter實(shí)現(xiàn)更多的功能試試。

        以上就是對(duì)Android ListView 和GridView 的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Android編程實(shí)用技術(shù)小結(jié)

    Android編程實(shí)用技術(shù)小結(jié)

    這篇文章主要介紹了Android編程實(shí)用技術(shù),實(shí)例匯總了開(kāi)機(jī)啟動(dòng)receiver、service使用、AlarmManager發(fā)送廣播及停止AlarmManager等相關(guān)技巧,需要的朋友可以參考下
    2016-01-01
  • 打造酷炫的AndroidStudio插件

    打造酷炫的AndroidStudio插件

    這篇文章主要為大家詳細(xì)介紹了如何打造酷炫的AndroidStudio插件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android?Kotlin?中的groupBy方法詳解

    Android?Kotlin?中的groupBy方法詳解

    在Kotlin中,groupBy函數(shù)可以對(duì)集合進(jìn)行分組,形成一個(gè)Map,其中key是分組標(biāo)準(zhǔn),value是對(duì)應(yīng)的元素列表,本文通過(guò)實(shí)例詳細(xì)解釋groupBy的使用方法和常見(jiàn)應(yīng)用場(chǎng)景,如按員工年齡分組或產(chǎn)品類型統(tǒng)計(jì)數(shù)量等,展示了groupBy的靈活性和實(shí)用性
    2024-09-09
  • Android自定義EditText實(shí)現(xiàn)淘寶登錄功能

    Android自定義EditText實(shí)現(xiàn)淘寶登錄功能

    這篇文章主要為大家詳細(xì)介紹了Android自定義EditText實(shí)現(xiàn)淘寶登錄功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android RecyclerView網(wǎng)格布局(支持多種分割線)詳解(2)

    Android RecyclerView網(wǎng)格布局(支持多種分割線)詳解(2)

    這篇文章主要為大家詳細(xì)介紹了Android RecyclerView網(wǎng)格布局,支持多種分割線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • SDL2和OpenGL使用踩坑筆記經(jīng)驗(yàn)分享

    SDL2和OpenGL使用踩坑筆記經(jīng)驗(yàn)分享

    今天小編就為大家分享一篇關(guān)于SDL2和OpenGL使用踩坑筆記經(jīng)驗(yàn)分享,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • ScrollView嵌套ListView及ListView嵌套的高度計(jì)算方法

    ScrollView嵌套ListView及ListView嵌套的高度計(jì)算方法

    下面小編就為大家分享一篇ScrollView嵌套ListView及ListView嵌套的高度計(jì)算方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • Android 應(yīng)用適配 Android 7.0 權(quán)限要求詳解

    Android 應(yīng)用適配 Android 7.0 權(quán)限要求詳解

    今天小編就為大家分享一篇Android 應(yīng)用適配 Android 7.0 權(quán)限要求詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • Android實(shí)現(xiàn)輪播圖效果

    Android實(shí)現(xiàn)輪播圖效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Flutter實(shí)現(xiàn)微信朋友圈功能

    Flutter實(shí)現(xiàn)微信朋友圈功能

    這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)微信朋友圈功,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評(píng)論

大港区| 乌兰察布市| 紫阳县| 玉溪市| 鄂温| 根河市| 神池县| 都昌县| 新巴尔虎右旗| 茶陵县| 南靖县| 临城县| 昌都县| 千阳县| 水城县| 阿拉善左旗| 陆河县| 双城市| 南丹县| 尚志市| 天等县| 齐齐哈尔市| SHOW| 荆门市| 松桃| 丽水市| 隆林| 洪湖市| 噶尔县| 雷州市| 阿拉尔市| 石渠县| 黄平县| 红桥区| 霍州市| 屏南县| 区。| 文安县| 原平市| 吉林市| 奉贤区|