Adapter接口及實(shí)現(xiàn)類(lèi)

xml代碼

listview_layout.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" >

? ? <!-- 直接使用數(shù)組資源給出列表項(xiàng),設(shè)置使用紅色分割條 -->

? ? <!--

? ? <ListView

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"

? ? android:entries="@array/love"

? ? android:divider="#f00"

? ? android:dividerHeight="2px"

? ? android:headerDividersEnabled="false"></ListView>

? ? -->

? ? <!-- 使用紅色分割條 -->

? ? <ListView

? ? ? ? android:id="@+id/list1"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:divider="#f00"

? ? ? ? android:dividerHeight="2px"

? ? ? ? android:headerDividersEnabled="false" />

? ? <!-- 使用綠色分隔條 -->

? ? <ListView

? ? ? ? android:id="@+id/list2"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:divider="#0f0"

? ? ? ? android:dividerHeight="2px"

? ? ? ? android:headerDividersEnabled="false" >

? ? </ListView>

</LinearLayout>

***array_item.xml***

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"

? ? android:id="@+id/TextView"

? ? android:textSize="24dp"

? ? android:padding="10px"

? ? android:shadowColor="#f0f"

? ? android:shadowDx="4"

? ? android:shadowDy="4"

? ? android:shadowRadius="2"

? ? >


</TextView>

***checked_item.xml***

<?xml version="1.0" encoding="utf-8"?>

<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="wrap_content"

? ? android:shadowColor="#00f" >

</CheckBox>

JAVA實(shí)現(xiàn)代碼

package com.example.lovemia;

import android.R.string;

import android.app.Activity;

import android.app.ActionBar;

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.os.Build;

public class MiaActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// setContentView(R.layout.activity_mia);

setContentView(R.layout.listview_layout);

ListView li = (ListView) findViewById(R.id.list1);

// 定義一個(gè)數(shù)組

String[] str1 = new String[] { "mia", "love mia", "princess", "my love" };

// 將數(shù)組包裝為ArrayAdapter

ArrayAdapter<String> aa = new ArrayAdapter<String>(this,

R.layout.array_item, str1);

// 為L(zhǎng)istView設(shè)置Adapter

li.setAdapter(aa);

ListView li2 = (ListView) findViewById(R.id.list2);

// 定義數(shù)組

String[] str2 = new String[] { "010802", "love", "mia", "miss" };

// 將數(shù)組包裝為ArrayAdapter

ArrayAdapter<String> aa2 = new ArrayAdapter<String>(this,

R.layout.checked_item, str2);

// 為listView設(shè)置Adapter

li2.setAdapter(aa2);

}

}

界面效果

![在這里插入圖片描述](https://img-blog.csdnimg.cn/2cb96abeeb1d4025972a876487cdd0b3.png)

// 基于ListActivity實(shí)現(xiàn)列表 不使用xml文件

public class MiaActivity extends ListActivity{

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

// 注意這個(gè)是在只有一個(gè)listview的情況下可以使用的

//無(wú)需使用布局文件

String[] str3=new String[]{

"mia","miss mia","love mia","010802"

};

//創(chuàng)建ArrayAdapter對(duì)象

ArrayAdapter<String> aa3=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,str3);

//設(shè)置窗口顯示列表

setListAdapter(aa3);

}

}

界面效果

![在這里插入圖片描述](https://img-blog.csdnimg.cn/39d9ff403bb649078991097192c8fbf6.png)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容