BaseAdapter使用案例


BaseAdapter使用案例_第1张图片

在页面布局上用ListView显示adapter传过来的数据。



    

    



通过BaseAdapter将数据传递到ListView上
package com.eson.baseadapter ;

import android.graphics.Color ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.view.View ;
import android.view.ViewGroup ;
import android.widget.BaseAdapter ;
import android.widget.ImageView ;
import android.widget.LinearLayout ;
import android.widget.ListView ;
import android.widget.TextView ;

public class BaseAdapterTest extends AppCompatActivity {
    private ListView myList;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super .onCreate(savedInstanceState) ;
        setContentView(R.layout. activity_main );
        myList = (ListView)findViewById(R.id. myList );
        final BaseAdapter adapter = new BaseAdapter() {
            @Override
            public int getCount () {
                return 40 ;//显示的行数
            }

            @Override
            public Object getItem( int position) {
                return null;
            }
            //重写该方法,该方法返回的View将作为列表框
            @Override
            public long getItemId (int position) {
                return position ;
            }

            @Override
            public View getView( int position, View convertView , ViewGroup parent) {
                //创建一个LinearLayout,并向其中添加两个组件
                LinearLayout line = new LinearLayout(BaseAdapterTest. this );
                line.setOrientation(LinearLayout. HORIZONTAL) ;
                ImageView image = new ImageView(BaseAdapterTest. this );
                image.setImageResource(R.drawable. show) ;
                TextView text = new TextView(BaseAdapterTest. this );
                text.setText("第" +(position+ 1)+ "个列表项" ) ;
                text.setTextSize(20 ) ;
                text.setTextColor(Color. RED) ;
                line.addView(image);
                line.addView(text);
                //返回LinearLayout实例
                return line ;
            }
        };
       myList .setAdapter(adapter) ;
    }
}




你可能感兴趣的:(BaseAdapter使用案例)