下面是基于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() 这两个函数