首页 > 代码库 > 封装的适配器 adapter
封装的适配器 adapter
//封装的适配器类 adapter public class NewJobAdapter extends BaseAdapter{ private List<Map<String, Object>> data = null; private LayoutInflater mInflater = null; private Context mContext; public NewJobAdapter(){ } public NewJobAdapter(Context context, List<Map<String, Object>> list) { // 根据context上下文加载布局 this.mInflater = LayoutInflater.from(context); mContext = context; data = list; } @Override public int getCount() { // 在此适配器中所代表的数据集中的条目数 return data.size(); } @Override public Object getItem(int position) { // 获取数据集中与指定索引对应的数据项 return position; } @Override public long getItemId(int position) { // 获取在列表中与指定索引对应的行id return position; } // 获取一个在数据集中指定索引的视图来显示数据 @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; // 如果缓存convertView为空,则需要创建View if (convertView == null) { holder = new ViewHolder(); // 根据自定义的Item布局加载布局 convertView = mInflater.inflate(R.layout.item_list_new_job, null); holder.img = (ImageView) convertView.findViewById(R.id.id_img); holder.time = (TextView) convertView.findViewById(R.id.id_time); holder.address = (TextView) convertView.findViewById(R.id.id_address); holder.message = (TextView) convertView.findViewById(R.id.id_message); holder.price = (TextView) convertView.findViewById(R.id.id_price); // 将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.img.setBackgroundResource((Integer) data.get(position).get("img")); holder.time.setText((String) data.get(position).get("time")); holder.message.setText((String) data.get(position).get("area")); holder.address.setText((String) data.get(position).get("position")); return convertView; } /** * ViewHolder静态类 * 图片 * 工作职位 * 时间 * 兼职地点 * 价钱 * */ static class ViewHolder { public ImageView img; public TextView message; public TextView time; public TextView address; public TextView price; } }
//activity调用 public class NewJobActivity extends Activity implements OnItemClickListener{ private List<Map<String, Object>> data = null; private List<Map<String, Object>> list; private ListView mListView; private NewJobAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_new_job); mListView = (ListView) findViewById(R.id.new_job_list); mListView.setOnItemClickListener(this); data = getData();// 获取数据 adapter = new NewJobAdapter(this, data); mListView.setAdapter(adapter); } private List<Map<String, Object>> getData(){ list = new ArrayList<Map<String, Object>>(); Map<String, Object> map; for(int i=0; i<20; i++){ map = new HashMap<String, Object>(); map.put("time", "2015-01-30"); map.put("img", R.drawable.ic_launcher); map.put("area", "三亚市区"); map.put("position", "促销员"); list.add(map); } return list; } public void backClick(View v){ NewJobActivity.this.finish(); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub //Toast.makeText(this, "" + arg2, Toast.LENGTH_LONG).show(); } }
//item_list <?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:layout_marginBottom="3dp" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:background="@drawable/mm_listitem" android:gravity="center_vertical" > <ImageView android:id="@+id/id_img" android:layout_width="55dp" android:layout_height="55dp" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:paddingLeft="5dp" android:src="http://www.mamicode.com/@drawable/xiaohei" /> <TextView android:id="@+id/id_address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/id_img" android:padding="7dp" android:text="屈臣氏学生兼职" android:textColor="#000" /> <TextView android:id="@+id/id_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="7dp" android:text="2015-01-29" android:textColor="#666" /> <TextView android:id="@+id/id_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/id_img" android:padding="7dp" android:text="三亚市区" android:textColor="#666" /> <TextView android:id="@+id/id_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:padding="7dp" android:text="200元/天" android:textColor="#000" /> </RelativeLayout> </LinearLayout>
本文出自 “爬过山见过海” 博客,请务必保留此出处http://670176656.blog.51cto.com/4500575/1610556
封装的适配器 adapter
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。