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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:58:19+00:00 2026-06-16T09:58:19+00:00

I need some type of explanation as to how I can pass any database

  • 0

I need some type of explanation as to how I can pass any database column value I want from the database for a listview item.

I would like to be able to grab any value, and pass it through to another activity via putExtras. I know how to do that part, what I don’t have a strong grasp of is how to get the value I want from the listview before sending it through the intent.

Here is my code and some more details to help you help me:

Day.java:

package com.dd.gfit;

public class Day {

    private long id;
    private long id_routine;
    private String name;
    private String day;

    public long getId() { return id; }  
    public long getIdRoutine() { return id_routine; }   
    public String getName() { return name; }    
    public String getDay() { return day; }

    public void setId(long id) { this.id = id; }    
    public void setIdRoutine(long id_routine) { this.id_routine = id_routine; } 
    public void setName(String name) { this.name = name; }  
    public void setDay(String day) { this.day = day; }

} 

This is where the values are stored and retreived. I get that.

DaysDataSource.java:

package com.dd.gfit;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DaysDataSource {

    private SQLiteDatabase database;
    private MySQLiteHelper dbHelper;
    private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_ID_ROUTINE, MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };

    public DaysDataSource(Context context) {
        dbHelper = new MySQLiteHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public Cursor fetchAllDays(long id) {     
        Cursor cursor = database.query(MySQLiteHelper.TABLE_DAYS, allColumns, MySQLiteHelper.COLUMN_ID_ROUTINE + " = " + id, null, null, null, null);
        if (cursor != null) { cursor.moveToFirst(); }
        return cursor;
    }

} 

This is the data source where I can retrieve all the days and their column values into a cursor to be used in a simplecursoradapter to be applied to a listview. I get this too.

DaysActivity.java:

package com.dd.gfit;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.widget.SimpleCursorAdapter;

public class DaysActivity extends ListActivity {

    private DaysDataSource datasource;
    private SimpleCursorAdapter dataAdapter;

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

        datasource = new DaysDataSource(this);
        datasource.open();

        Cursor cursor = datasource.fetchAllDays(routineDataID);
        String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };
        int[] to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
        dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
        setListAdapter(dataAdapter);
    }

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

    @Override
    protected void onListItemClick(ListView l, View v, int position, long thisID)
    {
        Day day = (Day)getListAdapter().getItem(position);
        long id = day.getId();

        Intent startDayActivity = new Intent(this, DayActivity.class);
        startDayActivity.putExtra("routineDataID", id);
        this.startActivity(startDayActivity);
    }

} 

This is the part I don’t get. First of all the listview shows up perfect with what I want in it. The onListItemClick() function is causing the app to crash when it is fired (when I click a list item). id seems to be coming up null or something? It’s like it was never assigned a value for the list item.

I need to know what I am missing here. I need someone to help me and tell me what I need to add to my code and where to add it so I can better understand how to do this properly in the future.

I’m assuming I need to have the values for the day applied BEFORE trying to use the getId() function? Any help would be great.

EDIT:

I have a button inside the listview, I also want to be able to grab the id when this button is clicked.

Heres the code using an array adapter that I would like to convert to a simplecursoradapter:

public void onClick(View view) {    
    @SuppressWarnings("unchecked")
    ArrayAdapter<Day> adapter = (ArrayAdapter<Day>)getListAdapter();
    ListView lv = getListView();
    int position = lv.getPositionForView(view);
    Day day = (Day) getListAdapter().getItem(position);
    long id = day.getId();
}

EDIT AGAIN:

Figured it out. Heres the code:

    ListView l = getListView();
    int position = l.getPositionForView(view);

    Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
    cursor.moveToPosition(position);
    long id = cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID));
    String name = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME));
  • 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-16T09:58:20+00:00Added an answer on June 16, 2026 at 9:58 am

    Change your onListItemClick code as for getting selected value from Cursor:

    @Override
        protected void onListItemClick(ListView l, View v, int position, long thisID)
        {
          Cursor cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
          cursor.moveToPosition(position);
          long id = cursor.getLong(cursor.getColumnIndex("YOUR_COLUMN_NAME_HERE"));
    
          Intent startDayActivity = new Intent(DaysActivity.this, DayActivity.class);
          startDayActivity.putExtra("routineDataID", id);
          startActivity(startDayActivity);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've some scenarios where i need to pass value type as reference without changed
I need to know how many objects of some type exist in my system
I've created a factory assembly that can process any type of transaction (that supports
I would like to know how to request images using some type of callback
[Check My EDIT for better Explanation] I need some help with a very big
I want to build a service that will pass the data read from the
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via
Need some quick advice I am trying to access a object array but I
Need some help with Activerecord Querying in a has_many :through association. Model: Job class

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.