Android之BaseAdapter(二)

下面是基于List这个存储结构的Adapter

package com.example.fouradpater_3;

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

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {
	private LayoutInflater mInflater;
	
	private String[] mad = {"4","3","2","1"};
	
	private List<String> mList;
	
	private Spinner mSpinner;
	
	private GridView mGridView;
	
	private Gallery mGallery;
	
	private ListView mListView;
	
	private Adapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	    mList = new ArrayList<String>();
	    
		for(int i = 0; i<mad.length;  i++){
			
			mList.add(mad[i]);
	    
		}
		
		
		mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		mInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		
		mSpinner = (Spinner) findViewById(R.id.spinner1);
		mGridView = (GridView) findViewById(R.id.gridView1);
		mGallery = (Gallery) findViewById(R.id.gallery1);
		mListView = (ListView) findViewById(R.id.listView1);
		
		mAdapter = new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stu

				TextView text;
				ImageView imgview;
				RelativeLayout rLayout;
				 if (convertView == null) {
					 rLayout = (RelativeLayout) mInflater.inflate(R.layout.new_file, parent, false);
//					 view = new TextView(MainActivity.this);
			        } else {
			        	rLayout = (RelativeLayout) convertView;
			        }
				String item = (String) getItem(position);
//				rLayout.setTag(item.toString());
				text = (TextView) rLayout.findViewById(R.id.TextView2);
//				text.setText(item.toString());   画蛇添足
				text.setText(item);
				
				imgview = (ImageView) rLayout.findViewById(R.id.ImageView1);
//				imgview.setId(R.drawable.ic_launcher);// 用错函数
				
				imgview.setImageResource(R.drawable.ic_launcher);
				
				
				return rLayout;
			}
			
			@Override
			public long getItemId(int position) {
				return position;
			}
			
			@Override
			public Object getItem(int position) {
				return  mList.get(position);
			}
			
			@Override
			public int getCount() {
				
				return mList.size();
			}
		};

	
		mSpinner.setAdapter((SpinnerAdapter) mAdapter);
		mGridView.setAdapter((ListAdapter) mAdapter);
		mGallery.setAdapter((SpinnerAdapter) mAdapter);
		mListView.setAdapter((ListAdapter) mAdapter);
	}
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}


}

 可以看出来只要我们需要的储存方式 跟adpter需要的4个基本函数有对应就行,储存结构的改变最需要改变

getItem() getCount() 这两个函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值