模仿 参考着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; } }