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

  • Home
  • SEARCH
  • 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 8673385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:26:45+00:00 2026-06-12T19:26:45+00:00

Im working on an android project, and I am trying to get the information

  • 0

Im working on an android project, and I am trying to get the information about a database item from whatever item is selected from a listView which contains entries of the database items. I am new to android and SQLite so Im not sure about several things. For the listview, what can I do to make the items click-able and then use that to get information about the item from the database.

In an unrelated matter, how could I check the list of names from the database in order to prevent the user from entering duplicates?

Here is the code for the ContactsMenu class which contains the listview.

package com.emailandcontactmanager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ContactsMenu extends Activity {


    ListView listView;
    public static final String fields[] = { DatabaseSetup.colName};
    Cursor cursor;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.managecontacts);
        listView = (ListView) findViewById(R.id.lstContacts); 
        DatabaseSetup.init(this); 

        Button btnAddItem = (Button) findViewById(R.id.btnAddContact);
        btnAddItem.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent addItem = new Intent(v.getContext(), AddContact.class);
            startActivity(addItem);
        }
        });

    }
        @Override
        protected void onPause() {
            listView.setAdapter(null);
            cursor.close();
            DatabaseSetup.deactivate();
            super.onPause();
        }

        @Override
        protected void onResume() {
            super.onResume();
            DatabaseSetup.init(this);
            cursor = DatabaseSetup.getContactNames();
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, fields, new int[] {R.id.item_text});
            listView.setAdapter(adapter); 
        }





////////////////MENU////////////////////////////////////////////////////////////////////////////////
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
            switch (which){ 
            case DialogInterface.BUTTON_POSITIVE: 
                finish();
                break; 

            case DialogInterface.BUTTON_NEGATIVE: 
                //Nothing happens on No button click, and the menu closes
                break; 
            } 
        } 
    }; 

    @Override
    public boolean onCreateOptionsMenu(Menu mainmenu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, mainmenu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        final CharSequence[] items = {"Contacts list", "Add Contact"};

        switch (item.getItemId()) {
            case R.id.help:     AlertDialog.Builder builder = new AlertDialog.Builder(this);
                                builder.setTitle("Select a function to revice information about it.");
                                builder.setItems(items, new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int selected) {
                                        switch(selected){
                                        case 0:
                                            Toast.makeText(getApplicationContext(),
                                                    "Allows you to view the selected item and make editations to it or delete it.",
                                                    Toast.LENGTH_LONG).show();
                                            break;
                                        case 1:
                                            Toast.makeText(getApplicationContext(),
                                                    "Allows you to add a new contact by bringing up a screen where the nececary information can be entered.",
                                                    Toast.LENGTH_LONG).show();
                                        }
                                    }
                                });
                                builder.show();
                                break;

                                case R.id.back:     AlertDialog.Builder builderBack = new AlertDialog.Builder(this); 
                                                    builderBack.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) 
                                                    .setNegativeButton("No", dialogClickListener).show(); 
                                break;
        }
        return true;

    }
}

Here is the code for the DatabaseSetup class.

package com.emailandcontactmanager;

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

/* 
 * usage:  
 * DatabaseSetup.init(egActivityOrContext); 
 * DatabaseSetup.createEntry() or DatabaseSetup.getContactNames() or DatabaseSetup.getDb() 
 * DatabaseSetup.deactivate() then job done 
 */ 

class DatabaseSetup extends SQLiteOpenHelper { 
static DatabaseSetup instance = null; 
static SQLiteDatabase db = null; 

public static void init(Context context) { 
    if (null == instance) { 
        instance = new DatabaseSetup(context); 
        } 
    } 

public static SQLiteDatabase getDb() { 
    if (null == db) { 
        db = instance.getWritableDatabase(); 
        } 
    return db; 
    } 

public static void deactivate() { 
    if (null != db && db.isOpen()) { 
        db.close(); 
        } 
    db = null; 
    instance = null; 
    } 

public static long createEntry(String name, String mail, String phone1, 
        String phone2, String address, String notes) { 
    ContentValues cv = new ContentValues(); 
    cv.put(colName, name); 
    cv.put(colMail, mail); 
    cv.put(colPhone1, phone1); 
    cv.put(colPhone2, phone2); 
    cv.put(colAddress, address); 
    cv.put(colNotes, notes); 
    return getDb().insert(contactsTable, null, cv); 

    } 

public static Cursor getContactNames() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[] {"_id", colName }; 
    return getDb().query(contactsTable, columns, null, null, null, null, 
            null); 
    } 

DatabaseSetup(Context context) { 
    super(context, dbName, null, dbVersion); 
    } 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // TODO Auto-generated method stub 
    db.execSQL("CREATE TABLE IF NOT EXISTS " + contactsTable 
            + " (_id integer primary key autoincrement, " + colName 
            + " TEXT NOT NULL, " + colMail + " TEXT NOT NULL, " + colPhone1 
            + " TEXT NOT NULL, " + colPhone2 + " TEXT NOT NULL, " 
            + colAddress + " TEXT NOT NULL, " + colNotes 
            + " TEXT NOT NULL)"); 

    db.execSQL("CREATE TABLE IF NOT EXISTS " + templatesTable 
            + " (_id integer primary key autoincrement, " + colSubject 
            + " TEXT NOT NULL, " + colBody + " TEXT NOT NULL)"); 

    db.execSQL("CREATE TABLE IF NOT EXISTS " + tagsTable 
            + " (_id integer primary key autoincrement, " + colTagName 
            + " TEXT NOT NULL, " + colContact + " TEXT NOT NULL)"); 

    } 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + contactsTable); 
    db.execSQL("DROP TABLE IF EXISTS " + templatesTable); 
    db.execSQL("DROP TABLE IF EXISTS " + tagsTable); 
    onCreate(db); 
    } 

