Android Projects

Android Development Books

Wednesday

Content provides user tutorial

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.



Share this post
  • Share to Facebook
  • Share to Twitter
  • Share to Google+
  • Share to Stumble Upon
  • Share to Evernote
  • Share to Blogger
  • Share to Email
  • Share to Yahoo Messenger
  • More...

0 comments

Thanks for your comment

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

Related Posts Plugin for WordPress, Blogger...
© Google Android Lovers
Designed by BlogThietKe Cooperated with Duy Pham
Released under Creative Commons 3.0 CC BY-NC 3.0