简体   繁体   English

SQLite Android。 管理资料

[英]SQLite Android. Managing data

I'll tell you the aim of this part of my app. 我将告诉您应用程序这一部分的目标。 I've got a SQLite dB which has 3 columns. 我有一个SQLite dB,其中有3列。 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. 好吧,我要做的是获取整个dB并通过电子邮件发送。 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. 到目前为止,我的代码工作正常,它在textView中显示数据。 Here is the problem. 这是问题所在。 My dB has a maximum of 15 rows. 我的dB最多有15行。 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. 第一行= filas(0),第二行= filas(1)...以便能够将此数组传递给另一个活动。 If the array has less than 15 rows i think it would give an exception. 如果数组少于15行,我认为它将给出异常。 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". 我收到的电子邮件消息为“空”。

I think you problem is the following: you never initialize String[] filas; 我认为您的问题如下:您从未初始化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? 为什么只在catch子句中调用下一个活动? Having so much code in the catch clause is very bad practise it is called expection handling . 在catch子句中包含太多代码是非常糟糕的做法,这被称为期望处理 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. 但是,不要忘记将代码放置在catch块之外,因为在修改之后,我不希望有任何异常。

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. 我不熟悉您使用的SQLManager类。 However the standard Android way is very similar to the JDBC model. 但是,标准的Android方法与JDBC模型非常相似。 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/ 在此处查看使用游标和SQLitehleprs示例: http : SQLitehleprs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM