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));
Change your
onListItemClickcode as for getting selected value from Cursor: