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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...