Android中ArrayAdapter的使用

本文详细介绍了Android中ArrayAdapter的使用方法,包括基本构造函数的解析,通过三种不同的示例展示了如何根据实际需求选择合适的构造函数来初始化Adapter,从而实现ListView的数据填充。
Class Overview
By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.

Constructor
ArrayAdapter(Context context, int resource)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId)
Constructor
ArrayAdapter(Context context, int resource, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
Constructor
ArrayAdapter(Context context, int resource, List<T> objects)
Constructor
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

第一种用法:
ListView listview = new ListView(this);  
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1);  
        adapter.add("string1");  
        adapter.add("haha");  
        adapter.add("heihei");        
        listview.setAdapter(adapter);  


第二种用法:
layout下的online_user_list_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="wrap_content">  
<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"  
android:id="@+id/online_user_list_item_textview" android:text="TextView">
</TextView>  
<Button  
    android:text="button"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content">  
</Button>  
</LinearLayout>  

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.online_user_list_item, R.id.online_user_list_item_textview);  


第三种用法:
如果我们需要展示的内容是一仅一个textview承载不了的,我们可以自定义
public class UserListAdapter extends ArrayAdapter<User> {  
    private int resourceId;  
    public UserListAdapter(Context context, int textViewResourceId, List<User> objects) {  
        super(context, textViewResourceId, objects);  
        this.resourceId = textViewResourceId;  
    }  
      
    @Override  
    public View getView(int position, View convertView, ViewGroup parent){  
        User user = getItem(position);  
        LinearLayout userListItem = new LinearLayout(getContext());  
        String inflater = Context.LAYOUT_INFLATER_SERVICE;   
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);   
        vi.inflate(resourceId, userListItem, true);  
        TextView tvUsername = (TextView)userListItem.findViewById(R.id.tv_user_list_username);  
        TextView tvAskedNum = (TextView)userListItem.findViewById(R.id.tv_user_list_askednum);  
        TextView tvLastMsg = (TextView)userListItem.findViewById(R.id.tv_user_list_lastmsg);  
        tvUsername.setText(user.getUsername());  
        tvAskedNum.setText(String.valueOf(user.getAskedNum()));  
        tvLastMsg.setText(user.getLastMsg());  
        return userListItem;  
    }  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值