static final String dbName = "DB"; 
static final int dbVersion = 1; 
static final String contactsTable = "Contacts"; 
static final String colName = "Name"; 
static final String colMail = "Email"; 
static final String colPhone1 = "Phone1"; 
static final String colPhone2 = "Phone2"; 
static final String colAddress = "Address"; 
static final String colNotes = "Notes"; 

static final String templatesTable = "Templates"; 
static final String colSubject = "Subject"; 
static final String colBody = "Body"; 

static final String tagsTable = "Tags"; 
static final String colTagName = "Name"; 
static final String colContact = "Contact"; 

} 

And for that last question here is my AddContact class

package com.emailandcontactmanager;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AddContact extends Activity
{
    EditText contactName, contactMail, contactPhone1, contactPhone2, contactAddress, contactNotes; 

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newcontact);


        contactName = (EditText) findViewById(R.id.txtName);
        contactMail = (EditText) findViewById(R.id.txtMail);
        contactPhone1 = (EditText) findViewById(R.id.txtPhone1);
        contactPhone2 = (EditText) findViewById(R.id.txtPhone2);
        contactAddress = (EditText) findViewById(R.id.txtAddress);
        contactNotes = (EditText) findViewById(R.id.txtNotes);
       // saveContact = (Button) findViewById(R.id.btnSaveContact);

        DatabaseSetup.init(this);

        Button btnSaveContact = (Button) findViewById(R.id.btnSaveContact);
        btnSaveContact.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                boolean working = true;

                try{
                    String name = contactName.getText().toString();
                    String mail = contactMail.getText().toString();
                    String phone1 = contactPhone1.getText().toString();
                    String phone2 = contactPhone2.getText().toString();
                    String address = contactAddress.getText().toString();
                    String notes = contactNotes.getText().toString();

                    if(name != null & mail != null) {
                    DatabaseSetup entry = new DatabaseSetup(AddContact.this);
                    DatabaseSetup.getDb();
                    DatabaseSetup.createEntry(name, mail, phone1, phone2, address, notes);
                    entry.close();
                    }
                    else{
                        Toast.makeText(getApplicationContext(),
                                "Must enter name and email for contact",
                                Toast.LENGTH_LONG).show();

                    }

                    }
                    catch(Exception e)
                    {
                        working = false;
                        String error = e.toString();
                        Dialog d = new Dialog(AddContact.this);
                        d.setTitle("Error");
                        TextView tv = new TextView(AddContact.this);
                        tv.setText(error);
                    }
                    finally
                    {
                        if(working)
                        {
                            Dialog d = new Dialog(AddContact.this);
                            d.setTitle("Success");
                            TextView tv = new TextView(AddContact.this);
                            tv.setText("The database changes have succeeded.");
                            d.setContentView(tv);
                            d.show();

                        }
                    }       
            }   
        });
    }
////////////////MENU///////////////////////////////////////////////MENU/////////////////////////////
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int which) { 
switch (which){ 
case DialogInterface.BUTTON_POSITIVE: 
    finish();
    break; 

case DialogInterface.BUTTON_NEGATIVE: 
    //Nothing happens on No button click, and the menu closes
    break; 
            } 
        } 
    }; 

@Override
public boolean onCreateOptionsMenu(Menu mainmenu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, mainmenu);
return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
final CharSequence[] items = {"Name Field", "Email Field", "Phone1&2 Fields", "Address Field", "Notes Field", "Add Button"};

switch (item.getItemId()) {
case R.id.help:     AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("Select a function to revice information about it.");
                    builder.setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int selected) {
                            switch(selected){
                            case 0:
                                Toast.makeText(getApplicationContext(),
                                        "Enter the desired name of a contact. This is a required field",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case 1:
                                Toast.makeText(getApplicationContext(),
                                        "Enter the email address of the contact. This is a required field.",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case 2:
                                Toast.makeText(getApplicationContext(),
                                        "Enter the phone number(s) of the contact. Both fields are optional",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case 3:
                                Toast.makeText(getApplicationContext(),
                                        "Enter the address of the contact. This field is optional",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case 4:
                                Toast.makeText(getApplicationContext(),
                                        "Enter notes about the contact. This field is optional",
                                        Toast.LENGTH_LONG).show();
                                break;
                            case 5:
                                Toast.makeText(getApplicationContext(),
                                        "This button will save the contact to the database. When you are done saving contacts, click back to return to the contacts menu.",
                                        Toast.LENGTH_LONG).show();
                            }
                        }
                    });
                    builder.show();
                    break;

                    case R.id.back:     AlertDialog.Builder builderBack = new AlertDialog.Builder(this); 
                                        builderBack.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener) 
                                        .setNegativeButton("No", dialogClickListener).show(); 
                    break;
    }
return true;

        }
    }

Thank you for any help

  • 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-12T19:26:46+00:00Added an answer on June 12, 2026 at 7:26 pm

    Use onItemClick or onItemLongClick(for using long press) and use the position to point to data in the Database

    http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html Here’s a detailed example.Hope It Helps.

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

Sidebar

Related Questions

I am working on an android project and have a spinner which contains items
When I try to run my Android project(which was working fine yesterday) I get
i am working on a android project for my assignment. i am trying to
I am currently working on an android project and I am trying to create
I am working on an android project where I am using a ListView and
I am working on an android project and I am using a spinner which
I am working on an Android project which relies on the unique UID of
I am trying to get Mockito (1.9.5 rc1) working in my Android tests (using
I am currently working on an android project. I am trying to have an
I am working on an android project that requires a database. The issue that

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.