Android中通过Tag为View保存数据绑定数据
来源:互联网 发布:vb数字金字塔 编辑:程序博客网 时间:2024/06/05 14:26
项目中有时候需要为View绑定数据,比如每一个云标签都对应服务器一个标签id,View中setTag可以方便的为控件绑定数据。
为控件绑定数据:
selectCategory.setTag(R.id.tag_id, id);selectCategory.setTag(R.id.tag_type, type);读取控件绑定的数据:
Object object=selectCategory.getTag(R.id.tag_id);if(object!=null&& object instanceof Integer){ ...setTag的源码:
/** * Sets a tag associated with this view and a key. A tag can be used * to mark a view in its hierarchy and does not have to be unique within * the hierarchy. Tags can also be used to store data within a view * without resorting to another data structure. * * The specified key should be an id declared in the resources of the * application to ensure it is unique (see the <a * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>). * Keys identified as belonging to * the Android framework or not associated with any package will cause * an {@link IllegalArgumentException} to be thrown. * * @param key The key identifying the tag * @param tag An Object to tag the view with * * @throws IllegalArgumentException If they specified key is not valid * * @see #setTag(Object) * @see #getTag(int) */public void setTag(int key, final Object tag) { // If the package id is 0x00 or 0x01, it's either an undefined package // or a framework id if ((key >>> 24) < 2) { throw new IllegalArgumentException("The key must be an application-specific " + "resource id."); } setKeyedTag(key, tag);}可以为View绑定key-value数据,但是key不能随便取值,必须取资源文件中资源id,保证id的唯一
如可在string.xml中设置两个常量
<item name="tag_id" type="id"></item><item name="tag_type" type="id"></item>
0 0
- Android中通过Tag为View保存数据绑定数据
- 用android view中的tag传送数据
- Android 通过JSON保存数据
- MVC中Partial View的数据绑定
- C#中通过反射进行数据绑定
- 【Android基础】通过SharedPreferences来保存数据
- Unity中通过PlayerPrefs进行数据保存
- winform中为CheckedListBox绑定数据
- 为AspxGridView绑定数据
- 从源码上分析Android View保存数据状态
- 通过视图进行数据绑定
- 通过自定义数据绑定类实现MVC中图片上传
- Android--将数据保存到SDCard中
- android中Json数据保存方式
- Android--将数据保存到SDCard中
- android -将数据保存在文件中
- 保存数据到Android设备中
- Android中突发情况数据的保存
- Hdu2177-博弈(Sg函数找规律)
- ECNU_OJ_1000
- eclipse的设置
- 抓包
- Codeforces 633H. Fibonacci-ish II (Mo's Algorithm(莫队算法) + 线段树 + 离散化)
- Android中通过Tag为View保存数据绑定数据
- [leetcode 309]Best Time to Buy and Sell Stock with Cooldown--买卖股票with冷却期(DP法)
- iconv转换文件编码
- UIPopover 的简单用法
- Tomcat部署网站
- 小猫统计支持导入通达信数据到mysql了
- ubuntu发现wifi无法连接的解决方法
- 热块争用的解决方法(原创)
- Android应用开发性能优化完全分析