Android ListView Adapter

本文介绍了一种在Android应用中自定义ListView的方法,通过创建一个名为RecipeAdapter的自定义适配器来展示菜谱信息。该适配器接收一个HashMap列表作为数据源,并通过LayoutInflater加载布局,将数据绑定到视图组件上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这段时间,由于在项目中经常要用到ListView控件,所以对ListView控件的自定义做了一些了解 ,其实listView控件的使用关键是adapter的使用

直接上代码

package com.Adapters;

import java.util.ArrayList;
import java.util.HashMap;

import com.AppClient.R;



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 RecipeAdapter extends BaseAdapter{

	
	private LayoutInflater layoutInflater;  
    private Context context;  
    
  
	
	/**
	 * 构造函数,配置参数
	 * @param data 配置ArrayList<HashMap<String,Object>类型的参数>
	 * @param context this*/
	public RecipeAdapter(ArrayList<HashMap<String, Object>> data,
			 Context context) {
		super();
		this.data = data;
		this.layoutInflater = layoutInflater.from(context);
		this.context = context;
	}
	
	
	

	private ArrayList<HashMap<String, Object>> data;  
    /** 
     * LayoutInflater 类是代码实现中获取布局文件的主要形式 
     *LayoutInflater layoutInflater = LayoutInflater.from(context); 
     *View convertView = layoutInflater.inflate(); 
     *LayoutInflater的使用,在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(), 
                  不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化! 
                而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。 
     */  
  
    
    
    
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
		
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
    
	
	 /** 
     * android绘制每一列的时候,都会调用这个方法 
     */ 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Controlors controlor=null;
		if(convertView==null)
		{
			controlor=new Controlors();
			//获取布局组件
			convertView=layoutInflater.inflate(R.layout.recipe_list, null);
			controlor.imageView=(ImageView)convertView.findViewById(R.id.recipe_image);
			controlor.textView=(TextView)convertView.findViewById(R.id.recipe_name);
			controlor.writer=(TextView)convertView.findViewById(R.id.writer);
			controlor.mainResource=(TextView)convertView.findViewById(R.id.main_resource);
			controlor.littleResourc=(TextView)convertView.findViewById(R.id.little_resource);
			//使用tag来存储数据
			convertView.setTag(controlor);
		}
		else
		{
			controlor=(Controlors)convertView.getTag();
		}
		//绑定数据以及事件触发
		controlor.imageView.setBackgroundResource((Integer)data.get(position).get("recipe_image"));
		controlor.textView.setText((String)data.get(position).get("recipe_name"));
	    controlor.writer.setText((String)data.get(position).get("writer"));
	    controlor.mainResource.setText((String)data.get(position).get("main_resource"));
	    controlor.littleResourc.setText((String)data.get(position).get("little_resource"));
		return convertView;
	}
	
	

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值