任意のMyAdapter extends BaseAdapter(純java)をカスタマイズ

3459 ワード

Spinner、AutoCompleteTextView、Gallery、GridView、ListViewなどに適用
 
主に次の2つの関数を完了します.
public int getCount() {}
public View getView(int position, View convertView, ViewGroup parent) {}
 
import java.util.*;

import android.content.Context;
import android.view.*;
import android.widget.*;
import android.widget.ImageView.ScaleType;

public class MyAdapter extends BaseAdapter{
	
	Context context=null;
	List> list=null;
	
	MyAdapter(Context context){
		this.context=context;
		//     
		list=new ArrayList>();
    	Map item;
    	item=new HashMap();
    	item.put("  ",R.drawable.icon);
    	item.put("  ","    ");
    	item.put("  "," ");
    	item.put("  ", "15 ");
    	list.add(item);
    	item=new HashMap();
    	item.put("  ",R.drawable.icon);
    	item.put("  ","    ");
    	item.put("  "," ");
    	item.put("  ", "15 ");
    	list.add(item);
    	item=new HashMap();
    	item.put("  ",R.drawable.icon);
    	item.put("  ","    ");
    	item.put("  "," ");
    	item.put("  ", "14 ");
    	list.add(item);
	}
	public int getCount() {return list.size();}
	public Object getItem(int position) {return position;}
	public long getItemId(int position) {return position;}

	public View getView(int position, View convertView, ViewGroup parent) {
    	//  
    	ImageView iv=new ImageView(context);
    	RelativeLayout.LayoutParams lp_iv=new RelativeLayout.LayoutParams(-2,-2);
    	lp_iv.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    	iv.setLayoutParams(lp_iv);
    	iv.setScaleType(ScaleType.CENTER_INSIDE);
    	iv.setImageResource((Integer)((list.get(position)).get("  ")));
    	//  
    	TextView name=new TextView(context);
    	RelativeLayout.LayoutParams lp_tv=new RelativeLayout.LayoutParams(-2,-2);
    	lp_tv.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    	name.setLayoutParams(lp_tv);
    	name.setTextSize(name.getTextSize()+10);
    	name.setText((String)(list.get(position).get("  ")));
		//  
    	TextView sex=new TextView(context);
    	RelativeLayout.LayoutParams lp_sex=new RelativeLayout.LayoutParams(-2,-2);
    	lp_sex.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    	sex.setLayoutParams(lp_sex);
    	sex.setText((String)(list.get(position).get("  ")));
    	//  
    	TextView  age=new TextView(context);
    	RelativeLayout.LayoutParams lp_age=new RelativeLayout.LayoutParams(-2,-2);
    	lp_age.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    	age.setLayoutParams(lp_age);
    	age.setText((String)(list.get(position).get("  ")));
    	//    1
    	RelativeLayout rl1=new RelativeLayout(context);
    	rl1.setLayoutParams(new RelativeLayout.LayoutParams(-1,-2));
    	rl1.addView(iv);
    	rl1.addView(name);
    	//    2
    	RelativeLayout rl2=new RelativeLayout(context);
    	rl2.setLayoutParams(new LinearLayout.LayoutParams(-1,-2));
    	rl2.addView(sex);
    	rl2.addView(age);
    	//    
		LinearLayout returnView=new LinearLayout(context);
    	returnView.setLayoutParams(new ListView.LayoutParams(-1,-2));//  :ListView.LayoutParams
    	returnView.setOrientation(LinearLayout.VERTICAL);
    	returnView.addView(rl1);
    	returnView.addView(rl2);

		return returnView;
	}

}