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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex