Android之BaseAdapter从一个函数内部放到外边

系统 1622 0

模仿 参考着ArrayAdapter将BaseAdapter写成了外部类,此次内部类转化外部类的唯一难点,是参数的传递,要通过自己写的BaseAdapter的构造函数将MainActivity的参数传递给外部类,通过外部类的返回值传递给插件,具体函数还是那些,但是多了一个构造函数。
      package com.example.fouradpater_3;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyAdapter<T> extends BaseAdapter {
	private LayoutInflater mInflater;
	
	private Context mContext;
	
	private int mResource;
	
	private int mDropDownResource;
	
	
	
	private List<T> mList;
    
	private void init(Context context, int resource, List<T> objects) {
        mContext = context;
        mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mResource = mDropDownResource = resource;
        mList = objects;
       
    }
    //重写构造函数,将List与Adapter关联
	public MyAdapter(Context context, int textViewResourceId, List<T> objects) {
		init(context, textViewResourceId, objects);
	}
	//获取list数量
	@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;
	}
	//从过度的view中获取view
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		TextView text;
		ImageView imgview;
		RelativeLayout rLayout;
		 if (convertView == null) {
			 rLayout = (RelativeLayout) mInflater.inflate(R.layout.new_file, parent, false);
			 Log.d("com.example.listadpater_text.MainActivity","pos = "+ position +"new");
	        } else {
	        	rLayout = (RelativeLayout) convertView;
	        	 Log.d("com.example.listadpater_text.MainActivity","pos = "+ position +"old");
	        }
		String item = (String) getItem(position);
		text = (TextView) rLayout.findViewById(R.id.TextView2);
		text.setText(item);
		
		
		
		imgview = (ImageView) rLayout.findViewById(R.id.ImageView1);
		
		imgview.setImageResource(R.drawable.ic_launcher);
		
		
		return rLayout;
	}



}

    
 
      package com.example.fouradpater_3;

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

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.Adapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class MainActivity extends Activity {
	
	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]);
	    
		}
		//利用自己写的外部类传递进去参数
		mAdapter = new MyAdapter<String>(this,R.layout.new_file,mList);
		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);
		
	
		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;
	}


}

    
 
Android之BaseAdapter从一个函数内部放到外边
 

Android之BaseAdapter从一个函数内部放到外边


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论