封装的适配器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="@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>
关于封装的适配器adapter如何用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。