Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:16:04+00:00 2026-06-08T14:16:04+00:00

I’ve created ListView, database and a table for it. I’ve like 5 names in

  • 0

I’ve created ListView, database and a table for it. I’ve like 5 names in the list. I need to capture more than one name into table of the sqlite database but I can only capture the first name from the list. Any help would be appreciated. Thankss! =)

Below are the codes.

package main.page;

import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ContactsProvider extends ListActivity
{
    BuddiesDBAdapter buddiesDB = new BuddiesDBAdapter(this);

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

        Uri allContacts = Uri.parse("content://contacts/people");

        Cursor c = managedQuery(allContacts, null, null, null, null);

        String[] columns = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.Contacts._ID};
        int[] views = new int[]{R.id.contactName, R.id.contactID};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contacts_list, c, columns, views);
        this.setListAdapter(adapter);
    }

    public void onListItemClick(ListView parent, View v, int position, long id)
    {
        buddiesDB.open();
        long _id;
        TextView contactName = (TextView) findViewById(R.id.contactName);
        String NameValue = contactName.getText().toString();

        _id = buddiesDB.insertContact(NameValue);
        buddiesDB.close();
    }

}


package main.page;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BuddiesDBAdapter
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "buddy_name";
    private static final String TAG = "BuddiesDBAdapter";

    private static final String DATABASE_NAME = "anniversary";
    private static final String DATABASE_TABLE = "buddiesList";
    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_CREATE = "create table buddiesList(_id integer primary key autoincrement, "
            + "buddy_name text not null);";

    private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public BuddiesDBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try
            {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }// end onCreate()

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

    }//end DatabaseHelper class

    public BuddiesDBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }// end open()

    public void close()
    {
        DBHelper.close();
    }// end close()

    public long insertContact(String buddy_name)
    {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, buddy_name);
        return db.insert(DATABASE_TABLE, null, values);         
    }//end insertContact()


    public boolean deleteContact(long rowId)
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }// end deleteContact()


    public Cursor getAllContacts()
    {
        return db.query(DATABASE_TABLE, new String[] 
                { KEY_ROWID, KEY_NAME }, null, null, null, null, null);
    }// end getAllContacts()

    public Cursor getContact(long rowId) throws SQLException
    {
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_NAME }, KEY_ROWID + "=" + rowId,
                null, null, null, null, null);
        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }// end getContact()

    public boolean updateContact(long rowId, String buddy_name)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, buddy_name);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }//end updateContact()
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:16:06+00:00Added an answer on June 8, 2026 at 2:16 pm

    The problem seems to be here:

    TextView contactName = (TextView) findViewById(R.id.contactName);
    String NameValue = contactName.getText().toString();

    You are using the same TextView for all the views in the list when you are using the SimpleCursorAdapter. So when you say findViewById(R.id.contactName) you are not specifying which among the many views you want.

    Solution: you can try using the position parameter you are getting in onListItemClick to identify exactly which item in the cursor was clicked, and store that item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.