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 7853693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:46:17+00:00 2026-06-02T19:46:17+00:00

I’ve created a ListView from an SQLite database but am stuck on how to

  • 0

I’ve created a ListView from an SQLite database but am stuck on how to add a listener to each ListView item so that when an item is clicked I can display another page with more information on that item. The database is just a sample. Any help would be appreciated.

public class Database extends ListActivity {

private final String SAMPLE_DB_NAME = "myFriendsDb";
//private final String SAMPLE_TABLE_NAME = "friends";

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


    ArrayList<String> results = new ArrayList<String>();
    SQLiteDatabase db = null;

    try {
        db =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

        db.execSQL("CREATE TABLE IF NOT EXISTS people" +
                " (LastName VARCHAR, FirstName VARCHAR," +
                " Country VARCHAR, Age INT(3));");

        db.execSQL("INSERT INTO people" +
                " Values ('Jones','Bob','UK',30);");
        db.execSQL("INSERT INTO people" +
                " Values ('Smith','John','UK',40);");
        db.execSQL("INSERT INTO people" +
                " Values ('Thompson','James','UK',50);");

        Cursor c = db.rawQuery("SELECT FirstName, LastName FROM people", null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String firstName = c.getString(c.getColumnIndex("FirstName"));
                    String lastName = c.getString(c.getColumnIndex("LastName"));
                    results.add("" + firstName + " " + lastName);
                }while (c.moveToNext());
            } 
        }

        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    } finally {
        if (db != null) 
            db.execSQL("DELETE FROM people");
            db.close();
    }
}

}

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

    there are many ways to solve your problem. One possible solution is this: you simply need to implement protected method onListItemClick(ListView l, View v, int position, long id) in your ListActivity.

    public class Database extends ListActivity {
    
        //YOUR CODE ABOVE HERE...
    
        public static final String SHOWITEMINTENT_EXTRA_FETCHROWID = "fetchRow";
        public static final int ACTIVITY_SHOWITEM = 0; /*Intent request user index*/
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id){
            /*
            position variable holds the position of item you clicked...
            do your stuff here. If you want to send to another page, say another activity
            that shows your stuff, you can always use an intent
            example:
            */
            Intent tmpIntent = new Intent(this, YourActivityForShowingItem.class);
            tmpIntent.putExtra(SHOWITEMINTENT_EXTRA_FETCHROWID, position);
            startActivityForResult(tmpIntent, ACTIVITY_SHOWITEM);
    
        }
    }
    

    Alternately, you can access the ListView of your listActivity using getListView(), and call the setters for listeners or context menu as you would have done with a regular ListView object. For instance, this function that sets a listener using this approach:

    private void setMyListListener(){
        getListView().setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id){
                /*same fake code as above for calling another activity using an intent:*/
                Intent tmpIntent = new Intent(this, YourActivityForShowingItem.class);
                tmpIntent.putExtra(SHOWITEMINTENT_EXTRA_FETCHROWID, position);
                startActivityForResult(tmpIntent, ACTIVITY_SHOWITEM);
            }
        });
    }
    

    This function can be called by your onCreate(…) function afterwards if you want your click listener to be configured the same way for the whole duration of your activity.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.