0

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 :(

MainActivity.java


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

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

VertragHinzufuegen.java


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

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

          // TODO:
          // Here happens the magic of data storage :)          
          mDbHelper.addAgreement("Testcontract","null","null","null","null");

          // Send callback
             setResult(RESULT_OK);
             finish();
         }
        });

UPDATE:

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.

VertraegeListe.java


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

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

Any idea?


UPDATE 2:

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

VertraegeDBAdapter.java


// 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.email,"
           + "t2.festnetznummer,"
           + "t2.faxnummer,"
           + "t2.url,"
           + "t3._id,"
           + "t3.vertragsartname";

// Entities String tables = DATABASE_TABLE_AGREEMENTS+" t1, " + DATABASE_TABLE_AGREEMENTPARTNER+" t2, " + DATABASE_TABLE_AGREEMENTTYPES+" t3";

// 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:

VertraegeListe.java


/**
     * 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();
        startManagingCursor(mAgreementsCursor);

        // 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[]{R.id.agreementEntryText};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter agreementsAdapter = 
                new SimpleCursorAdapter(this, R.layout.vertraege_datensatz, mAgreementsCursor, from, to);
        mAgreementList.setAdapter(agreementsAdapter);
    }
thkala
  • 84,049
  • 23
  • 157
  • 201

1 Answers1

0

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: how to use CursorAdapter? for a more robust solution.


You should look at Activity#startManagingCursor and 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.

Community
  • 1
  • 1
Rich Schuler
  • 41,814
  • 6
  • 72
  • 59