Content User tutorials followed by following codes....
1.ContentProvide.java
package com.horror.android;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ContentProvider extends Activity {
private static final String TAG = "ContentProvider";
private ArrayList<String> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver r = getContentResolver();
Cursor cursor = r.query(People.CONTENT_URI, null, null, null, null);
// Let activity manage the cursor
startManagingCursor(cursor);
Log.d(TAG, "cursor.getCount()=" + cursor.getCount());
// Get value from content provider
int nameOfIndex = cursor.getColumnIndexOrThrow(People.NAME);
int numberOfIndex = cursor.getColumnIndexOrThrow(People.NUMBER);
cursor.moveToFirst();
list = new ArrayList<String>();
do {
String name = cursor.getString(nameOfIndex);
String number = cursor.getString(numberOfIndex);
list.add(name + ": " + number);
} while (cursor.moveToNext());
ListView l = (ListView) findViewById(R.id.list);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
l.setAdapter(aa);
}
}
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list">
</ListView>
</LinearLayout>
3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.horror.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ContentProvider"
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>
Plese send me your valuable comment about this article.
1.ContentProvide.java
package com.horror.android;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ContentProvider extends Activity {
private static final String TAG = "ContentProvider";
private ArrayList<String> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver r = getContentResolver();
Cursor cursor = r.query(People.CONTENT_URI, null, null, null, null);
// Let activity manage the cursor
startManagingCursor(cursor);
Log.d(TAG, "cursor.getCount()=" + cursor.getCount());
// Get value from content provider
int nameOfIndex = cursor.getColumnIndexOrThrow(People.NAME);
int numberOfIndex = cursor.getColumnIndexOrThrow(People.NUMBER);
cursor.moveToFirst();
list = new ArrayList<String>();
do {
String name = cursor.getString(nameOfIndex);
String number = cursor.getString(numberOfIndex);
list.add(name + ": " + number);
} while (cursor.moveToNext());
ListView l = (ListView) findViewById(R.id.list);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
l.setAdapter(aa);
}
}
2.main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list">
</ListView>
</LinearLayout>
3.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.horror.android"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ContentProvider"
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>
Plese send me your valuable comment about this article.
0 comments
Thanks for your comment