惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - sayo.net

website the breakpoint will not currently be hit. no symbols have been loaded for this document How to add MS Test to post build 在WCF中使用REST时,如何去掉URL中的.svc后缀 Resolve Error: The service cannot be activated because it does not support ASP.NET compatibility. Resolve Error: Service has zero application (non-infrastructure) endpoints. WCF Could not establish trust relationship for the SSL/TLS secure channel with authority Could not find a base address that matches scheme http for the endpoint with binding MetadataExchangeHttpBinding. Resolve error: Could Not Load Type ‘System.ServiceModel.Activation.HttpModule’ 在IIS7.5上运行Message Security with UserName的WCF Service WCF部署问题两则 SQL Server Mangement Studio无法创建Database Dagrams的简单解决办法 一些有用的WCF相关链接 让Android的emulator支持web camera 如何旋转Android Emulator HTC Hero Android 2.1 真机调试 HTC Hero Android 2.1 真机调试 CouchDB系列 - 安装CouchDB Test from Windows Live Writer Silverlight Toolkit初探
[Android开发]如何自定义ArrayAdapter
sayo.net · 2010-12-30 · via 博客园 - sayo.net

场景:

我想显示sd卡的/DCIM/Camera目录中的所有图片到一个列表中,这个列表中的每一项左边是图片的缩略图,右边是文件名。

分析:

这个应用是很典型的 数据绑定自定义显示.

如何搞定?

1. 自定义列表样式

image_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="horizontal"> 
<ImageView 
    android:id="@+id/item_thumbnail" 
    android:layout_height="48dip" 
    android:layout_width="48dip" 
        /> 
<TextView 
    android:id="@+id/item_file_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
    android:paddingLeft="5dip" 
    />
</LinearLayout>

2. 自定义ArrayAdapter

ImageListAdapter

public class ImageListAdapter extends ArrayAdapter<File>{ 
    private int resource; 
    public ImageListAdapter(Context context, int resourceId, List<File> objects) { 
        super(context, resourceId, objects); 
        // 记录下来稍后使用 
        resource = resourceId; 
    }

    public View getView(int position, View convertView, ViewGroup parent) { 
        LinearLayout imageListView; 
        // 获取数据 
        File file = getItem(position); 
        String fileName = file.getName(); 
        Bitmap bitmap = getBitmapFromFile(file);

        // 系统显示列表时,首先实例化一个适配器(这里将实例化自定义的适配器)。 
        // 当手动完成适配时,必须手动映射数据,这需要重写getView()方法。 
        // 系统在绘制列表的每一行的时候将调用此方法。 
        // getView()有三个参数, 
        // position表示将显示的是第几行, 
        // covertView是从布局文件中inflate来的布局。 
        // 我们用LayoutInflater的方法将定义好的image_item.xml文件提取成View实例用来显示。 
        // 然后将xml文件中的各个组件实例化(简单的findViewById()方法)。 
        // 这样便可以将数据对应到各个组件上了。 
        // 
        if(convertView == null) { 
            imageListView = new LinearLayout(getContext()); 
            // 看一下android文档中关于LayoutInflater的定义吧 
            // This class is used to instantiate layout XML file into its corresponding View objects. 
            // It is never be used directly -- use getLayoutInflater() or getSystemService(String) 
            // to retrieve a standard LayoutInflater instance that is already hooked up to the current 
            // context and correctly configured for the device you are running on. . For example: 
            String inflater = Context.LAYOUT_INFLATER_SERVICE; 
            LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater); 
            vi.inflate(resource, imageListView, true); 
            // 这个每次select images时都被调用 
            Log.d("Adapter", "convertView is null now"); 
        } else { 
            // 很奇怪基本没被调用过 
            imageListView = (LinearLayout)convertView; 
            Log.d("Adapter", "convertView is not null now"); 
        }

        // 填充自定义数据 
        ImageView imageView = (ImageView) imageListView.findViewById(R.id.item_thumbnail); 
        TextView textView = (TextView) imageListView.findViewById(R.id.item_file_name); 
        textView.setText(fileName); 
        imageView.setImageBitmap(bitmap);

        return imageListView; 
    }

    // 从文件获取Bitmap用于填充 
    private Bitmap getBitmapFromFile(File file) { 
        Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); 
        return  bitmap; 
    } 
}

3. 绑定数据

private void bindFilesToList(File[] files) { 
        List<File> fileList = new ArrayList<File>(); 
        for(File file : files) { 
            fileList.add(file); 
        }

        ImageListAdapter adapter = new ImageListAdapter(ImageFilesListActivity.this, 
                                                        R.layout.image_item, 
                                                        fileList); 
        setListAdapter(adapter); 
}

基本就是这样了Smile

image