码迷,mamicode.com
首页 > 其他好文 > 详细

spinner使用自定义的Adapter

时间:2014-11-27 14:34:36      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:des   android   blog   http   io   ar   color   os   使用   

运行效果

            bubuko.com,布布扣                  bubuko.com,布布扣

定义每一个Item的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/tv_kc_year1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:textColor="@color/black" />

    <TextView
        android:id="@+id/tv_kc_year2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black" />

</LinearLayout>



建立实体类

package www.hbsi.news.model;

public class KcYears {

	private String kcYear1;
	private String kcYear2;

	public KcYears() {
		super();
	}

	public KcYears(String kcYear1, String kcYear2) {
		super();
		this.kcYear1 = kcYear1;
		this.kcYear2 = kcYear2;
	}

	public String getKcYear1() {
		return kcYear1;
	}

	public void setKcYear1(String kcYear1) {
		this.kcYear1 = kcYear1;
	}

	public String getKcYear2() {
		return kcYear2;
	}

	public void setKcYear2(String kcYear2) {
		this.kcYear2 = kcYear2;
	}

}



创建Adapter继承与BaseAdapter,进行适配

package com.hbsi.education;

import java.util.List;

import www.hbsi.newapp.R;
import www.hbsi.news.model.KcYears;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SpYearsAdapter extends BaseAdapter {
	private List<KcYears> mList;
	private Context mContext;

	public SpYearsAdapter(Context pContext, List<KcYears> pList) {
		this.mContext = pContext;
		this.mList = pList;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater _LayoutInflater = LayoutInflater.from(mContext);
		convertView = _LayoutInflater.inflate(R.layout.item_sp_years, null);
		if (convertView != null) {
			TextView tvKcYear1 = (TextView) convertView
					.findViewById(R.id.tv_kc_year1);
			TextView tvKcYear2 = (TextView) convertView
					.findViewById(R.id.tv_kc_year2);
			tvKcYear1.setText(mList.get(position).getKcYear1());
			tvKcYear2.setText(mList.get(position).getKcYear2());
		}
		return convertView;
	}
}




在Activity中加入如下代码
package com.hbsi.education;

import java.util.ArrayList;
import java.util.List;

import www.hbsi.common.Common;
import www.hbsi.common.MyApp;
import www.hbsi.newapp.R;
import www.hbsi.news.model.KcYears;
import www.hbsi.utils.HbsiConstents;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Spinner;

/**
 * 成绩学分查询
 * 
 * @author zhaoyz 2014-11-27上午9:33:03
 */
public class AchievementAct extends Activity {

	private Spinner spKcYear;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 用于安全退出
		MyApp.getInstance().addActivity(this);

		setContentView(R.layout.act_achievement);

		// 初始化控件
		spKcYear = (Spinner) findViewById(R.id.sp_kc_year);
		//获取学号
		String sno = HbsiConstents.usermap.get("sno");// 1333333
		//根据学号截取前两位获取年份
		String years1 = "20" + sno.substring(0, 2);// "2013"
		//将年份转为数值型进行运算获取后三年年份
		int i = Integer.parseInt(years1);// 2013
		String years2 = (i + 1) + "";
		String years3 = (i + 2) + "";
		String years4 = (i + 3) + "";

		// 建立数据源
		List<KcYears> kcYears = new ArrayList<KcYears>();
		kcYears.add(new KcYears(years1, years2));
		kcYears.add(new KcYears(years2, years3));
		kcYears.add(new KcYears(years3, years4));
		// 建立Adapter绑定数据源
		SpYearsAdapter adapter = new SpYearsAdapter(this, kcYears);
		// 绑定Adapter
		spKcYear.setAdapter(adapter);
	}

	@Override
	protected void onStart() {
		// 用于安全退出
		MyApp.getInstance().addActivity(this);
		super.onStart();
	}

	@Override
	protected void onDestroy() {
		Common.DestroyLoading(AchievementAct.this);
		finish();
		super.onDestroy();
	}

}


spinner使用自定义的Adapter

标签:des   android   blog   http   io   ar   color   os   使用   

原文地址:http://blog.csdn.net/zhaoyazhi2129/article/details/41544783

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!