2012-05-31 29 views
1

(這是我的lunch.class其填充ListView從database.tell我,如果有任何mistakes.im還是新來這個。)如何將數據從sqlite listview傳遞到新的意圖?

public class Lunch extends Activity implements OnItemClickListener { 

DBOpener dbopener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    //for fullscreen view 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    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.getLunchNames(); 
     while (dinners.moveToNext()) { 
      mealNames.add(dinners.getString(0)); // Get the current subj 
                // code, add 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", 
      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) { 
    // Use subject code from listview to retrieve other 
     // details with the dbopener 

    switch(pos) 
    { 
    case 0 : 
    Intent newActivity = new Intent("com.edu.tp.iit.mns.Display"); 
    newActivity.putExtra("1", "2"); // this is where im unaware of the codes.how to pass the strings of value to the next page 
    startActivity(newActivity); 
    break; 
    } 
    switch(pos) 
    { 
    case 1 : 
    Intent newActivity = new Intent("com.edu.tp.iit.mns.Display"); 
    startActivity(newActivity); 
    break; 
    } 
    switch(pos) 
    { 
    case 2 : 
    Intent newActivity = new Intent("com.edu.tp.iit.mns.Display"); 
    startActivity(newActivity); 
    break; 
    } 
    switch(pos) 
    { 
    case 3 : 
    Intent newActivity = new Intent("com.edu.tp.iit.mns.Display"); 
    startActivity(newActivity); 
    break; 
    } 
    switch(pos) 
    { 
    case 4 : 
    Intent newActivity = new Intent("com.edu.tp.iit.mns.Display"); 
    startActivity(newActivity); 
    break; 
    } 
    switch(pos) 
    { 
    case 5 : 
    Intent newActivity = new Intent("com.edu.tp.iit.mns.Display"); 
    startActivity(newActivity); 
    break; 
    } 
    switch(pos) 
    { 
    case 6 : 
    Intent newActivity = new Intent("com.edu.tp.iit.mns.Display"); 
    startActivity(newActivity); 
    break; 
    } 
    switch(pos) 
    { 
    case 7 : 
    Intent newActivity = new Intent("com.edu.tp.iit.mns.Display"); 
    startActivity(newActivity); 
    break; 
    } 

} 

(這是display.class(圖片2)工程就像一個模板來顯示圖像一樣的動態信息,食品名稱,食品的描述和其評級

public class Display extends Activity { 

DBOpener dbopener; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.display); 

    dbopener = new DBOpener(this);  
} 

@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 
     Toast.makeText(this, " open DB", 
       Toast.LENGTH_LONG).show(); 
     Cursor dinners = dbopener.getLunchNames(); 
     while (dinners.moveToNext()) { 
    //    mealNames.add(dinners.getString(0)); // Get the current subj 
                // code, add 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", 
      Toast.LENGTH_LONG).show(); 
    } 
} 

// Close the DB when app pauses 
    @Override 
    protected void onPause() { 
     super.onPause(); 

     dbopener.close(); 
    } 

} 

,最後我dpopener文件:

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

u能幫助我開始案例0,以便我可以完成代碼。例如,用戶點擊燒烤雞肉三明治。它將導航到display.class並從數據庫中檢索信息並顯示圖像評分和內容。

+0

對於每一種情況,您都有一個switch語句。你應該有一個單獨的switch語句,裏面有很多例子。閱讀如何正確使用switch語句來幫助清理代碼。 – dymmeh

回答

0

要通過意圖將參數傳遞給新活動,請使用putExtra(),就像您將代碼放在代碼中一樣: newActivity.putExtra(「1」,「2」);

,第一個參數是一個字符串常量,以確定您的參數,您將使用此字符串後得到的數值。第二個參數是值,android擁有所有基元的方法。

在新活動中,您使用getExtra(「1」,「2」),其中1是您先前使用的常數,2是默認值,以防萬一它找不到您的值或常數用過的。

+0

謝謝先生的回覆。我知道你在說什麼。但很抱歉再次提問。你說的字符串常數,我迷路了。你可以仔細看看我的DPOpener文件,並告訴我哪個字符串常量,我想把它放在putExtra方法中? – Riyas2329

+0

任何字符串都會執行,您只需在放置和檢索時使用相同的字符串。那就要看你的代碼,你可以只通過像putExtra標識符(「com.example.TYPE」,「TYPE1」),或者可能通過直接詢問,如putExtra(「com.example.SQLQUERY」,「SELECT * FROM D b;」)。 –

相關問題