浏览 4283 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-04-27
android开发中会经常用来自定义的Adapter,如下就是一个简单的自定义Adapter实例。
import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; /** * 自定义适配器通过ListView显示内容,这个比较重要需要掌握 * @author Dylan */ public class CustomAdapterActivity extends Activity { private ListView mListView; private CustomAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mListView = (ListView) findViewById(R.id.listview); mAdapter = new CustomAdapter(); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(new ItemClickListener()); } private final class ItemClickListener implements OnItemClickListener { // AdapterView<?> parent, --> ListView View view, ---> item int // position,--> item的数据在Adapter中位置 long id---> item在listView中得位置 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String name = (String) mAdapter.getItem(position); Toast.makeText(getApplicationContext(), "name =" + name + ",position " + position, Toast.LENGTH_SHORT) .show(); } } private final class CustomAdapter extends BaseAdapter { private String[] names = new String[] { "无线和网络", "通话设置", "声音", "显示", "位置和安全", "应用程序", "账户与同步", "隐私权", "SD卡和手机内存", "语言和键盘", "辅助功能", "日期和时间", "关于手机" }; private int[] images = new int[] { R.drawable.image01, R.drawable.image02, R.drawable.image03, R.drawable.image04, R.drawable.image05, R.drawable.image06, R.drawable.image07, R.drawable.image08, R.drawable.image09, R.drawable.image10, R.drawable.image11, R.drawable.image12, R.drawable.image13 }; private LayoutInflater mInflater; public CustomAdapter() { mInflater = getLayoutInflater(); } // 描述adpter的大小(确定了listView的条目) @Override public int getCount() { return names.length; } // Adapter对于的position的数据 @Override public Object getItem(int position) { return names[position]; } // 得到item 在adapter所对应的位置 @Override public long getItemId(int position) { return position; } // 创建listview的item条目,把数据绑定给item int position, Adapter的下标 View // convertView, 缓存的第一屏item的布局文件 ViewGroup parent ListView @Override public View getView(int position, View convertView, ViewGroup parent) { View view = null; if (convertView != null) { view = convertView; } else { view = mInflater.inflate(R.layout.item, parent, false); } ImageView iv_header = (ImageView) view.findViewById(R.id.iv_header); TextView tv_name = (TextView) view.findViewById(R.id.tv_name); iv_header.setImageResource(images[position]); tv_name.setText(names[position]); return view; } /* * @Override public View getView(int position, View convertView, ViewGroup parent) { ViewCache viewCache = null; if (convertView == null) { viewCache = new ViewCache(); convertView = inflater.inflate(R.layout.folder_list_item, null); viewCache.tvName = (TextView) convertView.findViewById(R.id.tv_folder_name); viewCache.ivPreview = (ImageView) convertView.findViewById(R.id.iv_folder_preview); convertView.setTag(viewCache); } else { viewCache = (ViewCache) convertView.getTag(); } FileInfo fileInfo = arrayList.get(position); viewCache.tvName.setText(new File(fileInfo.filePath).getName()); return convertView; } public static class ViewCache { public ImageView ivPreview; public TextView tvName; } */ } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-05-01
恩 代码清晰明了
|
|
返回顶楼 | |