0

I am having a problem when I update a certain field in my SQL database and then hitting the back button, back to my gridview (in my Level class). The intention is that I have a image and a text in a gv item, and I want the text to be updated if a user gave a correct answer:

//in my custom adapter:
//...
        if (mView != null) {

            int correctAnswer = getItem(position).getAnswerCorrect();
            if(correctAnswer == 1) { text.setText("yea"); } else { text.setText("too bad"); }
}

If you click an item you will get to the Answer.java:

if (qAnswer.equals(userAnswer)) {
                Toast.makeText(getApplicationContext(), ("Answer Correct"),
                        Toast.LENGTH_SHORT).show();
//update database:
                myDbHelper.updateAnswerCorrect((int)id, level);

            } else {
                Toast.makeText(getApplicationContext(), ("Wronggggg"),
                        Toast.LENGTH_SHORT).show();
            }

The strange thing is that when I go to the main screen and start Level.java again(Answer -> Level -> Main -> Level), it is displayed on the right way. How come I cant see it if I just go back one screen back to the gridview (Answer -> Level)?

I tried to put this in my Level class but it doesnt seem to work:

@Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        myAdapter.notifyDataSetChanged();
    }
Jack Commonw
  • 395
  • 6
  • 16

1 Answers1

1

You'll have to use a managed cursor ( http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/3/ ), or just recreate a fresh adapter (myAdapter = new MyAdapter(...)) and the call gridview.setAdapter() with the new one in your onResume()

petey
  • 16,914
  • 6
  • 65
  • 97
  • I tried @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); myAdapter= new CustomArrayAdapter(this, questions); gv.setAdapter(myAdapter); } , But this also doesn't work... I still have the same problem :s – Jack Commonw Oct 05 '12 at 13:13
  • Yer also going to have to recreate the data part named "questions" if you dont go the managed query route. – petey Oct 05 '12 at 13:15
  • Ok thanks, I now have super.onResume(); loadDataBase(); questions = new ArrayList(); questions = myDbHelper.getQuestionsLevel(level); myAdapter= new CustomArrayAdapter(this, questions); gv.setAdapter(myAdapter);, and this works. Is this not asking a lot memory to reload this always? Seems like it's not the proper way, it might also be just a feeling :) – Jack Commonw Oct 05 '12 at 13:22