2012-03-03 65 views
0

我會告訴你我的應用程序的這部分的目的。我有一個SQLite dB有3列。首先是「數量」,其次是「產品」,第三是「價格」。那麼,我想要做的就是獲得整個dB並通過電子郵件發送。 這就是我現在所擁有的:SQLite Android。管理數據

public class Visual extends Activity { 

TextView tv; 
Button sqlGetInfo; 
long l ; 
long b=1; 
int c,i; 
String returnedQuantity ,returnedProduct ,returnedPrice; 
String[] filas; 

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


    tv = (TextView) findViewById(R.id.tvSQLinfo); 
    sqlGetInfo = (Button) findViewById(R.id.EnviarPedido); 
    SQLManager info = new SQLManager(this); 
    info.open(); 
    String data= info.getData(); 
    info.close(); 
    tv.setText(data); 

到這裏,我的代碼工作正常,它顯示在TextView的數據。這是問題。我的dB最多有15行。我想要做的是將每行存儲在一個字符串數組(位置)的位置。第一行=絲狀(0),第二行=絲狀(1)...以便能夠將該陣列傳遞到另一活動。如果數組少於15行,我認爲它會給出一個例外。所以現在是時候打開其他活動了。

final SQLManager hon = new SQLManager(this); 
    sqlGetInfo.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      for (i = 1; i < 15; i++) { 

        try{ 
        l = (long) i; 
        hon.open(); 
        returnedQuantity = hon.getQuantity(l); 
        returnedProduct = hon.getProduct(l); 
        returnedPrice = hon.getPrice(l); 
        hon.close(); 
        c=(int)(l-b); 
        filas[c]="" + returnedQuantity+"  "+ returnedProduct+"  "+ returnedPrice + "\n"; 


        }catch (Exception e){ 

        i = 16; 
        l = (long) i; 
        Intent abrePedidos = new Intent(Visual.this, Pedidos.class); 
        abrePedidos.putExtra("pedidoCompleto", filas); 
        startActivity(abrePedidos); 
       } 
     } 

    } 
}); 
    } 
} 

另一項活動是這樣的:

public class Pedidos extends Activity { 

String[] filas; 
long numProd; 
boolean end; 
int i; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    filas=getIntent().getStringArrayExtra("pedidoCompleto"); 

    String subject = "Pedido a Domicilio"; 
    String cabecera = "Unid.  Producto   Precio\n\n"; 
    String[] emails = {"[email protected]"}; 

    String message = cabecera + filas; 

    Intent emailIntent = new Intent (android.content.Intent.ACTION_SEND); 
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, emails); 
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); 
    emailIntent.setType("plain/text"); 
    startActivity(emailIntent); 

    } 
} 

我得到了我的電子郵件的消息是「空」。

+0

如果我正確地找到了'filas',則在此行上爲null:'String message = cabecera + filas;'?或者你通過電子郵件引用了什麼? – 2012-03-03 12:33:38

+0

當通過startActivity調用emailIntent時,移動設備上的電子郵件應用程序(本例中爲gmail)顯示:電子郵件,主題和消息。什麼是零花絲,而不是cabecera。 – Aldridge1991 2012-03-03 12:47:37

回答

0

我認爲你的問題如下:你永遠不會初始化String[] filas;。這意味着它始終保持空白。之後,您轉到您的代碼:

try { 
    l = (long) i; 
    hon.open(); 
    returnedQuantity = hon.getQuantity(l); 
    returnedProduct = hon.getProduct(l); 
    returnedPrice = hon.getPrice(l); 
    hon.close(); 
    c=(int)(l-b); 
    // The next line throws null pointer exception 
    filas[c]="" + returnedQuantity+"  "+ returnedProduct+"  "+ returnedPrice + "\n"; 
} catch (Exception e) { // here you catch the null pointer exception 
    i = 16; 
    l = (long) i; 
    Intent abrePedidos = new Intent(Visual.this, Pedidos.class); 
    abrePedidos.putExtra("pedidoCompleto", filas); //filas is still null 
    startActivity(abrePedidos); 
} 

我已爲某些行添加了註釋。你爲什麼只在catch子句中調用下一個活動?在catch子句中有這麼多的代碼是非常糟糕的做法,它被稱爲expection handling。不要這樣做。否則,如果你初始化你的數組(我不確定你還沒有完成,但這是我猜測你隱藏的例外),你應該很好。不過,不要忘記將代碼放在catch塊之外,因爲我不希望修改後有任何異常。

編輯我不喜歡你迭代數據庫中元素的方式。我不熟悉你使用的SQLManager類。然而,標準的Android方式與JDBC模型非常相似。你做了一個查詢,它將結果返回一個光標。請參見使用遊標的示例,並在此處使用SQLitehleprshttp://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/2/

+0

我在catch塊上使用了這段代碼,因爲我不知道dB有多少行,所以當de cursor超出界限時,我可以猜測它使用了db的最後一項。你知道更好的方法來做到這一點嗎? – Aldridge1991 2012-03-03 13:08:58

+0

當然,考慮我的編輯(在5分鐘內出現) – 2012-03-03 13:09:48

+0

謝謝你的男人。它正在工作! – Aldridge1991 2012-03-03 13:26:21