To add the content in to the database - android

how to solved that error how to get the array list Result sets of queries are returned in Cursor objects. There are some common methods that you will use with cursors
public class DBAdapter {
Context c;
CluecatalogOpenHelper helper;
SQLiteDatabase db;
int flag=0;
public DBAdapter(Context ctx) {
super();
this.c = ctx;
helper=new CluecatalogOpenHelper(ctx);
}
public void openDB(){
db=helper.getWritableDatabase();
}
public void closeDB(){
db.close();
}
public void addRow(String name, byte[] data1) {
openDB();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("object",data1);
db.insert("topsearch", null, values);
closeDB();
}
public void deleteRow(String name)
{
try {
db.execSQL("delete from topsearch where name = (?)",new String[]{""+name});
} catch (SQLException e) {
e.printStackTrace();
}
}
public Cursor showData(){
try{
Cursor c = db.query("topsearch", new String[]{"name","object"}, null, null,null, null,null);
return c;
}catch(Exception e) {
e.printStackTrace();
return null;
}}}
how to solved that error

I'm not sure exactly what you're asking, but if you're asking how do you pull out the results out of a Cursor object and into a List you would do it like this:
List list = new ArrayList();
Cursor c = db.query("topsearch", new String[]{"name","object"}, null, null,null, null,null);
while(c.moveToNext())
{
//populate your return object from the cursor here
//i'm just making this part up
Obj obj = new Obj();
obj.setName(c.getString(0)); //0 being the column index
list.add(obj);
}
return list;

Related

android.content.Context.getContentResolver()' on a null object reference

I can't seem work out why I am getting a null pointer on this?
This is my AsyncTask that I call to grab the data. It passes it to a JSON Parser and an array of Objects is returned. This is then passed to my DBHelper where it was passing to my database through a ContentResolver....
public class getFilms extends AsyncTask<String, Void, Void> {
public int LIMIT_FILMS = 10;
String KEY = "apikey";
String LIMIT = "limit";
private static final String URL = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?";
private static final String API_KEY = "******************";
ArrayList<HashMap<String, String>> filmArrayList = new ArrayList<HashMap<String, String>>();
Context mContext;
#Override
protected Void doInBackground(String... params) {
Uri RottenUrl = Uri.parse(URL).buildUpon()
.appendQueryParameter(KEY, API_KEY)
.appendQueryParameter(LIMIT, Integer.toString(LIMIT_FILMS))
.build();
JSONParser jParser = new JSONParser();
Film[] json = jParser.getJSONFromUrl(RottenUrl.toString());
sortData(json);
return null;
}
public void sortData(Film[] jsonlist) {
DatabaseHelper dbHelper = new DatabaseHelper(mContext, null, null, 1);
dbHelper.deleteAll();
for (int i = 0; i < jsonlist.length; i++) {
dbHelper.contentAddFilm(jsonlist[i]);
}
}
}
This is my Database Helper
public class DatabaseHelper extends SQLiteOpenHelper {
private ContentResolver myCR;
public DatabaseHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, FilmDataContract.DATABASE_NAME, factory, FilmDataContract.DATABASE_VERSION);
myCR = context.getContentResolver();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(FilmDataContract.FilmEntry.SQL_CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(FilmDataContract.FilmEntry.DELETE_TABLE);
onCreate(db);
}
public void addFilm(Film film) {
ContentValues values = new ContentValues();
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(FilmDataContract.TABLE_NAME,
null,
values);
db.close();
}
public Film getFilm(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor =
db.query(FilmDataContract.TABLE_NAME,
FilmDataContract.FilmEntry.COLUMNS,
"_id = ?",
new String[]{String.valueOf(id)},
null,
null,
null,
null);
if (cursor != null)
cursor.moveToFirst();
Film film = new Film();
film.setTitle(cursor.getString(1));
film.setRating(cursor.getString(2));
film.setRuntime(cursor.getString(3));
film.setCritics(cursor.getString(4));
film.setAudience(cursor.getString(5));
film.setSynopsis(cursor.getString(6));
film.setProfile(cursor.getString(7));
return film;
}
public List<Film> getAllFilms() {
List<Film> films = new LinkedList<Film>();
String query = "SELECT * FROM " + FilmDataContract.TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Film film = null;
if (cursor.moveToFirst()) {
do {
film = new Film();
film.setId(Integer.parseInt(cursor.getString(0)));
film.setTitle(cursor.getString(1));
film.setRating(cursor.getString(2));
film.setRuntime(cursor.getString(3));
film.setCritics(cursor.getString(4));
film.setAudience(cursor.getString(5));
film.setSynopsis(cursor.getString(6));
film.setProfile(cursor.getString(7));
films.add(film);
} while (cursor.moveToNext());
}
return films;
}
public int updateFilm(Film film) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());
int i = db.update(FilmDataContract.FilmEntry.TABLE_NAME,
values,
"_id+ = ?",
new String[]{String.valueOf(film.getId())});
db.close();
return i;
}
public int getFilmsCount() {
String countQuery = "SELECT * FROM " + FilmDataContract.FilmEntry.TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
cursor.close();
return cnt;
}
public void deleteAll() {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(FilmDataContract.FilmEntry.TABLE_NAME, null, null);
}
public boolean contentDelete(String filmName) {
boolean result = false;
String selection = "title = \"" + filmName + "\"";
int rowsDeleted = myCR.delete(FilmProvider.CONTENT_URI,
selection, null);
if (rowsDeleted > 0)
result = true;
return result;
}
public Film contentFindFilm(String filmName) {
String[] projection = FilmDataContract.FilmEntry.COLUMNS;
String selection = "title = \"" + filmName + "\"";
Cursor cursor = myCR.query(FilmProvider.CONTENT_URI,
projection, selection, null,
null);
Film film = new Film();
if (cursor.moveToFirst()) {
cursor.moveToFirst();
film.setId(Integer.parseInt(cursor.getString(0)));
film.setTitle(cursor.getString(1));
film.setRating(cursor.getString(2));
film.setRuntime(cursor.getString(3));
film.setCritics(cursor.getString(4));
film.setAudience(cursor.getString(5));
film.setSynopsis(cursor.getString(6));
film.setProfile(cursor.getString(7));
cursor.close();
} else {
film = null;
}
return film;
}
public void contentAddFilm(Film film) {
ContentValues values = new ContentValues();
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_TITLE, film.getTitle());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RATING, film.getRating());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_RUNTIME, film.getRuntime());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_CRITICS, film.getCritics());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_AUDIENCE, film.getAudience());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_SYNOPSIS, film.getSynopsis());
values.put(FilmDataContract.FilmEntry.COLUMN_FILM_PROFILE, film.getProfile());
myCR.insert(FilmProvider.CONTENT_URI, values);
}
This is my stack trace... Seems to be happening when I am passing the context.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference
at com.purewowstudio.topmovies.data.DatabaseHelper.<init>(DatabaseHelper.java:25)
at com.purewowstudio.topmovies.util.getFilms.sortData(getFilms.java:48)
at com.purewowstudio.topmovies.util.getFilms.doInBackground(getFilms.java:43)
at com.purewowstudio.topmovies.util.getFilms.doInBackground(getFilms.java:16)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            
DatabaseHelper dbHelper = new DatabaseHelper(mContext, null, null, 1);
mContext is null, because you never assign a value to it.

How to get 2 tables in 1 cursorAdapter?

