Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.content_providers
- import android.database.Cursor
- import android.net.Uri
- import android.os.Bundle
- import android.provider.ContactsContract
- import android.util.Log
- import android.widget.SimpleCursorAdapter
- import androidx.appcompat.app.AppCompatActivity
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- val TAG = "MainActivity"
- val uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
- val SELECTED_COLUMNS = arrayOf(
- ContactsContract.CommonDataKinds.Phone._ID, // needed by simplecursoradapter
- ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
- ContactsContract.CommonDataKinds.Phone.NUMBER
- )
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- doContentProvidersWithAdapter()
- }
- fun doContentProvidersWithAdapter() {
- val cursor: Cursor? = contentResolver.query(uri, SELECTED_COLUMNS, null, null, null)
- if (cursor != null) {
- // link the SELECTED_COLUMNS to some view IDs. S
- val toViews = intArrayOf(
- -1, // since we don't want to display Phone_ID, we are assigning -1 to it, which doesn't match to any real View in our layout
- R.id.name_textview,
- R.id.number_textview)
- val cursorAdapter = SimpleCursorAdapter(
- this, // context
- R.layout.list_row, // layout that defines the views for this list item
- cursor, // the database cursor
- SELECTED_COLUMNS, // "From" - column names of the data to bind to the UI
- toViews, // TextViews that should display column in the "from" param
- 0) // flags used to determine the behaviour of the adapter
- main_listview.setAdapter(cursorAdapter)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement