BaseAdapter给LIstview填充数据

BaseAdapter

使用BaseAdapter比较简单,主要是通过继承此类来实现BaseAdapter的四个方法:

public int getCount(): 适配器中数据集的数据个数;

public Object getItem(int position): 获取数据集中与索引对应的数据项;

public long getItemId(int position): 获取指定行对应的ID;

public View getView(int position,View convertView,ViewGroup parent): 获取没一行Item的显示内容。

1.创建布局文件baseadapter.xml










2.Animal.java

public class Animal {
private String aName;
private String aSpeak;
private int aIcon;

public Animal() {
}

public Animal(String aName, String aSpeak, int aIcon) {
    this.aName = aName;
    this.aSpeak = aSpeak;
    this.aIcon = aIcon;
}
......get、set方法

3.AnimalAdapter.java
package com.example.baseadapter;

import java.util.LinkedList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AnimalAdapter extends BaseAdapter {

	    private LinkedList mData;
	    private Context mContext;

	    public AnimalAdapter(LinkedList mData, Context mContext) {
	        this.mData = mData;
	        this.mContext = mContext;
	    }

	    @Override
	    public int getCount() {
	        return mData.size();
	    }
	    @Override
	    public Object getItem(int position) {
	        return null;
	    }
	    @Override
	    public long getItemId(int position) {
	        return position;
	    }
	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	    
	    //通过LayoutInflater的from方法,可以将布局文件转换成一个View对象convertView
	    	convertView = LayoutInflater.from(mContext).inflate(R.layout.baseadapter,parent,false);
	    	//将convertView中的布局的id转换成Java中的对象。
	        ImageView img_icon = (ImageView) convertView.findViewById(R.id.imgtou);
	        TextView txt_aName = (TextView) convertView.findViewById(R.id.name);
	        TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.says);
	        //通过各种set方法修改对象的属性
	        img_icon.setBackgroundResource(mData.get(position).getaIcon());
	        txt_aName.setText(mData.get(position).getaName());
	        txt_aSpeak.setText(mData.get(position).getaSpeak());
	        return convertView;
	        
	    }

}
4.package com.example.baseadapter;

import java.util.LinkedList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;

import android.widget.ListView;

public class MainActivity extends Activity {

private List mData = null;

private Context mContext;
private AnimalAdapter mAdapter = null;
private ListView list_animal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baseadapter);

    mContext = MainActivity.this;
    list_animal = (ListView) findViewById(R.id.listView1);
    mData = new LinkedList();
    mData.add(new Animal("鲁迅", "《呐喊》", R.drawable.ic_launcher));
    mData.add(new Animal("朱自清", "《春》", R.drawable.ic_launcher));
    mData.add(new Animal("徐志摩", "《再别康桥》", R.drawable.ic_launcher));
    mData.add(new Animal("老舍", "骆驼祥子", R.drawable.ic_launcher));
    mData.add(new Animal("巴金", "家", R.drawable.ic_launcher));
    mAdapter = new AnimalAdapter((LinkedList) mData, mContext);
    list_animal.setAdapter(mAdapter);
}

}

5.,AndroidManifest.xml




    
        
            

            
        
    

![在这里插入图片描述](https://img-blog.csdnimg.cn/20190408095747358.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE3NDUzNg==,size_16,color_FFFFFF,t_70)

你可能感兴趣的:(Android)