Android: startActivityForResult para ir en una segunda actividad y guardar datos en sqlite

the data storage doesn't seem to work in my case. I read a lot in forums but couldn't find any answer to it. So here goes my issue:

I have an Activity called MainActivity that starts a second Activity named VertragHinzufuegen. This class allows entering some user data and confirm them with a button. The button has an onClickListener with which I want to save all data in SQL. But these data don't appear in my list :(

private void createAgreement() {
     Intent i = new Intent(this, VertragHinzufuegen.class);
     startActivityForResult(i, ACTIVITY_CREATE);

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

mConfirmButton.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
          Log.d(TAG, "mConfirmButton clicked");

          // Progress circle for loading
          ProgressDialog pdialog =, "", "Lade. Bitte warten...", true);

          // TODO:
          // Here happens the magic of data storage :)          

          // Send callback


Well, that is the point.

Besides the MainActivity class and the VertragHinzufuegen class there exists the class VertraegeListe. The MainActivity contains a TabHost which includes VertraegeListe as intent. And the population of the list is in the class VertraegeListe, so I canÄt call the population method in MainActivity.

private void fillData() {
        // Build adapter with agreement entries; Populate the agreement list
        Cursor cursor = mDbHelper.getAllAgreements();
        String[] fields = new String[] {
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.vertraege_datensatz, cursor,
                fields, new int[] {});

MainActivity -includes-> VertraegeListe -includes-> fillData()

¿Alguna idea?


How do I have to redefine my originally rawQuery??? I don't get it:

// Get all data from the table vertraege
 public Cursor getAllAgreements() {
  // Entity attributes
  String attributes = "t1._id,"
        + "t1._idvertragspartner,"
        + "t1._idvertragsart,"
        + "t1.kundennummer,"
        + "t1.vertragsname,"
        + "t1.startdatum,"
        + "t1.enddatum,"
        + "t1.kuendigungsfristdatum,"
        + "t1.kuendigungserinnerung,"
        + "t2._id,"
        + "t2.vertragspartnername,"
        + "t2.vertragspartnerkontaktperson,"
           + "t2.anschrift,"
           + "t2.postleitzahl,"
           + "t2.ort,"
           + ","
           + "t2.festnetznummer,"
           + "t2.faxnummer,"
           + "t2.url,"
           + "t3._id,"
           + "t3.vertragsartname";


// Where clause String whereclause = "t2._id = t1._idvertragspartner AND t3._id = t1._idvertragsart";

// The entire structured query language statement String sqlstatement = "SELECT "+attributes+" FROM "+tables+" WHERE "+whereclause;

// rawQuery(String sql, String selectionArgs) return mDb.rawQuery(sqlstatement, null); }

The refreshing method I have rewritten as follows:

     * Get agreements from database and project records on list
    private void fillData() {
         // Get all of the rows from the database and create the item list
        Cursor mAgreementsCursor = mDbHelper.getAllAgreements();

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{mDbHelper.KEY_AGREEMENTNAME};

        // and an array of the fields we want to bind those fields to (in this case just agreementEntryText)
        int[] to = new int[]{};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter agreementsAdapter = 
                new SimpleCursorAdapter(this, R.layout.vertraege_datensatz, mAgreementsCursor, from, to);

preguntado el 08 de enero de 11 a las 21:01

1 Respuestas

If your list is populated by a Cursor you're going to need to tell the Cursor to update. This is done by calling Cursor#requery. They do not automatically update. This is the simplest method. You can read my answer for a similar question here: Android: ¿cómo usar CursorAdapter? for a more robust solution.

Deberías mirar Activity#startManagingCursor y Activity#managedQuery. You have to manage your Cursor life cycle along with your Activity's in order for the data to be populated correctly. Those two methods help simplify that for you.

contestado el 23 de mayo de 17 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.