溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ArrayAdapter的定義

發(fā)布時間:2020-07-13 12:28:02 來源:網(wǎng)絡(luò) 閱讀:455 作者:王穎1 欄目:移動開發(fā)

各種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會出現(xiàn)紅色錯誤
    }    

}

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

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問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI