温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ArrayAdapter的定义

发布时间:2020-07-13 12:28:02 来源:网络 阅读:446 作者:王颖1 栏目:移动开发

各种Adapter的用法(适配器)

*同样是一个ListView,可以用不同的Adapter让它显示出来,比如说最常用的ArrayAdapter, SimpleAdapter,BaseAdapter.

 android.widget.ArrayAdapter<string>

A concrete BaseAdapter that is backed by an array of arbitrary objects. 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.
However the TextView is referenced, it will be filled with the toString() of 
each object in the array. You can add lists or arrays of custom objects. 
Override the toString() method of your objects to determine what text will be 
displayed for the item in the list.
To use something other than TextViews for the array display, for instance, 
ImageViews, or to have some of data besides toString() results fill the views, 
override getView(int, 
View, ViewGroup) to return the type of view you want.

ArrayAdapter的定义


如何定义ArrayAdapter


错误实例:

package com.example.testandroidproject;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;

public class MainActivity extends ActionBarActivity {

    private String[] ganlist = new String[]{"孙悟空","猪八戒","沙和尚"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        private ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,ganlist);
        //这样定义ArrayAdapter会出现红色错误
    }    

}

*****************************************************************************************************

Multiple markers at this line
    - Illegal modifier for parameter arrayAdapter; only final is permitted
    - Line breakpoint:MainActivity [line: 15] - onCreate(Bundle)

*****************************************************************************************************

正确实例:

package com.example.testandroidproject;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;

public class MainActivity extends ActionBarActivity {

    private String[] ganlist = new String[]{"孙悟空","猪八戒","沙和尚"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,ganlist);
        //去掉private
    }    

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI