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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:56:01+00:00 2026-06-14T03:56:01+00:00

I am reading from a database into a listview using a custom cursor adapter.

  • 0

I am reading from a database into a listview using a custom cursor adapter. The listview shows some details of each entry in the database. When I click on a list item, I want to open a details page that shows the other details of that record that are not displayed in the listview. I can’t figure it out.

I have attached the code snippets of the cursor creation and the adapter creation which are in MainActivity. I also have a separate customAdapter class and a class for the details page.

MainActivity:

public class MainActivity extends ListActivity {
    ....
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ...
        ...  
    }

    private void fillList() {
        ListView lv = getListView();

        Cursor c = db.getData();
        startManagingCursor(c);

        CustomCursorAdapter adapter = new CustomCursorAdapter(getApplicationContext(), c);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                Intent k = new Intent(MainActivity.this, ProductDetails.class);
            }
        });
    }

}

CustomCursorAdapter:

public class CustomCursorAdapter extends CursorAdapter {

        public CustomCursorAdapter(Context context, Cursor cursor) {
            super(context, cursor);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View v = inflater.inflate(R.layout.list_entry, parent, false);
            bindView(v, context, cursor);
            return v;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView txtvw1 = (TextView)view.findViewById(R.id.prodcode_entry);
            txtvw1.setText(cursor.getString(cursor.getColumnIndex(ProdDB.KEY_PRODCODE)));

            TextView txtvw2 = (TextView)view.findViewById(R.id.prodtype_entry);
            String ptype = (cursor.getString(cursor.getColumnIndex(ProdDB.KEY_PRODTYPE)));
            txtvw2.setText(ptype.substring(0, 1));

            TextView txtvw3 = (TextView)view.findViewById(R.id.prodcat_entry);
            String cat = (cursor.getString(cursor.getColumnIndex(ProdDB.PRODCAT)));
            txtvw3.setText(cat.substring(0, 2));

            TextView txtvw4 = (TextView)view.findViewById(R.id.prodcost_entry);
            float price = (cursor.getFloat(cursor.getColumnIndex(ProdDB.PRODPRICE)));
            txtvw4.setText(Float.toString(price));

            TextView txtvw5 = (TextView)view.findViewById(R.id.prodSup_entry);
            txtvw5.setText(cursor.getString(cursor.getColumnIndex(ProdDB.SUPPLIER)));
        }
}

ProdDetails

public class ProductDetails extends Activity {

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

        setContentView(R.layout.product_details);
    }
}

db.getData() – from ProdDB

public Cursor getData() {
        String[] columns = new String[] {KEY_ROWID, 
                KEY_PRODCODE,
                KEY_PRODNAME,
                KEY_PRODTYPE,
                KEY_PRODCAT,
                KEY_PRODPRICE,
                KEY_PRODRRP,
                KEY_PRODSUPPLIER,
                KEY_NOTES};

        return db.query(DB_TABLE, columns, null, null, null, null, null);
    }
  • 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-14T03:56:02+00:00Added an answer on June 14, 2026 at 3:56 am

    After this line:

    Intent k = new Intent(MainActivity.this, ProductDetails.class);
    

    pass also the id of the item:

    k.putExtra("item_id", id);
    // and start the activity
    startActivity(k);
    

    Then, from within the ProductDeftails activity, get the id again, query the database and retrieve its information:

    // after setContentView...
    long id = getIntent().getLongExtra("item_id", 0);
    // use this variable to get the item again and populate your views
    

    getLongExtra receives the key of the extra to retrieve plus a default value. It is always worthy to check the returned value against the default value to make sure the activity actually received something.

    So, just remember this next time… creating an intent does not launch the activity it represents (you have to use either startActivity or startActivityForResult methods). Also, you can pass data to an activity that is going to be launched using the Intent’s extras. It can be primitive types, Strings, String’s ArrayLists, or Parcelables (single objects, array lists or arrays).

    Details

    To get the details of your product you can use something like this:

    String[] columns = new String[] {KEY_ROWID, 
            KEY_PRODCODE,
            KEY_PRODNAME,
            KEY_PRODTYPE,
            KEY_PRODCAT,
            KEY_PRODPRICE,
            KEY_PRODRRP,
            KEY_PRODSUPPLIER,
            KEY_NOTES};
    return db.query(DB_TABLE, columns, KEY_ROWID + " = ?", new String[]{String.valueOf(id)}, null, null, null);
    

    You can put that inside a new method called getDetails(long id) (inside ProdDB class). Then, from within your activity you can do:

    Cursor cursor = db.getDetails(id);
    if(cursor.moveToFirst()){
        // I assume you had already created the views in the ProductDetails activity
        txtvw1.setText(cursor.getString(cursor.getColumnIndex(ProdDB.KEY_PRODCODE)))
    } else {
        // something went wrong, do something
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading data from a table in an SQLite3 database into a ListView
I am reading records from file and inserting into database. I am using DB2
I have some problem with reading from sqlite3 database. -(void) readMessengesFromDatabase { sqlite3 *database;
Continuing to the post of reading-from-XML-and-storing-the-values-in-database-using-grails am facing an another problem in it. As
So I'm trying to get the values from a SQLite database into a cursor,
I need to store simultaneous readings from 10 sensors into a SQLite database at
I am reading emails from database. And i want to echo the emails as
I'm trying to handle DBNull exception while reading data from database. It's my code:
For reading the data from the database which is faster BCP or Data reader?
My problem involves checking if I have a valid database connection before reading from

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.