自定義介面卡--ListView資料來源的繫結
阿新 • • 發佈:2019-02-13
package codefirst.listview_test; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.List; /** * Created by cheng on 2017/1/25. */ //自定義的介面卡 public class MyAdapter extends BaseAdapter { private List dataList; //裝載資料來源 //private Context context; //getView()方法的佈局裝載器需要用到 private LayoutInflater layoutInflater; //佈局裝載器 public MyAdapter(List dataList, Context context) { this.layoutInflater=LayoutInflater.from(context); this.dataList = dataList; } @Override public int getCount() { return dataList.size(); } //物件 @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int postion) { return postion; } @Override public View getView(int position, View convertView, ViewGroup viewGroup) { ViewHolder viewHolder=new ViewHolder(); //裝載id的中介 if(convertView==null){ //將二者繫結 convertView=layoutInflater.inflate(R.layout.lv_item,null); viewHolder.itemImage= (ImageView) convertView.findViewById(R.id.iv_icon); //用來裝載控制元件,這樣可以不用每次去findId viewHolder.itemName= (TextView) convertView.findViewById(R.id.tv_iconName); convertView.setTag(viewHolder); }else { viewHolder= (ViewHolder) convertView.getTag(); } ItemBean bean=dataList.get(position); //通過ViewHolder找到對應控制元件 viewHolder.itemImage.setImageResource(bean.itemImageId); viewHolder.itemName.setText(bean.itemName); return convertView; //findID沒有再用一箇中介去儲存 /* if(convertView==null){ convertView=layoutInflater.inflate(R.layout.lv_item,null); //快取機制 ItemBean bean=dataList.get(position); //一個項 ImageView itemImage= (ImageView) convertView.findViewById(R.id.iv_icon); TextView itemName= (TextView) convertView.findViewById(R.id.tv_iconName); itemImage.setImageResource(bean.itemImageId); itemName.setText(bean.itemName); }*/ //沒有用到快取 /* View view=layoutInflater.inflate(R.layout.lv_item,null); /* //將一個佈局檔案轉化為一個view ItemBean bean=dataList.get(position); //一個item項的內容 ImageView imageView= (ImageView) view.findViewById(R.id.iv_icon); TextView textView= (TextView) view.findViewById(R.id.tv_iconName); imageView.setImageResource(bean.itemImageId); //物件呼叫成員屬性 textView.setText(bean.itemName); return view;*/ //return convertView; } //自定義泛型----內部類 private class ViewHolder { private ImageView itemImage; private TextView itemName; //一個ListView項的名稱 } }package codefirst.listview_test; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private String iconName_res[]={"通訊錄","漂亮","日曆","相機","鬧鐘","遊戲", "簡訊","音樂","河流","設定","氣球","天氣","地圖","視訊"}; //名稱資料來源 private int iconImage_res[]={R.mipmap.address_book,R.mipmap.beau,R.mipmap.calendar, R.mipmap.camera,R.mipmap.clock,R.mipmap.games_control,R.mipmap.messenger, R.mipmap.ringtone,R.mipmap.riven,R.mipmap.settings,R.mipmap.speech_balloon, R.mipmap.weather,R.mipmap.world,R.mipmap.youtube}; private List dataList; //裝載資料來源 private MyAdapter adapter; private ListView listItem_lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initAdapter(); listItem_lv= (ListView) findViewById(R.id.lv_main); listItem_lv.setAdapter(adapter); } private void initAdapter() { adapter=new MyAdapter(dataList,this); } //初始化資料來源 private void initData() { dataList=new ArrayList<>(); for(int i=0;i