Android基础_常见Adapter/ExpandListView/Dialog/PopupWindow/自定义AlertDialog/单元测试/转移文件到SD卡(四)
来源:互联网 发布:usr share mysql 编辑:程序博客网 时间:2024/06/18 14:42
Adapter接口
public interface Adapter { void registerDataSetObserver(DataSetObserver observer); void unregisterDataSetObserver(DataSetObserver observer); int getCount(); Object getItem(int position); long getItemId(int position); View getView(int position, View convertView, ViewGroup parent); static final int IGNORE_ITEM_VIEW_TYPE = AdapterView.ITEM_VIEW_TYPE_IGNORE; int getItemViewType(int position); int getViewTypeCount(); static final int NO_SELECTION = Integer.MIN_VALUE; boolean isEmpty();}
BaseAdapter
因为要实现的方法过多,我们一般使用简单的封装类BaseAdapterpublic abstract class BaseAdapter implements ListAdapter, SpinnerAdapter只需要实现四个方法
ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, // 定义每个item的布局 android.R.id.text1, // 定义需要绑定的textview的id datas;// 填充的一系列数据
SimpleAdapter
/*List<? extends Map<String, ?>> data, * extends Map == map的子类 * ? ==Object * simpleadapter 的子控件一般只能使用系统自带的控件 * 比如Textview-settext ,但非系统的他就不知道该调用什么方法了 * 比如 MyImageView --- * */ ArrayList<HashMap <String, Object>> datas =new ArrayList<HashMap <String, Object>>() ; for (int i = 0; i < 10; i++) { HashMap <String, Object> itemData = new HashMap<String,Object>(); itemData.put("name","袜子"+(i+3)); itemData.put("age",i+3); itemData.put("gender",i%2==0?"男":"女"); datas.add(itemData); }SimpleAdapter adapter = new SimpleAdapter( //进去首先 调用getcount -- datas.size(); //getView -- 第一步:将布局转换成view this, datas, //数据源,是一个列表数据,他的每一项是HashMAP<String,Obejct>,该HashMap代表的就是我们每一个item的所有数据 第三步:为每个item子控件赋值锁需要的数据源 R.layout.lv_item_layout, //每个item的布局文件 第一步:将布局转换成view new String[]{"name","age","gender"}, // 第四步:先取出item的所有数据 datas.get(position) --->HashMap<String,Object>,即整个item的所有数据的集合object-->再去找子数据 new int[]{R.id.name,R.id.age,R.id.gender});//代表item内部需要绑定的控件的id 第二步:找到子控件 lv.setAdapter(adapter);
ExpandListView
1.xml中定义 <ExpandableListView android:id="@+id/elv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 2.定义适配器 public class MyAdapter extends BaseExpandableListAdapter { ArrayList<String> mInitGroupNames; HashMap<String, ArrayList<String>> mChildrenData; public ArrayList<String> getmInitGroupNames() { return mInitGroupNames; } public HashMap<String, ArrayList<String>> getmChildrenData() { return mChildrenData; } public MyAdapter(ArrayList<String> initGroupNames, HashMap<String, ArrayList<String>> hashMap) { mInitGroupNames = initGroupNames; mChildrenData = hashMap; } @Override public int getGroupCount() { return mInitGroupNames.size(); } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { TextView tv = null; if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()).inflate( android.R.layout.simple_list_item_1, parent, false); tv = (TextView) convertView.findViewById(android.R.id.text1); convertView.setTag(tv); } else { tv = (TextView) convertView.getTag(); } String groupName = mInitGroupNames.get(groupPosition); tv.setText(groupName); return convertView; } /* * 每一个对应的子项的长度 1.首先根据groutposition获取对应的组名 2.根据组名获取对应所有的子参数 3.返回子参数的长度 */ @Override public int getChildrenCount(int groupPosition) { String groupName = mInitGroupNames.get(groupPosition); ArrayList<String> childrenData = mChildrenData.get(groupName); return childrenData.size(); } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView tv = null; if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()).inflate( android.R.layout.simple_list_item_1, parent, false); tv = (TextView) convertView.findViewById(android.R.id.text1); convertView.setTag(tv); } else { tv = (TextView) convertView.getTag(); } /*获取子项目的数据bean-->队列 * * */ String groupName = mInitGroupNames.get(groupPosition); ArrayList<String> childrenData = mChildrenData.get(groupName);// 根据子项目的索引找到对应的子项目名称 String childName = childrenData.get(childPosition); tv.setText(childName); return convertView; } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return null; } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return null; } @Override public long getGroupId(int groupPosition) { // TODO Auto-generated method stub return 0; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return 0; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; }} 3. ExpandableListView elv = (ExpandableListView) findViewById(R.id.elv); final MyAdapter dapter = new MyAdapter(initGroupNames(), initChildrenData()); elv.setAdapter(dapter);
Dialog
public void click01(View v) { /* 1.使用工厂模式 ,创建一个builder * titie message setpositive setnegative 可以选择使用 * 1.也可以使用构造器生成一个空白对话框 * 2.生成对话框 * 3.显示对话框 */ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); builder.setMessage("你确认要退出吗?"); builder.setPositiveButton("不走不走", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tip("不走不走"); } }); builder.setNegativeButton("拜拜了您呐", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tip("拜拜了您呐"); } }); AlertDialog dialog = builder.create(); dialog.show(); } /*链式调用*/ public void click02(View v) { new AlertDialog.Builder(this).setTitle("提示").setMessage("你确认要退出吗?") .setPositiveButton("不走不走", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tip("不走不走"); } }).setNegativeButton("拜拜了您呐", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tip("拜拜了您呐"); } }).create().show(); } /* * 1.所有选项, * 2.默认选中的索引 * 3.每次选择的监听器 */ String[] subjects = new String[]{"语文","数学","英语"}; int mTabIndex=1; public void click03(View v) { new AlertDialog.Builder(this) .setTitle("请选择你最喜欢的学科") .setSingleChoiceItems(subjects, mTabIndex, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mTabIndex=which; } }) .setPositiveButton("不走不走", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) {// tip(subjects[mTabIndex]); Toast.makeText(MainActivity.this, subjects[mTabIndex], 0).show(); } }).setNegativeButton("拜拜了您呐", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tip("拜拜了您呐"); } }).create().show(); } /* 弹出已选择的选项 * 1.遍历flags * 2.如果某个item为true,那么应该找到对应的数据 * */ boolean[] flags = new boolean[]{false,true,true}; public void click04(View v) { new AlertDialog.Builder(this) .setTitle("请选择你最喜欢的学科") .setMultiChoiceItems(subjects, flags, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { flags[which]=isChecked; } }) .setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String str = ""; for (int i = 0; i < flags.length; i++) { if (flags[i]) { str+=subjects[i]+" "; } } Toast.makeText(MainActivity.this, str, 0).show(); } }) .create() .show(); } /* 3秒后对话框消失 * 1.使用Timer定时器 * 2.dismiss() * 定时器 timer,定时器内部的操作是在子线程中 * */ public void click05(View v){ final ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage("角色删除中,请等待..."); dialog.setTitle("尊敬的玩家"); dialog.show(); new Timer().schedule(new TimerTask() { @Override public void run() { dialog.dismiss(); } }, 3000); }
PopupWindow
// pop.showAsDropDown(anchor); 下拉方式展示 anchor在指定的控件下展示// pop.showAsDropDown(anchor, xoff, yoff) ;yoff,xoff就是偏移量 ,默认左下角// 要使它在正中间,就需要在外面再嵌套一个布局,再使用xoff去调整// pop.showAtLocation(parent, gravity, x, y) 在指定位置展示 @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn: showPopupWindow(); break; case R.id.all_tv: Toast.makeText(this, "第二个子选项", 0).show(); pop.dismiss(); break; } } /*点击外部使pop消失 * 1.给外面的控件一个焦点/ 默认打开pop后内部控件获取焦点 * pop.setOutsideTouchable(true);如果失效,是因为一个bug,在没有给pop一个背景的情况下就会出现此情况; * 相对的,无法有时候内部空间爱你无法获取焦点,就需要给他们一个焦点 * 2. pop.setFocusable(true); * pop.setOutsideTouchable(true); * pop.setBackgroundDrawable(new BitmapDrawable()); * */ PopupWindow pop; private void showPopupWindow() { View contentView = LayoutInflater.from( MainActivity.this).inflate(R.layout.pop_view, null); contentView.findViewById(R.id.all_tv).setOnClickListener(this); pop = new PopupWindow(contentView, 400,ViewGroup.LayoutParams.WRAP_CONTENT); pop.setFocusable(true); pop.setOutsideTouchable(true); pop.setBackgroundDrawable(new BitmapDrawable()); pop.showAsDropDown(btn, 0, 0); } public void myclick(View v){ }
自定义PopupWindow的布局
/* * 1.创建一个只有自定的listview * 2.获取listview的inflater * 3.获取listview * 4.创建一个ArrayAdapter * 5.lv设置adapter * 6.创建pop对象 * 7.pop创建按钮和展示方式 * */ PopupWindow pop; private void showPopupWindow() { View contentView = LayoutInflater.from( MainActivity.this).inflate(R.layout.pop_view, null); ListView lv =(ListView)contentView.findViewById(R.id.lv); ArrayAdapter<String> adapter =new ArrayAdapter<String>( this ,android.R.layout.simple_expandable_list_item_1 ,android.R.id.text1 ,new String[]{"全部","我的微博","周边","特别关注"}); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "onitemclick", 0).show(); } }); pop=new PopupWindow(contentView,200,ViewGroup.LayoutParams.WRAP_CONTENT); pop.setFocusable(true); pop.setOutsideTouchable(true); pop.setBackgroundDrawable(new BitmapDrawable()); pop.showAsDropDown(btn, 0, 0); }
自定义AlertDialog
/*需求:自定义一个对话框 * 1.创建一个class并继承AlertDialog * 2.弹出一个对话框,需要实现oncreate(),并且在内部调用setconvertview * setContentView(R.layout.activity_main); * 3.在layout中创建一个xml文件 * 4.显示对话框 可以自定义背景/大小 但只能在屏幕的正中间 * */ public class MyAdapter extends AlertDialog { protected MyAdapter(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.my_dialog_layout); } MyAdapter dialog = new MyAdapter(this); dialog.show();}
ListView添加头布局和脚布局
/*需求:做一个有头部和文本的简单列表 * 1.创建一个简单列表 * 2.创建头部xml,并setheaderview(inflate(xml)); * 注意需要在调用setAdapter方法前调用 * 3.setadapter(adapter); * ps;涉及到头部的做法有个bug * 在调用item的监听的时候,点击的对象的索引会+1,因为把头部也视为了一个item * */View headerView = LayoutInflater.from(this).inflate(R.layout.lv_header_layout, null); lv.addHeaderView(headerView); lv.addFooterView(headerView);
单元测试
public class MockCalcultorDao { public int add(int x , int y){ return x+y; }}/*测试类 * 1.创建一个类并继承于AndroidTestCase * 2.创建测试方法testXXX() * 1.创建一个被测试的对象并进行相关操作 * 2.断言真实值和预期值 * assertEquals(expected,actual); * */public class CalcultorDaoTestCase extends AndroidTestCase { public void testAdd(){ MockCalcultorDao dao = new MockCalcultorDao(); int actualResult =dao.add(3,5); assertEquals(8,actualResult); }}
手机的存储结构
/* * 1.手机的内部存储 * Environment.getDataDirectory(); * /data该目录下无法直接存储文件 * * 2.手机的外部存储 * Environment.getExternalStorageDirecetory(); * /mnt/sdcard 此为标准路径,也就是说还有别的路径 * /mnt/storyage01 * /mount/storyage02 */将某个文件转移到SD卡 /*需求:将某个avi文件转移到sd * 1.因为sd不一定存在,每次使用sd卡的时候需要判断sd是否安装 * Environment.getExternalStorageState() * MEDIA_MOUNTED已安装的状态 * 2.存在的情况下,在sd卡存储文件 * 3.往文件里模拟下载 模拟创建一个空的文件(只有文件大小,没有内容) * 4.往sd卡传输数据是敏感操作,需要在manifest声明 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> * READ_是写权限 * */ public void startDownload(View v) { try { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File file = new File(Environment.getExternalStorageDirectory(), "葫芦娃.avi"); FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[1024*1024]; for (int i = 0; i < 5; i++) { fos.write(buffer); } fos.close(); Log.v("me", file.exists()+" "); } } catch (Exception e) { } }
阅读全文
0 0
- Android基础_常见Adapter/ExpandListView/Dialog/PopupWindow/自定义AlertDialog/单元测试/转移文件到SD卡(四)
- Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)
- Android之自定义AlertDialog和PopupWindow实现(仿微信Dialog)
- android 对话弹出框(Dialog AlertDialog PopupWindow)
- Android应用支持转移到SD卡
- Android弹出框PopupWindow,Dialog,AlertDialog.Builder,AlertDialog的一知半解
- 0828Android基础自定义Dialog+DatePickerDialog+TimePickerDialog+PopupWindow+Notification
- 让你的Android应用支持转移到SD卡
- android软件从内存转移到SD卡
- Android:应用程序转移到USB存储器(SD卡)
- 让你的Android应用支持转移到SD卡
- Android手机应用程序转移到SD卡操作
- 让你的Android应用支持转移到SD卡
- 26_保存文件到SD卡
- 自定义PopupWindow和AlertDialog
- android自定义tabwidget,popupwindow,spinner,dialog,listview
- Android自定义Dialog,Toast,Notification和PopupWindow
- 【Android】AlertDialog PopupWindow对话框
- 2017-08-12 NOIp
- 人类如何思考
- [HDU 5383] Yu-Gi-Oh!
- HDU 4398 STL + 贪心
- 顺序存储的二叉树的最近的公共祖先问题
- Android基础_常见Adapter/ExpandListView/Dialog/PopupWindow/自定义AlertDialog/单元测试/转移文件到SD卡(四)
- 进程与线程的关系
- zoj1494 暴力模拟 简单数学问题
- squid3正向代理
- 读书笔记:利用Python进行数据分析【第一章:准备工作】
- 函数式编程
- Fragment 生命周期完整版
- C++(7)模板与泛型编程
- MySQL性能分析之explain的使用