模仿 參考著ArrayAdapter將BaseAdapter寫成了外部類,此次內(nèi)部類轉(zhuǎn)化外部類的唯一難點(diǎn),是參數(shù)的傳遞,要通過自己寫的BaseAdapter的構(gòu)造函數(shù)將MainActivity的參數(shù)傳遞給外部類,通過外部類的返回值傳遞給插件,具體函數(shù)還是那些,但是多了一個(gè)構(gòu)造函數(shù)。
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;
}
//重寫構(gòu)造函數(shù),將List與Adapter關(guān)聯(lián)
public MyAdapter(Context context, int textViewResourceId, List<T> objects) {
init(context, textViewResourceId, objects);
}
//獲取list數(shù)量
@Override
public int getCount() {
return mList.size();
}
//獲取某個(gè)地址的數(shù)值
@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]);
}
//利用自己寫的外部類傳遞進(jìn)去參數(shù)
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;
}
}
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

