Skip to main content

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.


Comments

  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

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.