首页 > 代码库 > 在Android中自定义适配器

在Android中自定义适配器

啊喂,好久没更新博客了,唉唉,真是抱歉啊!求原谅!



1、自定义列表项适配器的主要方法是:

①public int getCount()=====>此方法用来控制列表项的个数

②public View getView(int position, View convertView, ViewGroup parent)====>此方法用来控制页面


2、自定义适配器在实际开发中使用的非常灵活也非常广泛,因为,此方法一定要掌握。(虽然也有现成的适配器,但是在实际开发中使用不多)


3、在自定义适配器的过程中,需要创建一个BaseAdapter对象,扩展该对象需要重写如下四个方法:

①getCount():该方法的返回值控制该Adapter将会包含多少个列表项;

②getItem(int position):该方法的返回值决定第position处的列表项的内容;

③getItemId(int position):该方法的返回值决定第position处的列表项的ID;

④getView(int position, View convertView, ViewGroup parent):该方法的返回值决定第position出的列表项组件。


4、具体实现代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="match_parent"
	android:layout_height="match_parent" >
<ListView
    android:id="@+id/myList"
	android:layout_width="match_parent"
	android:layout_height="match_parent"/>
</LinearLayout>
package com.example.baseadaptert;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.content.Context;

public class BaseAdapterT extends Activity {

	private ListView myList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myList = (ListView) findViewById(R.id.myList);
        
        final String names[] = new String[]{"xiaoming", "xiaohua", "xiaomi", "xiaomei"};
        final int images[] = new int[] {R.drawable.libai, R.drawable.nongyu, R.drawable.qingzhao, R.drawable.tiger};
//        final int layoutID = R.layout.item;
        myList.setAdapter(new BaseAdapter() {
			
        	@Override
			public int getCount() {
				return names.length;
			}
        	
        	
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				Activity activity = (Activity) BaseAdapterT.this;
//				LinearLayout out = (LinearLayout) activity.getLayoutInflater().inflate(position, null);
				LinearLayout out = (LinearLayout) activity.getLayoutInflater().inflate(R.layout.item, null);
				ImageView iv = (ImageView) out.findViewById(R.id.imageView);
				iv.setImageResource(images[position]);
				TextView tv = (TextView) out.findViewById(R.id.textView);
				tv.setText(names[position]);
				return out;
			}
			
			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}
			
			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}
			
		});
        
        myList.setOnItemClickListener(new OnItemClickListener() {// 设置点击监听器
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(view.getContext(), names[position], 1).show();
			}
		});
    }
}




在Android中自定义适配器