In my app i have two different tables with different rows.
I trying to show the two tables on listView with the cursorAdapter.
But cursorAdapter gets query only one table to show.
how can i set the two tables two one cursorAdapter?
here is my code:
MainActivity:
hand = new DbHandler(getActivity());
Cursor c = hand.getDayCursor(workName);
adapterC = new CustomCursorAdapter(getActivity(), c, 0);
lv.setAdapter(adapterC);
From DbHandler:
public Cursor getSettingCursor(String workName) {
Cursor c = null;
try {
open();
c = myDb.query(TABLE_DAY, null, "workName=?", new String[]{workName}, null, null, null, null);
c.getInt(0);
c.getString(1);
c.getFloat(2);
c.getFloat(3);
c.getInt(4);
c.getInt(5);
c.getInt(6);
c.getInt(7);
c.getInt(8);
c.getFloat(9);
c.getFloat(10);
c.getFloat(11);
c.getInt(12);
c.getInt(13);
c.getFloat(14);
c.getFloat(15);
c.getFloat(16);
c.getInt(17);
c.getInt(18);
c.getFloat(19);
c.getFloat(20);
c.getFloat(21);
} catch (Exception e) {
// TODO: handle exception
}finally{
close();
}
return c;
}
public Cursor getDayCursor(String workName) {
Cursor c = null;
try {
open();
c = myDb.query(TABLE_DAY, null, "workName=?", new String[]{workName}, null, null, null, null);
c.getInt(0);
c.getString(1);
c.getFloat(2);
c.getFloat(3);
c.getInt(4);
c.getInt(5);
c.getInt(6);
c.getInt(7);
c.getInt(8);
c.getFloat(9);
c.getFloat(10);
c.getFloat(11);
c.getInt(12);
c.getInt(13);
c.getFloat(14);
c.getFloat(15);
c.getFloat(16);
c.getInt(17);
c.getInt(18);
c.getFloat(19);
c.getFloat(20);
c.getFloat(21);
} catch (Exception e) {
// TODO: handle exception
}finally{
close();
}
return c;
}
cursorAdapter:
#Override
public void bindView(View view, Context context, Cursor cursor) {
int dateDay = cursor.getInt(cursor.getColumnIndex(DbHandler.DATE_DAY));
int dateMonth = cursor.getInt(cursor.getColumnIndex(DbHandler.DATE_MONTH));
int dateYear = cursor.getInt(cursor.getColumnIndex(DbHandler.DATE_YEAR)); .....
}
and one more question, in the Dbhandler ,do i have to put "c.getInt(0);...c.getString(1);.."?
Thank's helpers..

Display sqlite data on listview

