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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:16:09+00:00 2026-06-04T23:16:09+00:00

public class Breakfast extends Activity implements OnItemClickListener { DBOpener dbopener; @Override protected void onCreate(Bundle

  • 0
 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);
    }
 }
  • 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-04T23:16:10+00:00Added an answer on June 4, 2026 at 11:16 pm

    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:

    enter image description here

    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.

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

Sidebar

Related Questions

public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*
public class Browser1Activity extends Activity { TextView url; WebView ourBrow; @Override protected void onCreate(Bundle
public class Offer_Popup extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.offer_popup); //newly
public class Boards : TabActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Tab);
public class HttpPostTask extends AsyncTask<Void, Integer, Void> { TextView txtStatus = (TextView)findViewById(R.id.txtStatus); @Override protected
public class abc<X extends Z> implements Iterable<X> { protected ArrayList<X> list; public Iterator<X> iterator()
public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
public class HomeActivity extends Activity{ // public ArrayList<User> users1 = new ArrayList<User>(); @Override public
public class CustomEditor : Editor { protected override void Render(HtmlTextWriter writer) { Toolbar topToolbar
public class TextBoxDerived : System.Web.UI.WebControls.TextBox { protected override void OnLoad(EventArgs e) { this.Controls.Add(new LiteralControl(Hello));

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.