Hi Everyone,

I am writing program for which I am creating my own ContentProvider. I
want a database of people (by name) and the debt that they owe to the
user (it's an app to keep track of debt). However, it is refusing to
run. I have posted below the ContentProvider that I use. The Activity
class that calls on this provider and the manifest. But no luck in
getting it run. Any ideas?


Here is the ContentProvider:

package com.Split.ourfirstandroidapp;

import android.content.*;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.*;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.text.*;
import android.util.*;


public class SplitProvider extends ContentProvider {
        public static final Uri CONTENT_URI = Uri.parse("content://
com.Split.ourfirstandroidapp.provider.friend/friends");
        private SQLiteDatabase splitDB;
        private static final String TAG = "SplitProvider";
        private static final String DATABASE_NAME = "split.db";
        private static final String DATABASE_TABLE = "friends";
        private static final int DATABASE_VERSION = 1;

        //Column names
        public static final String KEY_ID = "_id";
        public static final String KEY_NAME = "name";
        public static final String KEY_DEBT = "debt";

        //Column indices

        public static final int NAME_COLUMN = 1;
        public static final int DEBT_COLUMN = 2;
        //URI request handles
        private static final int FRIENDS = 1;
        private static final int FRIENDS_ID = 2;

        private static final UriMatcher uriMatcher;

        static {
                uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
                uriMatcher.addURI("com.Split.provider.friend", "friends", 
FRIENDS);
                uriMatcher.addURI("com.Split.provider.friend", "friends/#",
FRIENDS_ID);
        }


        //nested helper class start
        private static class SplitDatabaseHelper extends SQLiteOpenHelper{
                private static final String DATABASE_CREATE =
                        "create table" + DATABASE_TABLE + " ("
                        + KEY_ID + " integer primary key autoincrement, "
                        + KEY_NAME + " TEXT, "
                        + KEY_DEBT + " INT)";

                public SplitDatabaseHelper (Context context, String name,
CursorFactory factory, int version){
                        super(context, name, factory, version);
                }

                @Override
                public void onCreate(SQLiteDatabase db) {
                        db.execSQL(DATABASE_CREATE);
                }
                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
                        Log.w(TAG, "Upgrading database from version " + 
oldVersion + " to
"+ newVersion
                                        + ", which will destroy old data");
                        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
                        onCreate(db);
                }
        }
        //nested helper class end!

        @Override
        public int delete(Uri uri, String where, String[] whereArgs) {
                int count;
                switch (uriMatcher.match(uri)){
                case FRIENDS:
                        count = splitDB.delete(DATABASE_TABLE, where, 
whereArgs);
                        break;

                case FRIENDS_ID:
                        String segment = uri.getPathSegments().get(1);
                        count = splitDB.delete(DATABASE_TABLE, KEY_ID + "="
                                        + segment + (!TextUtils.isEmpty(where)? 
" AND (" + where + ')' :
""),
                                        whereArgs);
                        break;
                        default: throw new 
IllegalArgumentException("Unsupported URI: "+
uri);
                }

                getContext().getContentResolver().notifyChange(uri, null);
                return count;
        }

        @Override
        public String getType(Uri uri) {
                switch (uriMatcher.match(uri)){
                case FRIENDS: return "vnd.android.cursor.dir/
vnd.Split.ourfirstandroidapp.friend";
                case FRIENDS_ID: return "vnd.android.cursor.item/
vnd.Split.ourfirstandroidapp.friend";
                default: throw new IllegalArgumentException("Unsupported URI: "
+uri);
                }
        }

        @Override
        public Uri insert(Uri _uri, ContentValues _initialValues) {

                long rowID = splitDB.insert(DATABASE_TABLE, "friend",
_initialValues);
                if (rowID > 0){
                        Uri uri = ContentUris.withAppendedId(CONTENT_URI, 
rowID);
                        getContext().getContentResolver().notifyChange(uri, 
null);
                        return uri;
                }
                throw new SQLException ("Failed to insert row into " + _uri);

        }

        @Override
        public boolean onCreate() {
                Context context = getContext();

                SplitDatabaseHelper dbHelper = new SplitDatabaseHelper(context,
DATABASE_NAME,
                                null, DATABASE_VERSION);
                splitDB = dbHelper.getWritableDatabase();
                return (splitDB == null) ? false : true;
        }

        @Override
        public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sort) {
                SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
                qb.setTables(DATABASE_TABLE);

                switch (uriMatcher.match(uri)){
                case FRIENDS_ID: qb.appendWhere(KEY_ID + "="+
uri.getPathSegments().get(1));
                break;
                default: break;
                }

                String orderBy;
                if (TextUtils.isEmpty(sort)){
                        orderBy = KEY_NAME;
                } else{
                        orderBy = sort;
                }

                Cursor c = qb.query(splitDB, projection, selection, 
selectionArgs,
null,null, orderBy);
                c.setNotificationUri(getContext().getContentResolver(), uri);

                return c;
        }

        @Override
        public int update(Uri uri, ContentValues values, String where,
                        String[] whereArgs) {
                int count;
                switch (uriMatcher.match(uri)){
                case FRIENDS:
                        count = splitDB.update(DATABASE_TABLE, values, where, 
whereArgs);
                        break;

                case FRIENDS_ID:
                        String segment = uri.getPathSegments().get(1);
                        count = splitDB.update(DATABASE_TABLE, values, KEY_ID + 
"="
                                        + segment + (!TextUtils.isEmpty(where)? 
" AND (" + where + ')' :
""),
                                        whereArgs);
                        break;
                        default: throw new IllegalArgumentException("Unknown 
URI: "+ uri);
                }

                getContext().getContentResolver().notifyChange(uri, null);
                return count;
        }
}


HERE IS THE ACTIVITY CLASS:

package com.Split.ourfirstandroidapp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class EvenSplit extends Activity {
        ListView friendsListView;
        ArrayAdapter<Friend> aa;

        ArrayList<Friend> friends = new ArrayList<Friend>();
        @Override
        public void onCreate(Bundle icicle){
                super.onCreate(icicle);
       setContentView(R.layout.evensplit);
                friendsListView = (ListView)this.findViewById(R.id.friendsList);
                aa = new ArrayAdapter<Friend>(this, R.layout.item_friendlist,
friends);
                friendsListView.setAdapter(aa);

       /* final Uri data = Uri.parse("content://
com.Split.provider.friend/");
              final Cursor c = managedQuery(SplitProvider.CONTENT_URI, null,
null, null, null);
              String[] from = new String[] {SplitProvider.KEY_NAME};
              int[] to = new int[] { R.id.item_friend};
              SimpleCursorAdapter adapter = new
SimpleCursorAdapter(this,R.layout.item_friendlist,c,from,to);
              ListView lv = (ListView)findViewById(R.id.friendsList);
              lv.setAdapter(adapter);
        */

                loadFriendsFromProvider();
                Friend juhi = new Friend("Juhi", 2);
                addNewFriend(juhi);

        }

        private void addFriendToArray(Friend _friend){
                if(_friend.getDebt() != 0){
                        friends.add(_friend);
                        aa.notifyDataSetChanged();
                }
        }


        private void addNewFriend(Friend _friend){
                ContentResolver cr = getContentResolver();
                String w = SplitProvider.KEY_NAME + "=" + _friend.getName();
                //if the friend is new!
                if (cr.query(SplitProvider.CONTENT_URI, null, w, null,
null).getCount()==0){
                        ContentValues values = new ContentValues();
                        values.put(SplitProvider.KEY_NAME, _friend.getName());
                        values.put(SplitProvider.KEY_DEBT, _friend.getDebt());

                        cr.insert(SplitProvider.CONTENT_URI, values);
                        friends.add(_friend);
                        addFriendToArray(_friend);
                }

        }

        private void loadFriendsFromProvider(){
                friends.clear();
                ContentResolver cr = getContentResolver();
                Cursor c = cr.query(SplitProvider.CONTENT_URI, null, null, null,
null);

                if(c.moveToFirst()){
                        do{
                                String name = 
c.getString(SplitProvider.NAME_COLUMN);
                                int debt = c.getInt(SplitProvider.DEBT_COLUMN);

                                Friend f = new Friend(name, debt);
                                addFriendToArray(f);
                        }
                        while(c.moveToNext());
                }

        }


}


Here is the Manifest!!

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.Split.ourfirstandroidapp"
      android:versionCode="1"
      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/
app_name">

        <activity android:name=".OurFirstAndroidApp"
                  android:label="Our First Android App">
            <intent-filter>
              <action android:name="android.intent.action.MAIN" />
             <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

         <activity android:name=".AddNewEvent"
                  android:label="Add New Event">
            <intent-filter>
                <action android:name = "android.intent.action.PICK"></
action>
                        <category android:name =
"android.intent.category.DEFAULT"></category>
                        <data android:path = "contacts"
android:scheme="content"></data>
            </intent-filter>
        </activity>

        <activity android:name=".ContactPickerTester"
                  android:label="Contact Picker Tester">

        </activity>

    <activity android:name="SplitMethod"
android:label=".SplitMethod">

    </activity>

    <activity android:name="EvenSplit" android:label=".EvenSplit">
    <intent-filter>

    </intent-filter>
    </activity>

    <activity android:name="ManualSplit"
android:label=".ManualSplit">

    </activity>

      <provider android:name = ".SplitProvider"
 
android:authorities="com.Split.ourfirstandroidapp.provider.friend">

        </provider>

</application>
    <uses-permission android:name="android.permission.READ_CONTACTS"></
uses-permission>

    <uses-sdk android:minSdkVersion="3" />


</manifest>

Any help is appreciated! Thanks! :)

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to