在这里我们新创建一个任务来演示

  然后再布局文件中定义一个ListView 来当作显示数据的容器,布局文件代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ListView
android:id="@+id/listShow"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>

我们先来看一个简单的演示:

先贴出运行时的效果图

主Activity中的代码如下,在这里我们可以看到我们使用了系统的预留布局

android.R.layout.simple_expandable_list_item_1
    private ListView ls;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ls= (ListView)findViewById(R.id.listShow);
ls.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getArray()){
});
}
private ArrayList<String> getArray()
{
ArrayList<String> s=new ArrayList<String>();
for(int i=0;i<;i++)
{
s.add(String.valueOf(i));
} return s;
}

  可是这样显示往往不能满足我们的需求,我们需要自定义的布局

<?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" > <TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" /> </LinearLayout>

那我们需要使用自己的布局文件,那么我们肯定是要自己写Adapter的啦,那么问题来了,怎么写?

private class MyAdapter extends ArrayAdapter<String>
{ public MyAdapter(ArrayList<String> ay) {
super(MainActivity.this, 0,ay);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=getLayoutInflater().inflate(R.layout.myls,null);
}
String s=getItem(position);
TextView tx=(TextView)convertView.findViewById(R.id.tv_show);
tx.setText(s);
return convertView;
}
}

当然我们也可以使用BaseAdapter 来实现

Android ArrayAdpater 填充集合的更多相关文章

  1. Android面试题集合

    https://www.jianshu.com/p/718aa3c1a70b https://www.jianshu.com/p/2dd855aa1938 https://www.jianshu.co ...

  2. Android视图动画集合AndoridViewAnimations

    Android视图动画集合AndoridViewAnimations Android视图动画是针对视图对象的动画效果,包括对象平移.旋转.缩放和渐变四种类型.通过组合这四种类型,可以创建出无数种动画效 ...

  3. fir.im Weekly - 1000 个 Android 开源项目集合

    冬天到了,适宜囤点代码暖暖身.本期 fir.im Weekly 收集了最近一些不错的 GitHub 源码.开发工具和技术实践教程类文章分享给大家. codeKK - 集合近 1000 Android ...

  4. Android开发贴士集合

    Activity.startActivities()——对于从app流的中部启动会非常好. TextUtils.isEmpty()——一个普遍适用的简单工具类. Html.fromHtml()——格式 ...

  5. android自动填充短信验证码

    自动拦截短信实际上就是在系统注册一个BroadcastReceiver,然后通过设置拦截短信的: filter.addAction("android.provider.Telephony.S ...

  6. Android数据填充器LayoutInflater

    LayoutInflater类在应用程序中比较实用,可以叫布局填充器,也可以成为打气筒,意思就是将布局文件填充到自己想要的位置,LayoutInflater是用来找res/layout/下的xml布局 ...

  7. Android布局属性集合

    <!-- android:id  —— 为控件指定相应的ID android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串 andro ...

  8. Android 开发问题集合

    1.屏幕横.竖切换 修改“AndroidManifest.xml”的android:screenOrientation 一般需要:landscape.portrait 2.修改应用名字 修改“Andr ...

  9. Android常用代码集合

    这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中. 1:调用浏览器,载入某网址 1 2 3 Uri uri = Uri.parse("http://www.android-st ...

随机推荐

  1. Oracle中的触发器

    创建触发器的语法: Create trigger 触发器的名字 after insert/update/delete/(select是没有触发器的) on 表名字 declare begin dbms ...

  2. 在安装MySQL Workbentch的时候出现如下问题,已经解决。

    mysql workbench cannot be executed from a path that contains non-ASCII characters. this problem is i ...

  3. float([x]): 将一个字符串或数转换为浮点数。如果无参数将返回0.0

    float([x]): 将一个字符串或数转换为浮点数.如果无参数将返回0.0 >>> float(12) 12.0 >>> float(-122) -122.0 & ...

  4. 关于最大流的EdmondsKarp算法详解

    最近大三学生让我去讲课,我就恶补了最大流算法,笔者认为最重要的是让学弟学妹们入门,知道算法怎么来的?为什么是这样?理解的话提出自己的改进,然后再看看Dinic.SAP和ISAP算法….. 一.概念引入 ...

  5. List中toArray()的使用方法

    当我们需要把一个链表中的元素放入数组时,jdk给我们提供了一种方法,也即运用toArray(),方法的使用如下: public class Test { public static void main ...

  6. Memcached总结一:memcached简介及适用和不适应场景

    Memcached是免费的,开源的,高性能的,分布式内存对象的缓存系统(键/值字典),旨在通过减轻数据库负载加快动态Web应用程序的使用. Memcached是由布拉德·菲茨帕特里克(Brad Fit ...

  7. [codility]Array-closest-ascenders

    http://codility.com/demo/take-sample-test/pi2012 又是一道单调栈的题目.首先这道题目n^2是最朴素的做法.继续优化,因为和顺序有关就不好排序.然后,看到 ...

  8. SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值

    1.When injecting properties and constructor arguments on beans that are created via component-scanni ...

  9. 146. LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  10. input checkbox问题和li里面包含checkbox

    <input type="checkbox" id="checkbox1"/> $("input#checkbox1").cli ...