您的位置:首页 > 移动开发 > Android开发

Android-ArrayAdapter使用方法

2015-12-14 14:52 561 查看
一、简介:

比方说像那个“网易新闻客户端”中,从左往右滑动将会出现slidingmenu菜单,里面有各种各样的条目,实际上就是继承的ArrayAdapter实现的,因为继承ArrayAdapter实现的都是一些简单的文本内容的展示,是有限的内容,不像BaseAdapter那样需要实现的方法很多。所以这种情况下一般使用的是ArrayAdapter继承的,所以自己要注意了!

二、示例代码:

(这个程序的意思是将自定义的xml文件添加到listview控件中,使用的是ArrayAdapter控件)

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.ArrayAdapter;

import android.widget.ListView;

public class MainActivity extends Activity {

private static String[] arr={"汽车","美女","新闻","娱乐","八卦"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView lv=(ListView) findViewById(R.id.lv);
//使用谷歌工程师自己封装好的一个ArrayAdapter类对需要显示的数据进行操作 
//里面的参数分别是:上下文、需要关联的自定义xml文件、需要被遍历的内容
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.tv, arr));
}

}

这个是自定义以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" >

    <!-- 设置的是图片的引用 -->

    <ImageView 

        android:layout_width="50px"

    android:layout_height="50px"

    android:src="@drawable/p1"

   

        />

    

    <TextView 

        android:id="@+id/tv"

        android:layout_width="match_parent"

    android:layout_height="wrap_content"

        />

    

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息