如何从两个不同的活动中保存和获取SQLite中的数据?

时间:2011-06-04 17:19:01

标签: android sqlite get save

当我的应用程序启动时,我从用于服务器的符号响应的服务器获取tiket。这张票(字符串)我保存在数据库中,当我运行新的响应时,我从数据库获取此tiket并附加到响应。当我从同一活动中运行回复时,一切都很好,我保存了我的机票。我的意思是,当我保存tiket时,我还会释放我正在使用数据库执行此操作的活动上下文。但是当我在一个活动中保存tiket时,启动另一个活动并尝试从第二个活动中的数据库获取票证,我得到null(rown不包含项目)。那么,任何人都可以告诉我,我在哪里错了吗?

我感谢任何帮助。谢谢。

这里代码:

    public long setTicket(Ticket ticket) throws SQLException {
    ContentValues insertValues = new ContentValues();
    insertValues.put(TicketTable.KEY_TICKET, ticket.getTicket());
    insertValues.put(TicketTable.KEY_RESULT_CODE, ticket.getResultCode());

    long result = m_db.insert(DATABASE_TABLE_TICKET, null, insertValues);

    return result;
}

public Ticket getTicket() throws SQLException {
    String[] columns = { TicketTable.KEY_ID, TicketTable.KEY_TICKET,
            TicketTable.KEY_RESULT_CODE };
    Cursor cursor = getCursor(DATABASE_TABLE_TICKET, columns);

    Ticket result = new Ticket();
    result.setTicket(cursor.getString(TicketTable.TICKET_COLUMN));
    result.setResultCode(cursor.getString(TicketTable.RESULT_CODE_COLUMN));

    cursor.close();

    return result;
}

这里我设置数据库访问权限的方法:

    public String getTicket(){
    DBAdapter adapter = new DBAdapter(m_context);
    adapter.open();
    String ticketEncoded = null;
    Ticket ticket = null;
    try {
        ticket = adapter.getTicket();
        ticketEncoded = java.net.URLEncoder.encode(ticket.getTicket());
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        adapter.close();
    }

    return ticketEncoded;
}

public void saveTicketFromServer(){
    GetTicketHandler handler = new GetTicketHandler(m_context);
    Ticket ticket = new Ticket();
    ticket.setCommand("getTicket");

    TicketTask task = new TicketTask(ticket);
    createTask(task, handler);
}

在这里,我发现了无票的票:

        String comment = java.net.URLEncoder.encode(strToEncode, "UTF-8");
    String barcodeId = "4605246006340";
    String ball = "10";
    String sign = account.getTicket();//here!
    CreateCommentTask task = new CreateCommentTask(barcodeId, ball,
            comment, sign);
    //TODO create handler
    HttpService httpService = new HttpService();
    httpService.createTask(task, null);

但在另一个回复中一切都很好:

String sign = account.getTicket();
    try {
        task = new SearchBarcodeTask(searchForm, sign);
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    SearchBarcodeHandler handler = new SearchBarcodeHandler(m_activity);

    HttpService httpService = new HttpService();
    httpService.createTask(task, handler);

1 个答案:

答案 0 :(得分:0)

好吧,我看到的问题是String sign = account.getTicket();//here!

帐户必须为null。因此,请检查您提到的其他两个方法调用之间的不同之处。无论什么帐户,你都在做一些不同的事情。