2012-06-03 92 views
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; 

這只是代碼的一部分。我想知道我應該在(???)裏面放什麼。它的一個列表視圖項目..所以爲案例0我想名稱營養和評級顯示在顯示類。這是我的數據庫使用SQLite數據庫瀏覽器完成的。putExtra方法值?

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); 
    } 
} 
+0

您沒有指定您的問題是什麼 –

回答

2

所以putExtra()方法將擴展數據添加到意圖。因此,當您想要通過Activities傳遞數據時,您可以使用它,您可以將所有基本類型(例如float,integer,short)或參考類型(如String)。您可以添加Bundle對象,方法putExtras()也可以添加其他Objects因此,您需要將目標數據類型添加到

看到這個:

enter image description here

例添加ObjectIntent

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())); 

你應該閱讀一些有關意向here

所以你創建的字符串的ArrayAdapter,並使用getBreakfastNames()只返回名稱早餐,所以你可以添加到意圖僅

String name = (String) parent.getItemAtPosition(pos); 
event1.putExtra("name", name); 

,但我建議你創建一個從例如SimpleCursorAdapter延伸,並ListView您的數據使用設計模式Holder完全控制類。它更乾淨,更快。

+0

謝謝先生。我試着現在putextra ..im編輯Dsiplay類來檢索item.i會再次與我的代碼再次與您先生。非常感謝。 (對不起,但是我的提交是在即將到來的星期五和我的模塊的70%。im stucked sir.pls幫助我。) – Riyas2329

+0

先生,我已經給你回覆了一個回覆。對不起,麻煩 – Riyas2329

+0

是啊會看看它,但現在對不起,我現在沒有時間,以後好嗎? – Sajmon