Adapter基础讲解-SimpleCursorAdapter使用示例
2017-09-12 16:41
274 查看
虽然这东西过时了,不过对于不怎么会SQLite的初学者来说,用起来还是蛮方便的!记得前面我们学ContentProivder写过的读取联系人的例子么?之前是通过打印Log的方式显示出来,现在我们通过这个SimpleCursorAdapter把它显示到ListView上!
实现效果图:
代码实现:
先写下listView每个item的布局:
list_item.xml:
接着activity_main布局和前面的一样,就是简单的ListView,然后是
MainActivity.java:
最后AndroidManifest.xml里加个读联系人的权限就可以了!
实现效果图:
代码实现:
先写下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以上需要动态申请权限)
相关文章推荐
- Adapter基础讲解-SimpleAdapter使用示例:
- Adapter基础讲解-ArrayAdapter使用示例
- Android基础学习之BaseAdaptery以及SimpleCursorAdapter的使用
- android——ListView(使用simpleCursorAdapter)
- android SimpleCursorAdapter的使用
- Android开发模板------自定义SimpleCursorAdapter的使用
- 使用SimpleCursorAdapter
- 简单使用SimpleCursorAdapter
- SimpleCursorAdapter和ListView的结合使用
- Android开发模板------自己定义SimpleCursorAdapter的使用
- GridView的使用(三):与SimpleCursorAdapter搭配使用
- Android中Adapter之SimpleCursorAdapter使用
- 简单使用SimpleCursorAdapter
- 使用SimpleCursorAdapter的ListView如何更新显示
- SimpleAdapter 和 SimpleCursorAdapter的使用
- 【Android基础】listview控件的使用(3)------Map与SimpleAdapter组成的多显示条目的Listview
- SimpleCursorAdapter的简单使用方法
- android中使用CursorAdapter和SimpleCursorAdapter的方法
- Android中Adapter之SimpleCursorAdapter使用
- 简单使用SimpleCursorAdapter