日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AdapterView及其子类之三:基于ListView及ArrayAdapter实现列表

發(fā)布時間:2024/1/23 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AdapterView及其子类之三:基于ListView及ArrayAdapter实现列表 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

見歸檔項目ListViewDemo.zip.

基本步驟如下:

1、創(chuàng)建主布局文件,里面包含一個ListView元素。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><ListView android:id="@+id/listview"android:layout_height="wrap_content"android:layout_width="match_parent"android:divider="#00ff00"android:dividerHeight="2dp"android:headerDividersEnabled="false"/></RelativeLayout>
2、 創(chuàng)建一個TextView,用于指定每一個選項的格式

<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/list"android:layout_width="match_parent"android:layout_height="20dp" android:background="#0000ff"></TextView> 3、創(chuàng)建主類:

package com.ljh.listviewdemo;import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import android.app.Activity;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// (1)創(chuàng)建要顯示的文本內(nèi)容String[] arr = { "java", "c/c++", "python", "ruby" };//(2)與使用ListActivity的最大區(qū)別:使用findViewById得到一個ListViewListView lv = (ListView) findViewById(R.id.listview);// (3)創(chuàng)建ArrayAdapter,其中第二個參數(shù)resource:The resource ID for a layout file// containing a TextView to use when instantiating views.是要以一個layout作為// 參數(shù),且此layout需要包含textview。 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list, arr);// (4)為ListActivity設置adapter.lv.setAdapter(adapter);lv.setOnItemClickListener(new OnItemClickListener(){//定義當某個選項被點擊時的操作。@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {Toast.makeText(MainActivity.this, position+" item is clicked.", Toast.LENGTH_LONG).show();}});}}


使用ListActivity或ListView創(chuàng)建列表的區(qū)別:

1、用途區(qū)別:二者都只能支持TextView類型,更強大的支持能力請見SimpleAdapter。

ListActivity只能支持創(chuàng)建單個列表,而ListView可在同一頁面創(chuàng)建多個列表,只要在布局文件中定義多個ListView元素即可。

2、創(chuàng)建時的區(qū)別:

ListActivity方法是繼承了ListActivity,從而可以直接調(diào)用setListAdapter()及onListItemClick().

ListView方法必須通過findViewById()得到一個ListView,然后再通過其調(diào)用setListAdapter(),以及設置OfnListItemClick接口。



總結

以上是生活随笔為你收集整理的AdapterView及其子类之三:基于ListView及ArrayAdapter实现列表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。