Adapter基础讲解-SimpleCursorAdapter使用示例
来源:互联网 发布:广联达装修预算软件 编辑:程序博客网 时间:2024/06/06 00:54
虽然这东西过时了,不过对于不怎么会SQLite的初学者来说,用起来还是蛮方便的!记得前面我们学ContentProivder写过的读取联系人的例子么?之前是通过打印Log的方式显示出来,现在我们通过这个SimpleCursorAdapter把它显示到ListView上!
实现效果图:
代码实现:
先写下listView每个item的布局:
list_item.xml:
<?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:orientation="horizontal"> <TextView android:id="@+id/list_name" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:gravity="center" android:text="小猪" android:textColor="#0000FF" android:textSize="18sp" /> <TextView android:id="@+id/list_phone" android:layout_width="0dp" android:layout_height="64dp" android:layout_weight="1" android:gravity="center" android:text="13798989898" android:textColor="#EA5C4D" android:textSize="18sp" /></LinearLayout>
接着activity_main布局和前面的一样,就是简单的ListView,然后是
MainActivity.java:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView list_test = (ListView) findViewById(R.id.list_test); //读取联系人 Cursor cursor = getContentResolver() .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); SimpleCursorAdapter spcAdapter = new SimpleCursorAdapter(this,R.layout.list_item,cursor, new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER}, new int[]{R.id.list_name,R.id.list_phone}); list_test.setAdapter(spcAdapter); }}
最后AndroidManifest.xml里加个读联系人的权限就可以了!
<uses-permission android:name="android.permission.READ_CONTACTS"/>(6.0以上需要动态申请权限)
阅读全文
0 0
- Adapter基础讲解-SimpleCursorAdapter使用示例
- Adapter基础讲解-ArrayAdapter使用示例
- Adapter基础讲解-SimpleAdapter使用示例:
- Android中Adapter之SimpleCursorAdapter使用
- Android中Adapter之SimpleCursorAdapter使用
- Adapter基础讲解
- 继承SimpleCursorAdapter定制Adapter
- AdapterView<Adapter>使用示例
- 使用SimpleCursorAdapter
- Adapter基础讲解-MVC模式的简单理解和Adapter概念解析
- RecyclerView 使用示例,包含adapter
- Android基础学习之BaseAdaptery以及SimpleCursorAdapter的使用
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- 使用详解及源码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
- android 部分原生Adapter简介之SimpleCursorAdapter
- 与数据库打交道的Adapter----SimpleCursorAdapter
- Android-Adapter实现方法(ArrayAdapter,SimpleAdapter,SimpleCursorAdapter)
- Android图文混排实现方式详解
- 594. Longest Harmonious Subsequence
- LeetCode Task Scheduler
- 安装Nginx依赖的包到CentOS 6.5
- 机器学习-梯度下降VI(学习率)
- Adapter基础讲解-SimpleCursorAdapter使用示例
- 获取输入框的值,计算出的支付费用
- 算法 第四版 2.3.9
- IntelliJ IDEA创建maven多模块项目
- vue.js移动数组位置,同时更新视图
- 百宝云人事信息管理平台_免费搭建人事管理系统
- Ubuntu使用apt-get安装Apache2报错时的解决方案
- java分页详解
- QListWidget中项的灰显设置