{"id":6228,"date":"2014-04-13T23:31:41","date_gmt":"2014-04-13T23:31:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/sqlitedatabase-every-username-has-the-same-list-collection-of-common-programming-errors\/"},"modified":"2014-04-13T23:31:41","modified_gmt":"2014-04-13T23:31:41","slug":"sqlitedatabase-every-username-has-the-same-list-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/sqlitedatabase-every-username-has-the-same-list-collection-of-common-programming-errors\/","title":{"rendered":"SQLitedatabase every username has the same list-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/f300c9887f4486fce09a63c9267dfc3c?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nBananeweizen<\/p>\n<p>In my application&#8217;s database I have 2 tables one called &#8220;PLAYERS&#8221; which holds the data of every user and &#8220;FRIENDSLIST&#8221; which is intended to be a personal &#8220;friends&#8221; list for every registered account.How the process goes in my app is that when a player (Joe) registers a Username his information goes into the &#8220;PLAYERS&#8221; table and now he could go into my FindFriends activty,search the app for his friend&#8217;s Username (Bob) and then clicks the ADD button.<\/p>\n<p>The friend (Bob) then gets added to the &#8220;FRIENDSLIST&#8221; table.When he goes into the PlayAFriend activity a list will load showing the &#8220;friends&#8221; that he added.All this works good until I realized that when I log into one of his friend&#8217;s account (Bob) ,that friend has the same list that (Joe) has.<\/p>\n<p>I know that the cursor I&#8217;m using to load up (Joe)&#8217;s friends list is the same cursor I&#8217;m using to load up (Bob)&#8217;s friends list so the question I&#8217;m asking is,<\/p>\n<p>how would I make Joe and Bob have their own personal friends list?<\/p>\n<p>I&#8217;ve tried searching on this website and googling to find any examples on how to accomplish this but I find it crazy that I&#8217;m not finding any tutorials or posts on this subject even though this type of practice is used in every popular app made.Sorry for the long post I know it&#8217;s not a simple question so that&#8217;s why I had to explain in detail on what the problem is.<\/p>\n<p>I&#8217;m posting up my DBAdapter class to show if how I made it would accommodate this and my PlayAFriend class to show my Cursor and ListView in action.<\/p>\n<p>DBAdapter class<\/p>\n<pre><code>package com.fullfrontalgames.numberfighter;\n\nimport android.content.ContentValues;\nimport android.content.Context;\nimport android.database.Cursor;\nimport android.database.SQLException;\nimport android.database.sqlite.SQLiteDatabase;\nimport android.database.sqlite.SQLiteException;\nimport android.database.sqlite.SQLiteOpenHelper;\nimport android.util.Log;\n\npublic class DBAdapter {\n    static final String KEY_ROWID = \"_id\";\n\n    static final String KEY_USERNAME = \"USERNAME\";\n\n    static final String KEY_PASSWORD = \"PASSWORD\";\n\n    static final String KEY_EMAIL = \"EMAIL\";\n\n    static final String KEY_NUMBERINPUT = \"NUMBERINPUT\";\n\n    static final String KEY_SCORE = \"SCORE\";\n\n    static final String KEY_FRIENDS = \"FRIENDS\";\n\n    static final String TAG = \"DBAdapter\";\n\n    static final String DATABASE_NAME = \"NFDatabase\";\n\n    static final String DATABASE_TABLE1 = \"PLAYERS\";\n\n    static final String DATABASE_TABLE2 = \"FRIENDSLIST\";\n\n    static final int DATABASE_VERSION = 4;\n\n    static final String DATABASE_CREATE_TABLE1 = \"create table PLAYERS ( _id integer primary key autoincrement, \"\n            + \"USERNAME text not null unique,PASSWORD text not null,EMAIL text,NUMBERINPUT text,SCORE text);\";\n\n    static final String DATBASE_CREATE_TABLE2 = \"create table FRIENDSLIST (_id integer primary key autoincrement,\"\n            + \"FRIENDS text not null,USERNAME text ,NUMBERINPUT text,SCORE text);\";\n\n    final Context context;\n\n    DatabaseHelper DBHelper;\n\n    static SQLiteDatabase db;\n\n    public DBAdapter(Context ctx) {\n        this.context = ctx;\n        DBHelper = new DatabaseHelper(context);\n    }\n\n    private static class DatabaseHelper extends SQLiteOpenHelper {\n\n        public DatabaseHelper(Context context) {\n            \/\/ TODO Auto-generated constructor stub\n            super(context, DATABASE_NAME, null, DATABASE_VERSION);\n        }\n\n        @Override\n        public void onCreate(SQLiteDatabase db) {\n            \/\/ TODO Auto-generated method stub\n            try {\n                db.execSQL(DATABASE_CREATE_TABLE1);\n                db.execSQL(DATBASE_CREATE_TABLE2);\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n\n        @Override\n        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n            \/\/ TODO Auto-generated method stub\n            Log.w(TAG, \"Upgrading database from version \" + oldVersion + \" to \" + newVersion\n                    + \", which will destroy all old data\");\n            db.execSQL(\"DROP TABLE IF EXISTS PLAYERS\");\n            db.execSQL(\"DROP TABLE IF EXISTS FRIENDSLIST\");\n            onCreate(db);\n        }\n\n    }\n\n    public DBAdapter open() throws SQLiteException {\n        db = DBHelper.getWritableDatabase();\n        return this;\n    }\n\n    public void close() {\n        DBHelper.close();\n    }\n\n    \/\/ PLAYERS TABLE CRUD\n\n    public long insertPlayer(String Username, String Password, String Email) {\n        ContentValues initialValues = new ContentValues();\n        initialValues.put(\"USERNAME\", Username);\n        initialValues.put(\"PASSWORD\", Password);\n        initialValues.put(\"EMAIL\", Email);\n\n        db.insert(\"PLAYERS\", null, initialValues);\n\n        return db.insertWithOnConflict(\"PLAYERS\", null, initialValues,\n                SQLiteDatabase.CONFLICT_IGNORE);\n    }\n\n    public boolean deletePlayer(long id) {\n        Log.d(TAG, \"delete is working\");\n        return db.delete(\"PLAYERS\", KEY_ROWID + \" = \" + id, null) &gt; 0;\n\n    }\n\n    public Cursor getAllPlayers() {\n        return db.query(false, \"Players\", new String[] {\n                \"_id\", \"USERNAME\", \"PASSWORD\", \"EMAIL\"\n        }, null, null, null, null, null, null);\n\n    }\n\n    public String getData() {\n        String[] columns = new String[] {\n                \"_id\", \"USERNAME\", \"PASSWORD\"\n        };\n        Cursor mCursor = db.query(\"PLAYERS\", columns, null, null, null, null, null);\n        String result = \"\";\n        int iRow = mCursor.getColumnIndex(KEY_ROWID);\n        int iName = mCursor.getColumnIndex(KEY_USERNAME);\n\n        for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {\n            result = mCursor.getString(iRow) + \" \" + mCursor.getString(iName) + \"\/n\";\n        }\n        return result;\n    }\n\n    public Cursor getPlayer(int _id) throws SQLException {\n        Cursor mCursor = db.query(true, \"PLAYERS\", new String[] {\n                \"_id\", \"USERNAME\"\n        }, KEY_ROWID + \" =  \" + \"_id\", null, null, null, null, null);\n        while (mCursor.moveToNext()) {\n            int id = mCursor.getInt(mCursor.getColumnIndex(\"_id\"));\n        }\n        return mCursor;\n    }\n\n    public String getUsername(String Username) {\n        Cursor mCursor = db.query(\"Players\", new String[] {\n            \"USERNAME\"\n        }, \"USERNAME = ?\", new String[] {\n            Username\n        }, null, null, null);\n        if (mCursor.moveToNext())\n            return mCursor.getString(0);\n        else\n            return \"\";\n    }\n\n    public String getSinlgeEntry(String Username) {\n        Cursor cursor = db.query(\"PLAYERS\", null, \" USERNAME=?\", new String[] {\n            Username\n        }, null, null, null);\n        if (cursor.getCount() &lt; 1) \/\/ UserName Not Exist\n        {\n            cursor.close();\n            return \"NOT EXIST\";\n        }\n        cursor.moveToFirst();\n        String password = cursor.getString(cursor.getColumnIndex(\"PASSWORD\"));\n        cursor.close();\n        return password;\n    }\n\n    public boolean updatePlayer(long _id, String Username, String Password, String Email) {\n        ContentValues args = new ContentValues();\n        args.put(\"USERNAME\", Username);\n        args.put(\"PASSWORD\", Password);\n        args.put(\"EMAIL\", Email);\n        return db.update(\"PLAYERS\", args, KEY_ROWID + \" = \" + _id, null) &gt; 0;\n    }\n\n    \/\/ FRIENDS TABLE CRUD\n\n    public void insertFriend(String Friend) {\n        ContentValues initialValues = new ContentValues();\n        initialValues.put(\"FRIENDS\", Friend);\n        db.insert(\"FRIENDSLIST\", null, initialValues);\n    }\n\n    public boolean deleteFriend() {\n        return db.delete(\"FRIENDSLIST\", KEY_ROWID + \" = \" + \"_id\", null) &gt; 0;\n    }\n\n    public Cursor getAllFriends() {\n        return db.query(false, \"FRIENDSLIST\", new String[] {\n                \"_id\", \"FRIENDS\"\n        }, null, null, null, null, null, null);\n    }\n\n    public Cursor getFriend(long rowid) throws SQLException {\n        Cursor mCursor = db.query(true, \"FRIENDSLIST\", new String[] {\n                \"_id\", \"FRIENDS\"\n        }, KEY_ROWID + \" =  \" + \"_id\", null, null, null, null, null);\n        if (mCursor != null) {\n            mCursor.moveToFirst();\n        }\n        return mCursor;\n    }\n\n    public boolean updateFriend(long _id, String Friends) {\n        ContentValues args = new ContentValues();\n        args.put(\"FRIENDS\", Friends);\n\n        return db.update(\"FRIENDSLIST\", args, KEY_ROWID + \" = \" + _id, null) &gt; 0;\n    }\n\n}\n<\/code><\/pre>\n<p>PlayAFriend Class<\/p>\n<pre><code>package com.fullfrontalgames.numberfighter;\n\nimport android.app.ListActivity;\nimport android.content.Context;\nimport android.database.Cursor;\nimport android.os.Bundle;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.View.OnClickListener;\nimport android.view.ViewGroup;\nimport android.widget.BaseAdapter;\nimport android.widget.Button;\nimport android.widget.ListAdapter;\nimport android.widget.TextView;\n\npublic class PlayAFriend extends ListActivity {\n\n    DBAdapter DBAdapter;\n\n    @Override\n    public void onCreate(Bundle savedInstanceState) {\n        \/\/ TODO Auto-generated method stub\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.playafriend);\n\n        final DBAdapter db = new DBAdapter(this);\n        DBAdapter = db.open();\n\n        getListView().setAdapter(new FriendsListAdapter(this, db.getAllFriends()));\n\n    }\n\n    private class FriendsListAdapter extends BaseAdapter implements OnClickListener, ListAdapter {\n        private Cursor fFriends;\n\n        private Context fContext;\n\n        public FriendsListAdapter(Context context, Cursor friends) {\n            \/\/ TODO Auto-generated constructor stub\n            fContext = context;\n            fFriends = friends;\n        }\n\n        @Override\n        public int getCount() {\n            \/\/ TODO Auto-generated method stub\n            return fFriends.getCount();\n        }\n\n        @Override\n        public FriendInfo getItem(int position) {\n            \/\/ TODO Auto-generated method stub\n            if (fFriends.moveToPosition(position)) {\n                String name = fFriends.getString(fFriends.getColumnIndex(\"FRIENDS\"));\n\n                return new FriendInfo(name);\n            }\n            return null;\n        }\n\n        @Override\n        public long getItemId(int position) {\n            \/\/ TODO Auto-generated method stub\n            fFriends.moveToPosition(position);\n            return fFriends.getLong(fFriends.getColumnIndex(\"_id\"));\n        }\n\n        @Override\n        public View getView(int position, View convertView, ViewGroup parent) {\n            \/\/ TODO Auto-generated method stub\n            if (convertView == null) {\n                convertView = LayoutInflater.from(fContext).inflate(R.layout.playafriendlistitems,\n                        parent, false);\n            }\n            FriendInfo friendinfo = getItem(position);\n            TextView friendTV = ViewHolder.get(convertView, R.id.textview_friends);\n            Button playbutton = ViewHolder.get(convertView, R.id.playbutton, position);\n            playbutton.setOnClickListener(this);\n\n            friendTV.setText(friendinfo.getName());\n\n            return convertView;\n        }\n\n        @Override\n        public void onClick(View v) {\n            \/\/ TODO Auto-generated method stub\n            DBAdapter db = new DBAdapter(fContext);\n            db.open();\n            int position = (Integer)v.getTag();\n            fFriends.moveToPosition(position);\n            long id = fFriends.getLong(fFriends.getColumnIndex(\"_id\"));\n            fFriends = db.getAllFriends();\n            db.close();\n        }\n\n    }\n\n}\n<\/code><\/pre>\n<p>New DBAdapter<\/p>\n<pre><code>package com.fullfrontalgames.numberfighter;\n\nimport android.content.ContentValues;\nimport android.content.Context;\nimport android.database.Cursor;\nimport android.database.SQLException;\nimport android.database.sqlite.SQLiteDatabase;\nimport android.database.sqlite.SQLiteException;\nimport android.database.sqlite.SQLiteOpenHelper;\nimport android.util.Log;\n\npublic class DBAdapter {\n    static final String KEY_ROWID = \"_id\";\n\n    static final String KEY_USERNAME = \"USERNAME\";\n\n    static final String KEY_PASSWORD = \"PASSWORD\";\n\n    static final String KEY_EMAIL = \"EMAIL\";\n\n    static final String KEY_NUMBERINPUT = \"NUMBERINPUT\";\n\n    static final String KEY_SCORE = \"SCORE\";\n\n    static final String KEY_FRIENDS = \"FRIENDS\";\n\n    static final String TAG = \"DBAdapter\";\n\n    static final String DATABASE_NAME = \"NFDatabase\";\n\n    static final String DATABASE_TABLE1 = \"PLAYERS\";\n\n    static final String DATABASE_TABLE2 = \"FRIENDSLIST\";\n\n    static final int DATABASE_VERSION = 9;\n\n    static final String DATABASE_CREATE_TABLE1 = \"create table PLAYERS ( _id integer primary key autoincrement, \"\n            + \"USERNAME text not null unique,PASSWORD text not null,EMAIL text,NUMBERINPUT text,SCORE text);\";\n\n    static final String DATBASE_CREATE_TABLE2 = \"create table FRIENDSLIST (_id integer primary key autoincrement,\"\n            + \"player_id integer not null references PLAYERS(_id) on update cascade on delete cascade,\"\n            + \"FRIENDS text ,NUMBERINPUT text,SCORE text);\";\n\n    final Context context;\n\n    DatabaseHelper DBHelper;\n\n    static SQLiteDatabase db;\n\n    public DBAdapter(Context ctx) {\n        this.context = ctx;\n        DBHelper = new DatabaseHelper(context);\n    }\n\n    private static class DatabaseHelper extends SQLiteOpenHelper {\n\n        public DatabaseHelper(Context context) {\n            \/\/ TODO Auto-generated constructor stub\n            super(context, DATABASE_NAME, null, DATABASE_VERSION);\n        }\n\n        @Override\n        public void onCreate(SQLiteDatabase db) {\n            \/\/ TODO Auto-generated method stub\n            try {\n                db.execSQL(DATABASE_CREATE_TABLE1);\n                db.execSQL(DATBASE_CREATE_TABLE2);\n            } catch (SQLException e) {\n                e.printStackTrace();\n            }\n        }\n\n        @Override\n        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {\n            \/\/ TODO Auto-generated method stub\n            Log.w(TAG, \"Upgrading database from version \" + oldVersion + \" to \" + newVersion\n                    + \", which will destroy all old data\");\n            db.execSQL(\"DROP TABLE IF EXISTS PLAYERS\");\n            db.execSQL(\"DROP TABLE IF EXISTS FRIENDSLIST\");\n            onCreate(db);\n        }\n\n    }\n\n    public DBAdapter open() throws SQLiteException {\n        db = DBHelper.getWritableDatabase();\n        db.execSQL(\"PRAGMA foreign_keys=ON;\");\n        return this;\n    }\n\n    public void close() {\n        DBHelper.close();\n    }\n\n    \/\/ PLAYERS TABLE CRUD\n\n    public long insertPlayer(String Username, String Password, String Email) {\n        ContentValues initialValues = new ContentValues();\n        initialValues.put(\"USERNAME\", Username);\n        initialValues.put(\"PASSWORD\", Password);\n        initialValues.put(\"EMAIL\", Email);\n\n        db.insert(\"PLAYERS\", null, initialValues);\n\n        return db.insertWithOnConflict(\"PLAYERS\", null, initialValues,\n                SQLiteDatabase.CONFLICT_IGNORE);\n    }\n\n    public boolean deletePlayer(long id) {\n        Log.d(TAG, \"delete is working\");\n        return db.delete(\"PLAYERS\", KEY_ROWID + \" = \" + id, null) &gt; 0;\n\n    }\n\n    public Cursor getAllPlayers() {\n        return db.query(false, \"Players\", new String[] {\n                \"_id\", \"USERNAME\", \"PASSWORD\", \"EMAIL\"\n        }, null, null, null, null, null, null);\n\n    }\n\n    public String getData() {\n        String[] columns = new String[] {\n                \"_id\", \"USERNAME\", \"PASSWORD\"\n        };\n        Cursor mCursor = db.query(\"PLAYERS\", columns, null, null, null, null, null);\n        String result = \"\";\n        int iRow = mCursor.getColumnIndex(KEY_ROWID);\n        int iName = mCursor.getColumnIndex(KEY_USERNAME);\n\n        for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {\n            result = mCursor.getString(iRow) + \" \" + mCursor.getString(iName) + \"\/n\";\n        }\n        return result;\n    }\n\n    public Cursor getPlayer(int _id) throws SQLException {\n        Cursor mCursor = db.query(true, \"PLAYERS\", new String[] {\n                \"_id\", \"USERNAME\"\n        }, KEY_ROWID + \" =  \" + \"_id\", null, null, null, null, null);\n        while (mCursor.moveToNext()) {\n            int id = mCursor.getInt(mCursor.getColumnIndex(\"_id\"));\n        }\n        return mCursor;\n    }\n\n    public String getUsername(String Username) {\n        Cursor mCursor = db.query(\"Players\", new String[] {\n            \"USERNAME\"\n        }, \"USERNAME = ?\", new String[] {\n            Username\n        }, null, null, null);\n        if (mCursor.moveToNext())\n            return mCursor.getString(0);\n        else\n            return \"\";\n    }\n\n    public String getSinlgeEntry(String Username) {\n        Cursor cursor = db.query(\"PLAYERS\", null, \" USERNAME=?\", new String[] {\n            Username\n        }, null, null, null);\n        if (cursor.getCount() &lt; 1) \/\/ UserName Not Exist\n        {\n            cursor.close();\n            return \"NOT EXIST\";\n        }\n        cursor.moveToFirst();\n        String password = cursor.getString(cursor.getColumnIndex(\"PASSWORD\"));\n        cursor.close();\n        return password;\n    }\n\n    public boolean updatePlayer(long _id, String Username, String Password, String Email) {\n        ContentValues args = new ContentValues();\n        args.put(\"USERNAME\", Username);\n        args.put(\"PASSWORD\", Password);\n        args.put(\"EMAIL\", Email);\n        return db.update(\"PLAYERS\", args, KEY_ROWID + \" = \" + _id, null) &gt; 0;\n    }\n\n    \/\/ FRIENDS TABLE CRUD\n\n    public void insertFriend(long playerId, String Friend) {\n        ContentValues initialValues = new ContentValues();\n        initialValues.put(\"player_id\", playerId);\n        initialValues.put(\"FRIENDS\", Friend);\n        db.insertWithOnConflict(\"FRIENDSLIST\", null, initialValues, SQLiteDatabase.CONFLICT_IGNORE);\n    }\n\n    public boolean deleteFriend() {\n        return db.delete(\"FRIENDSLIST\", KEY_ROWID + \" = \" + \"_id\", null) &gt; 0;\n    }\n\n    public Cursor getAllFriends() {\n        return db.query(false, \"FRIENDSLIST\", new String[] {\n                \"_id\", \"FRIENDS\"\n        }, null, null, null, null, null, null);\n    }\n\n    public Cursor getFriend(long player_id) throws SQLException {\n        Cursor mCursor = db.query(true, \"FRIENDSLIST\", new String[] {\n                \"_id\", \"FRIENDS\"\n        }, \"player_id\" + \" =  \" + \"player_id\", null, null, null, null, null);\n        \/\/ cursor from query is never null,\n        \/\/ should just check for if (cursor.moveToFirst()) in the calling\n        \/\/ activity\n        return mCursor;\n    }\n\n    public boolean updateFriend(long _id, String Friends) {\n        ContentValues args = new ContentValues();\n        args.put(\"FRIENDS\", Friends);\n\n        return db.update(\"FRIENDSLIST\", args, KEY_ROWID + \" = \" + _id, null) &gt; 0;\n    }\n\n    public long getPlayerId(String Username) {\n        \/\/ TODO Auto-generated method stub\n        long id = 0;\n        Cursor c = db.query(\"PLAYERS\", new String[] {\n            KEY_ROWID\n        }, KEY_USERNAME + \"=?\", new String[] {\n            Username\n        }, null, null, null);\n        if (c.moveToFirst()) {\n            id = c.getLong(0);\n        }\n        c.close();\n        return id;\n    }\n\n}\n\n\nNew PlayAFriend\n\n\n\n\npackage com.fullfrontalgames.numberfighter;\n\nimport android.app.ListActivity;\nimport android.content.Context;\nimport android.database.Cursor;\nimport android.os.Bundle;\nimport android.view.LayoutInflater;\nimport android.view.View;\nimport android.view.View.OnClickListener;\nimport android.view.ViewGroup;\nimport android.widget.BaseAdapter;\nimport android.widget.Button;\nimport android.widget.ListAdapter;\nimport android.widget.TextView;\n\npublic class PlayAFriend extends ListActivity {\n\n    DBAdapter DBAdapter;\n\n    @Override\n    public void onCreate(Bundle savedInstanceState) {\n        \/\/ TODO Auto-generated method stub\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.playafriend);\n\n        final DBAdapter db = new DBAdapter(this);\n        DBAdapter = db.open();\n\n        long player_id = 0;\n        getListView().setAdapter(new FriendsListAdapter(this, db.getFriend(player_id)));\n\n    }\n\n    private class FriendsListAdapter extends BaseAdapter implements OnClickListener, ListAdapter {\n        private static final long player_id = 0;\n\n        private Cursor fFriends;\n\n        private Context fContext;\n\n        public FriendsListAdapter(Context context, Cursor friends) {\n            \/\/ TODO Auto-generated constructor stub\n            fContext = context;\n            fFriends = friends;\n        }\n\n        @Override\n        public int getCount() {\n            \/\/ TODO Auto-generated method stub\n            return fFriends.getCount();\n        }\n\n        @Override\n        public FriendInfo getItem(int position) {\n            \/\/ TODO Auto-generated method stub\n            if (fFriends.moveToPosition(position)) {\n                String name = fFriends.getString(fFriends.getColumnIndex(\"FRIENDS\"));\n\n                return new FriendInfo(name);\n            }\n            return null;\n        }\n\n        @Override\n        public long getItemId(int position) {\n            \/\/ TODO Auto-generated method stub\n            fFriends.moveToPosition(position);\n            return fFriends.getLong(fFriends.getColumnIndex(\"_id\"));\n        }\n\n        @Override\n        public View getView(int position, View convertView, ViewGroup parent) {\n            \/\/ TODO Auto-generated method stub\n            if (convertView == null) {\n                convertView = LayoutInflater.from(fContext).inflate(R.layout.playafriendlistitems,\n                        parent, false);\n            }\n            FriendInfo friendinfo = getItem(position);\n            TextView friendTV = ViewHolder.get(convertView, R.id.textview_friends);\n            Button playbutton = ViewHolder.get(convertView, R.id.playbutton, position);\n            playbutton.setOnClickListener(this);\n\n            friendTV.setText(friendinfo.getName());\n\n            return convertView;\n        }\n\n        @Override\n        public void onClick(View v) {\n            \/\/ TODO Auto-generated method stub\n            DBAdapter db = new DBAdapter(fContext);\n            db.open();\n            int position = (Integer)v.getTag();\n            fFriends.moveToPosition(position);\n            long id = fFriends.getLong(fFriends.getColumnIndex(\"_id\"));\n            fFriends = db.getFriend(id);\n            db.close();\n        }\n\n    }\n\n}\n<\/code><\/pre>\n<p>FindFriends Activity<\/p>\n<pre><code>package com.fullfrontalgames.numberfighter;\n\nimport android.app.Activity;\nimport android.os.Bundle;\nimport android.view.View;\nimport android.widget.Button;\nimport android.widget.EditText;\nimport android.widget.FrameLayout;\nimport android.widget.TextView;\nimport android.widget.Toast;\n\npublic class Findfriends extends Activity {\n    DBAdapter db;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        \/\/ TODO Auto-generated method stub\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.findfriends);\n\n        final EditText sbar = (EditText)findViewById(R.id.PlayerSeachBar);\n        Button search = (Button)findViewById(R.id.Search);\n        Button Add = (Button)findViewById(R.id.Add);\n        final TextView ResultText = (TextView)findViewById(R.id.ResultTextView);\n        final FrameLayout ResultFrame = (FrameLayout)findViewById(R.id.ResultFrameLayout);\n\n        ResultFrame.setVisibility(View.GONE);\n\n        final DBAdapter db = new DBAdapter(this);\n        db.open();\n\n        search.setOnClickListener(new View.OnClickListener() {\n\n            @Override\n            public void onClick(View v) {\n                \/\/ TODO Auto-generated method stub\n                String Username = sbar.getText().toString();\n                String foundplayer = db.getUsername(Username);\n\n                if (Username.equals(foundplayer)) {\n\n                    ResultFrame.setVisibility(View.VISIBLE);\n                    ResultText.setText(foundplayer);\n                    Toast.makeText(getApplicationContext(), \"Player Found\", Toast.LENGTH_LONG)\n                            .show();\n\n                } else {\n                    Toast.makeText(getApplicationContext(), \"Username Not Found\", Toast.LENGTH_LONG)\n                            .show();\n                }\n            }\n\n        });\n\n        Add.setOnClickListener(new View.OnClickListener() {\n\n            @Override\n            public void onClick(View v) {\n                \/\/ TODO Auto-generated method stub\n                String Friends = sbar.getText().toString();\n                long playerId = db.getPlayerId(Friends);\n                db.insertFriend(playerId, Friends);\n                Toast.makeText(getApplicationContext(), \"Player Has Been Added\", Toast.LENGTH_LONG)\n                        .show();\n\n            }\n        });\n    }\n}\n<\/code><\/pre>\n<p>LogCat<\/p>\n<pre><code> 04-22 21:11:37.834: E\/SQLiteDatabase(15862): Error inserting player_id=0 FRIENDS=null\n    04-22 21:11:37.834: E\/SQLiteDatabase(15862): android.database.sqlite.SQLiteConstraintException: foreign key constraint failed (code 19)\n    04-22 21:11:37.834: E\/SQLiteDatabase(15862):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)\n    04-22 21:11:37.834: E\/SQLiteDatabase(15862):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)\n<\/code><\/pre>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/85667f83c1e8aa5761ee8ce12db505fb?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nHoan Nguyen<\/p>\n<p>Change your table <code>FRIENDSLIST<\/code><\/p>\n<pre><code>static final String DATBASE_CREATE_TABLE2 = \n    \"create table FRIENDSLIST (_id integer primary key autoincrement,\"\n    + \"player_id integer not null references PLAYERS(\"_id\") on update cascade on delete cascade,\"\n        + \"FRIENDS text not null,USERNAME text ,NUMBERINPUT text,SCORE text);\";  \n<\/code><\/pre>\n<p>And when you insert<\/p>\n<pre><code>public void insertFriend(long playerId, String Friend) {\n    ContentValues initialValues = new ContentValues();\n    initialValues.put(\"player_id\", playerId);\n    initialValues.put(\"FRIENDS\", Friend);\n    db.insertWithOnConflict(\"FRIENDSLIST\", null, initialValues, SQLiteDatabase.CONFLICT_IGNORE);\n}  \n<\/code><\/pre>\n<p>Get friends<\/p>\n<pre><code>public Cursor getFriend(long rowid) throws SQLException {\n    Cursor mCursor = db.query(true, \"FRIENDSLIST\", new String[] {\n            \"_id\", \"FRIENDS\"\n    }, player_id + \" =  \" + \"rowid\", null, null, null, null, null);\n    \/\/ cursor from query is never null, \n    \/\/ should just check for if (cursor.moveToFirst()) in the calling activity\n    return mCursor;\n}  \n<\/code><\/pre>\n<p>In the database open method add<\/p>\n<pre><code>public DBAdapter open() throws SQLiteException {\n    db = DBHelper.getWritableDatabase();\n    db.execSQL(\"PRAGMA foreign_keys=ON;\"); \n    return this;\n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Bananeweizen In my application&#8217;s database I have 2 tables one called &#8220;PLAYERS&#8221; which holds the data of every user and &#8220;FRIENDSLIST&#8221; which is intended to be a personal &#8220;friends&#8221; list for every registered account.How the process goes in my app is that when a player (Joe) registers a Username his information goes into the &#8220;PLAYERS&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6228","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6228"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6228\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}