适配器的经典写法
有关ListView优化机制及滑动时数据错乱问题的讨论
class GridViewAdapter extends BaseAdapter{ LayoutInflater inflater; List<PackageInfo> pkInfos; public GridViewAdapter(Context context,List<PackageInfo> packageInfos) { inflater = LayoutInflater.from(context); this.pkInfos = packageInfos; } @Override public int getCount() { // TODO Auto-generated method stub return pkInfos.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return pkInfos.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.gv_item, null); TextView tv = (TextView)view.findViewById(R.id.gv_item_appname); ImageView iv = (ImageView)view.findViewById(R.id.gv_item_icon); tv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager())); iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager())); return view; } }
1 class ListViewAdapter extends BaseAdapter{ 2 3 LayoutInflater inflater; 4 List<PackageInfo> pkInfos; 5 public ListViewAdapter(Context context,List<PackageInfo> packageInfos) { 6 inflater = LayoutInflater.from(context); 7 this.pkInfos = packageInfos; 8 } 9 @Override 10 public int getCount() { 11 // TODO Auto-generated method stub 12 return pkInfos.size(); 13 } 14 15 @Override 16 public Object getItem(int position) { 17 // TODO Auto-generated method stub 18 return pkInfos.get(position); 19 } 20 21 @Override 22 public long getItemId(int position) { 23 // TODO Auto-generated method stub 24 return position; 25 } 26 27 @Override 28 public View getView(int position, View convertView, ViewGroup parent) { 29 // TODO Auto-generated method stub 30 31 View view = inflater.inflate(R.layout.lv_item, null); 32 TextView ntv = (TextView)view.findViewById(R.id.lv_item_appname); 33 TextView ptv = (TextView)view.findViewById(R.id.lv_item_packagename); 34 ImageView iv = (ImageView)view.findViewById(R.id.lv_icon); 35 ntv.setText(pkInfos.get(position).packageName); 36 ptv.setText(pkInfos.get(position).applicationInfo.loadLabel(getPackageManager())); 37 iv.setImageDrawable(pkInfos.get(position).applicationInfo.loadIcon(getPackageManager())); 38 39 return view; 40 } 41 42 }
尽量自己构建适配器,这样加载速度快,效率高。哪怕简单的也尽量不要使用SimpleAdapter
view holder的用法:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/userinfo1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="4" /> <TextView android:id="@+id/userinfo2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" /> <TextView android:id="@+id/userinfo3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" /> <TextView android:id="@+id/userinfo4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" /> <TextView android:id="@+id/userinfo5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_weight="3" /> </LinearLayout>
package com.example.health; import java.util.List; import com.example.health.util.LogUtil; import com.example.health.util.User; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class UserListActivity extends Activity{ private ListView userList; private MyApplication app; private static final String TAG = "UserListActivity"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.userlist); app = (MyApplication) getApplication(); LogUtil.i(TAG ,app.userList.get(0).toString()); userList = (ListView) findViewById(R.id.data_list); userList.setAdapter(new UserListAdapter(this, app.userList)); } class UserListAdapter extends BaseAdapter { private LayoutInflater mInflater; private Context mContext; private List<User> mDatas; public UserListAdapter(Context context, List<User> mDatas) { mInflater = LayoutInflater.from(context); this.mContext = context; this.mDatas = mDatas; } @Override public int getCount() { return mDatas.size(); } @Override public Object getItem(int position) { return mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = mInflater.inflate(R.layout.userlistitem, parent, false); viewHolder = new ViewHolder(); viewHolder.mTextView1 = (TextView) convertView.findViewById(R.id.userinfo1); viewHolder.mTextView2 = (TextView) convertView.findViewById(R.id.userinfo2); viewHolder.mTextView3 = (TextView) convertView.findViewById(R.id.userinfo3); viewHolder.mTextView4 = (TextView) convertView.findViewById(R.id.userinfo4); viewHolder.mTextView5 = (TextView) convertView.findViewById(R.id.userinfo5); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.mTextView1.setText(mDatas.get(position).getShenfennum()); viewHolder.mTextView2.setText(mDatas.get(position).getName()); viewHolder.mTextView3.setText(mDatas.get(position).getRegtime()); viewHolder.mTextView4.setText(mDatas.get(position).getAddress()); viewHolder.mTextView5.setText(mDatas.get(position).getBirthdate()); return convertView; } /*@Override public View getView(int position, View convertView, ViewGroup parent) { TextView mTextView1; TextView mTextView2; TextView mTextView3; TextView mTextView4; TextView mTextView5; View view = mInflater.inflate(R.layout.userlistitem, null); mTextView1 = (TextView) view.findViewById(R.id.userinfo1); mTextView2 = (TextView) view.findViewById(R.id.userinfo2); mTextView3 = (TextView) view.findViewById(R.id.userinfo3); mTextView4 = (TextView) view.findViewById(R.id.userinfo4); mTextView5 = (TextView) view.findViewById(R.id.userinfo5); mTextView1.setText(mDatas.get(position).getShenfennum()); mTextView2.setText(mDatas.get(position).getName()); mTextView3.setText(mDatas.get(position).getRegtime()); mTextView4.setText(mDatas.get(position).getAddress()); mTextView5.setText(mDatas.get(position).getBirthdate()); return view; } */ } private final class ViewHolder { TextView mTextView1; TextView mTextView2; TextView mTextView3; TextView mTextView4; TextView mTextView5; } }