I have to show saved sqlite data on a listview. But only one column's data is enough to show.Here is my codes. I want to get "tarih" column's data.
public class TarihDataSource {
private SQLiteDatabase database;
private DatabaseOlusturma dbOlustur;
private String []allColumns = {"tarih","gunluknotu","resim"};
public TarihDataSource(Context context) {
dbOlustur = new DatabaseOlusturma(context);
}
public void open() throws SQLException {
database = dbOlustur.getReadableDatabase();
}
public void close() {
dbOlustur.close();
}
public List<TarihListHelper> getAllTarih() {
List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();
Cursor cursor = database.query("gunluker", allColumns, null, null,
null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
TarihListHelper comment = cursorToComment(cursor);
tarihlistesi.add(comment);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return tarihlistesi;
}
private TarihListHelper cursorToComment(Cursor cursor) {
TarihListHelper comment = new TarihListHelper();
comment.setTarih(cursor.getString(1));
return comment;
}
}
What is the wrong in this codes?
This code should help you.
public List<TarihListHelper> getAllTarih()
{
List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();
Cursor cursor = database.query(DB_NAME, columns, null, null, null, null, null);
while(cursor.moveToNext())
{
TarihListHelper tarihlistesi= cursorToComment(cursor);
tarihlistesi.add(comment);
}
cursor.close();
return tarihlistesi;
}
Where columns is the String[] which contains the names of all the columns in your database table.
This way you will get all the columns, then you can select from which column do want to read data.
Hope this helps.
You simply need an Adapter to your list. In your case i think that a SimpleCursorAdapter is the best.
Check this official page and this example.
public class TarihDataSource {
private SQLiteDatabase database;
private DatabaseOlusturma dbOlustur;
private final Context context;
private String []allColumns = {"tarih","gunluknotu","resim"};
public TarihDataSource(Context _context) {
context = _context;
dbOlustur = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
public void open() throws SQLException {
database = dbOlustur.getReadableDatabase();
}
public void close() {
dbOlustur.close();
}
public List<TarihListHelper> getAllTarih() {
List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();
Cursor cursor = database.query("gunluker", allColumns, null, null,
null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
TarihListHelper comment = cursorToComment(cursor);
tarihlistesi.add(comment);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return tarihlistesi;
}
private TarihListHelper cursorToComment(Cursor cursor) {
TarihListHelper comment = new TarihListHelper();
comment.setTarih(cursor.getString(1));
return comment;
}
}
Try this.

how do i fetch a record from SQL

sorry im kind of a beginner,
I have a simple app that stores data on one page and fetches it on another. i keep getting an error when i try and fetch the data, stating that no such column could be found. please help
this is my code from where i enter the data,
EnterDetails.java
#Override
public void onClick(View arg0) {
EditText holdersala = (EditText) findViewById(R.id.rawsalary);
EditText wantspercentage = (EditText) findViewById(R.id.wantspercent);
EditText needspercentage = (EditText) findViewById(R.id.needspercent);
String holdersalary = holdersala.getText().toString();
final int salary = Integer.parseInt(holdersalary);
String holderwantsp = wantspercentage.getText().toString();
final int wantsp = Integer.parseInt(holderwantsp);
String holderneedsp = needspercentage.getText().toString();
final int needsp = Integer.parseInt(holderneedsp);
if(wantsp + needsp <= 100){
DatabaseAdapter databaseAdapter = new DatabaseAdapter(getApplicationContext());
databaseAdapter.open();
databaseAdapter.createRecordS(salary);
databaseAdapter.createRecordW(wantsp);
databaseAdapter.createRecordN(needsp);
databaseAdapter.close();
startActivity(new Intent(EnterDetails.this, Summary.class));
} else {
}
Toast toast = Toast.makeText(EnterDetails.this, "incorrect data", 5000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
code from where i fetch the data
TextView bn = (TextView) findViewById(R.id.budget_need);
DatabaseAdapter databaseAdapter = new DatabaseAdapter(getApplicationContext());
databaseAdapter.open();
bn.setText(databaseAdapter.fetchRecordS(1));
databaseAdapter.close();
and code from my databaseAdapter.java
public class DatabaseAdapter {
private Context context;
private SQLiteDatabase database;
private DatabaseOpenHelper dbHelper;
public DatabaseAdapter(Context context) {
this.context = context;
}
public DatabaseAdapter open() throws SQLException {
dbHelper = new DatabaseOpenHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
public long createRecordS(int interger) {
ContentValues contentValue = new ContentValues();
contentValue.put("salafig", interger);
return database.insert("maindata", null, contentValue);
}
public long createRecordN(int interger) {
ContentValues contentValue = new ContentValues();
contentValue.put("needsper", interger);
return database.insert("maindata", null, contentValue);
}
public long createRecordW(int interger) {
ContentValues contentValue = new ContentValues();
contentValue.put("wantsper", interger);
return database.insert("maindata", null, contentValue);
}
public int fetchRecordS(long rowId) throws SQLException {
Cursor mCursor = database.query(true, "maindata", new String[] { "salafig",
"needsper", "wantsper" }, "salafig"+ rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
return (mCursor.getInt(1));
}
return (Integer) null;
}
public int fetchRecordW(long rowId) throws SQLException {
Cursor mCursor = database.query(true, "maindata", new String[] { "salafig",
"needsper", "wantsper" }, "wantsper"+ rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
return (mCursor.getInt(1));
}
return (Integer) null;
}public int fetchRecordN(long rowId) throws SQLException {
Cursor mCursor = database.query(true, "maindata", new String[] { "salafig",
"needsper", "wantsper" }, "needsper"+ rowId, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
return (mCursor.getInt(1));
}
hope it makes sense thanks if advance for any help.
Look at HEREto check the correct syntax of the SQLiteDatabase query method.
The forth parameter of the method is the selection condition, it s supposed to be something like:
"salafig = "+ rowId"
instead of this :
"salafig"+ rowId"

putExtra method values?

public class Breakfast extends Activity implements OnItemClickListener {
DBOpener dbopener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//for fullscreen view
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.dinner);
dbopener = new DBOpener(this);
}
// Open the DB, query all subject codes and refresh the listview when app resumes
#Override
protected void onResume() {
super.onResume();
// Configure the listview
ArrayList<String> mealNames = new ArrayList<String>();
ListView lstDine = (ListView)this.findViewById(R.id.dine);
lstDine.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mealNames));
// Open/create the DB
try
{
dbopener.createDatabase(); // Create DB if necessary
dbopener.openDatabase(); // Open the DB
Cursor dinners = dbopener.getBreakfastNames();
while (dinners.moveToNext()) {
mealNames.add(dinners.getString(0)); // Get the Lunch Name & adds to list
}
dinners.close();
// Update the listview
ArrayAdapter<String> ad = (ArrayAdapter<String>)lstDine.getAdapter();
ad.notifyDataSetChanged();
lstDine.setOnItemClickListener(this);
}
catch (Exception e)
{
Toast.makeText(this, "Could not open DB", //Display when Database Cannot be opened
Toast.LENGTH_LONG).show();
}
}
//Close the DB when app pauses
#Override
protected void onPause() {
super.onPause();
dbopener.close();
}
// When user clicks on an item
public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
switch(pos)
{
case 0 :
Intent event1 = new Intent("com.edu.tp.iit.mns.Display");
//event1.putExtra("name" , ???);
//event1.putExtra("nutrition" , ???);
//event1.putExtra("rating" , ???);
startActivity(event1);
break;
this is only part of the code. i want to know what should i put inside the (???). its a list view item..so for case 0 i want the name nutrition and rating to be displayed in Display class. and this is my database done using SQLite Database browser.
public class DBOpener extends SQLiteOpenHelper {
private static String DB_PATH =
"/data/data/com.edu.tp.iit.mns/databases/"; //path of our database
private static String DB_NAME ="finals"; // Database name
private final Context myContext;
private SQLiteDatabase db;
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public DBOpener(Context context) {
super(context, DB_NAME, null, 1);
myContext = context;
}
public void createDatabase() throws IOException {
boolean dbExists = checkDatabase();
if (dbExists) {
// Do nothing, DB already exists
Log.d("DBOpener", "DB exists");
} else {
// By calling this method an empty database will be created
// in the default system path of your application, which we
// will overwrite with our own database.
Log.d("DBOpener", "DB does not exit - copying from assets");
this.getReadableDatabase();
copyDatabase();
}
}
private boolean checkDatabase() {
SQLiteDatabase checkDB = null;
try {
// Try opening the database
String path = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// If it fails, DB does not exist
}
if (checkDB != null)
checkDB.close(); // Close the DB; we don’t need it now
return checkDB != null;
}
private void copyDatabase() throws IOException {
InputStream istream = myContext.getAssets().open(DB_NAME);
OutputStream ostream = new FileOutputStream(DB_PATH + DB_NAME);
// Transfer bytes from istream to ostream
byte[] buffer = new byte[1024];
int length;
while ((length = istream.read(buffer)) > 0) {
ostream.write(buffer, 0, length);
}
// Close streams
istream.close();
ostream.flush();
ostream.close();
}
public void openDatabase() throws SQLiteException {
db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
SQLiteDatabase.OPEN_READWRITE);
}
#Override
public synchronized void close() {
if (db != null)
db.close();
super.close();
}
// Retrieve all subject codes
public Cursor getDinnerNames() {
if (db == null)
return null;
return db.query("dinner", new String[] {"name"},
null, null, null, null, null);
}
// Get details of specific subject
public Cursor getDinnerDetails(String name) {
if (db == null)
return null;
return db.query("dinner", new String[] {"name", "nutrition", "rating"},
"name = ?", new String[] {name}, null, null, null);
}
// Retrieve all subject codes
public Cursor getLunchNames() {
if (db == null)
return null;
return db.query("lunch", new String[] {"name"},
null, null, null, null, null);
}
// Get details of specific subject
public Cursor getLunchDetails(String name) {
if (db == null)
return null;
return db.query("dinner", new String[] {"name", "nutrition", "rating"},
"name = ?", new String[] {name}, null, null, null);
}
// Retrieve all subject codes
public Cursor getBreakfastNames() {
if (db == null)
return null;
return db.query("breakfast", new String[] {"name"},
null, null, null, null, null);
}
// Get details of specific subject
public Cursor getBreakfastDetails(String name) {
if (db == null)
return null;
return db.query("breakfast", new String[] {"name", "nutrition", "rating"},
"name = ?", new String[] {name}, null, null, null);
}
}
So putExtra() method add extended data to the intent. So you use it when you want to pass data through Activities, you can put all primitive types like float, integer, short or also reference types like String. You can add Bundle object with method putExtras() also other Objects. So you add to intent datatypes you need to.
See this:
Example of add Object to Intent:
Intent intent = new Intent(DownloadingActivity.this, DownloadService.class);
intent.putExtra("url", "http://dl.dropbox.com/u/67617541/DOR0023.rar");
intent.putExtra("receiver", new DownloadReceiver(new Handler()));
You should read something about Intent here
So you created ArrayAdapter of Strings and you use getBreakfastNames() that return only name of breakfast so you can add to intent only
String name = (String) parent.getItemAtPosition(pos);
event1.putExtra("name", name);
but i recommend to you create class that extend from for example SimpleCursorAdapter and use design pattern Holder to full control over your data in ListView. It's cleaner, faster.

Categories

Resources