ContentProvider User Demo

Content Providers are the Android platforms way of sharing information betweeen multiple applications through its ContentResolver interface. Each application has access to the SQLite database to maintain their information and this cannot be shared with another application.

In this demo, we use the content provider information avaialable through the getContentResolver() method to get device information with the contentresolver instance and quierying the provided cursor. Apart from query, you can with the related methods, the ability to insert, update, delete and getType (to extract the MIME type).

End result is the list of all the system setting on the user on the android device.

With the SimpleCursorAdapter instance makes use of the row.xml with cursor information to populate the listView withing the main.xml layout.

ContentUserDemo.java
Code:

package org.example.cp;


import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public class ContentUserDemo extends Activity {
private static final String TAG = "ContentUserDemo";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Get content provider and cursor
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Settings.System.CONTENT_URI, null, null, null, null);

// Let activity manage the cursor
startManagingCursor(cursor);
Log.d(TAG, "cursor.getCount()=" + cursor.getCount());

// Get the list view
ListView listView = (ListView) findViewById(R.id.listView);
String[] from = { Settings.System.NAME, Settings.System.VALUE };
int[] to = { R.id.textName, R.id.textValue };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listView.setAdapter(adapter);
}
}

Addition to the AndroidManifest.xml

Do not forget to add the following user permission tag <uses-permission android:name="android.permission.READ_CONTACTS" /> To givee your application access to the contacts information.


AndroidManifest.xml

Code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="org.example.cp" android:versionCode="1" android:versionName="1.0.0">
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ContentUserDemo" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


row.xml

Where the individual row information is populated.
Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:padding="5sp"
android:layout_width="fill_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/textName"
android:text="Name"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_weight="1"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textValue"
android:text="Value"
android:textSize="18sp"
android:gravity="right"></TextView>
</LinearLayout>


main.xml

Where all the rows.xml populate the ListView tag
Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/listView">
</ListView>
</LinearLayout>

ScreenShot


Source
https://www.protechtraining.com/static/tutorials/ContentUserDemo.zip

Copyright © 2018 ProTech. All Rights Reserved.

Sign In Create Account

Navigation

Social Media