Monday, March 5, 2012

SQLlite Android. Managing data


Hi friends. I'll tell you the aim of this part of my app. I've got a SQLite dB which has 3 columns. First is "Quantity", Second is "Product" and third is "Price". Well, what i want to do is to get the whole dB and send it by email. This is what i have right now:




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



Up to here, my code works fine, it displays the data in the textView. Here is the problem. My dB has a maximum of 15 rows. What i want to do is to store each row in a position of a string array (filas). First row = filas(0), second row = filas(1)...in order to be able to pass this array to another activity. If the array has less than 15 rows i think it would give an exception. So it's the time to open the other activity.




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

}
});
}
}



The other activity is this:




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 = {"ulrickpspgo@gmail.com"};

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

}
}



What i get as the message of my email is "null". Could you help me, please? Thanks a lot.

1 comment:

  1. I think you problem is the following: you never initialize String[] filas;. This means that it remains null all the time. Afterwards you go to your code:

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


    I have added comments for some of your lines. Why do you call the next activity only in the catch clause? Having so much code in the catch clause is very bad practise it is called expection handling. don't do it. Otherwise if you initialize your array (which I am not sure you have not already done, but this is my guess what the exception you hide is) you should be good to go. Do not forget to place the code outside the catch block, though, because I do not expect any exceptions after the modification.

    EDIT I do not like the way you iterate over the elements in the database. I am not familiar with the SQLManager class you use. However the standard Android way is very similar to the JDBC model. You do a query and it returns a cursor over the results. See example of using cursors and SQLitehleprs over here: http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/2/

    ReplyDelete