0

While inserting my listview gets refreshed automatically but not update when the item in the listview is updated. It only updates on database. I can see the listview is updated when I close the application and open again, or come back from previous activity.

I found some discussion related to my problem. Like: Refresh ListView with ArrayAdapter after editing an Item . Her I found that make a new method to populate the Listview and call it in the onResume method of your activity. And the problem has been solved using this. But I do not get how to make new method mentioned like there. Could anybody help me to make it understandable?

My code in activity class:

personNamesListView = (ListView) findViewById(R.id.traineeslist);
    traineeListAdapter = new ArrayAdapter<Trainee>(this,
            android.R.layout.simple_list_item_1,
            currentTraining.getTraineeArrayList());

    personNamesListView.setAdapter(traineeListAdapter);

protected void onResume() {
    super.onResume();
}

And this way I populated my personNamesListView using method stringToString() in model class;

public void loadTraineeList() {

    DatabaseHelper db = DatabaseHelper.getInstance();

    this.traineeArrayList = new ArrayList <Trainee>();

    Cursor cursor = db.select("SELECT * FROM person p JOIN attendance a ON p._id = a.person_id WHERE training_id="+Integer.toString(this.getId())+";");

    while (cursor.moveToNext()) {
        Trainee trainee = new Trainee();
        trainee.setID(cursor.getInt(cursor.getColumnIndex(DatabaseHelper.PERSON_ID)));
        trainee.setFirstname(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_FIRSTNAME)));
        trainee.setLastname(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_LASTNAME)));
        trainee.setJobTitle(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_JOBTITLE)));
        trainee.setEmail(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_EMAIL)));
        trainee.setCompany(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_COMPANY)));
        trainee.setDepartment(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_DEPARTMENT)));
        trainee.setBadgeNumber(cursor.getString(cursor.getColumnIndex(DatabaseHelper.PERSON_BADGE)));


        // Pass to the arraylist
        this.traineeArrayList.add(trainee);
    }
}
public ArrayList<Trainee> getTraineeArrayList() {
    return traineeArrayList;
}

public void setTraineeArrayList(ArrayList<Trainee> traineeArrayList) {
    this.traineeArrayList = traineeArrayList;
}

I insert and Update data into database into one method:

public void storeToDB() {

    DatabaseHelper db = DatabaseHelper.getInstance();

    db.getWritableDatabase();

    if (this.id == -1) {
        // Person not yet stored into Db => SQL INSERT
        // ContentValues class is used to store a set of values that the
        // ContentResolver can process.
        ContentValues contentValues = new ContentValues();

        // Get values from the Person class and passing them to the
        // ContentValues class
        contentValues.put(DatabaseHelper.PERSON_FIRSTNAME, this
                .getFirstname().trim().toUpperCase());
        contentValues.put(DatabaseHelper.PERSON_LASTNAME, this
                .getLastname().trim().toUpperCase());
        contentValues.put(DatabaseHelper.PERSON_JOBTITLE, this
                .getJobTitle().trim().toUpperCase());
        contentValues.put(DatabaseHelper.PERSON_EMAIL, this.getEmail());
        contentValues.put(DatabaseHelper.PERSON_COMPANY, this.getCompany()
                .trim().toUpperCase());
        contentValues.put(DatabaseHelper.PERSON_DEPARTMENT, this
                .getDepartment().trim().toUpperCase());
        contentValues.put(DatabaseHelper.PERSON_BADGE, this
                .getBadgeNumber().trim().toUpperCase());

        // here we insert the data we have put in values
        this.setID((int) db.insert(DatabaseHelper.TABLE_PERSON,
                contentValues));

    } else {
        // Person already existing into Db => SQL UPDATE

        ContentValues updateTrainee = new ContentValues();
        updateTrainee.put(DatabaseHelper.PERSON_FIRSTNAME, this
                .getFirstname().trim().toUpperCase());
        updateTrainee.put(DatabaseHelper.PERSON_LASTNAME, this
                .getLastname().trim().toUpperCase());
        updateTrainee.put(DatabaseHelper.PERSON_JOBTITLE, this
                .getJobTitle().trim().toUpperCase());
        updateTrainee.put(DatabaseHelper.PERSON_EMAIL, this.getEmail());
        updateTrainee.put(DatabaseHelper.PERSON_COMPANY, this.getCompany()
                .trim().toUpperCase());
        updateTrainee.put(DatabaseHelper.PERSON_DEPARTMENT, this
                .getDepartment().trim().toUpperCase());
        updateTrainee.put(DatabaseHelper.PERSON_BADGE, this
                .getBadgeNumber().trim().toUpperCase());

        db.update(DatabaseHelper.TABLE_PERSON, updateTrainee,
                DatabaseHelper.PERSON_ID+"= ?", new String[]{Integer.toString(this.getId())});

        System.out.println("Data updated");

    }

}
5
  • 1
    use the personNamesListView.notifyDataSetchange() after the insertion and deletion the item in list or update the items of list Commented Jan 22, 2014 at 10:31
  • And when I type personNamesListView. there is no suggestions like notifyDataSetChanges(). Commented Jan 22, 2014 at 11:01
  • Sorry brother its traineeListAdapter.notifyDataSetChanged() Commented Jan 22, 2014 at 11:03
  • Still it does not show any hint like that. it just shows; notify() and notifyAll(); Commented Jan 22, 2014 at 11:13
  • I am idiot. I made variable like: private ListAdapter traineeListAdapter; And declare on the onCreate() {traineeListAdapter = new ArrayAdapter<trainee>}. Now I changed and able to get traineeListAdapter.notifyDataSetChanged. But still it is not working. :( Commented Jan 23, 2014 at 8:13

3 Answers 3

1

You should call traineeListAdapter.notifyDataSetChanged() whenever you update your ArrayList representing the items in the ListView.

Sign up to request clarification or add additional context in comments.

2 Comments

Should I do this in the same activity?
Not necessarily from the same activity but you have to call it on the UI thread.
0

There's a similar question here that can give you some help.

Although I've accomplished something similar using

yourlistview.invalidateViews() after changing the data to show in the listview

when notifyDataSetChanged() didn't work.

EDIT:

After making all the operations in the data that I want to show i just set the adapter and try to refresh my listview by calling invalidateViews().

selectedStrings = new ArrayList<String>(typeFilterStrings);
adapter.setArrayResultados(selectedStrings);
listTypeFilter.invalidateViews();

It's not obligatory to set the adapter again in my case worked.

16 Comments

I am sorry to say that my listview is populated with array of items not with single items in each view. And I do not have interface to selectItem or item cannot be clickable. :(
I have search box using autocomplete text view. When user finds himself he clicks the dropdown string of autocomplete and all the data is redirected to the form from database. There he can edit his data and update to database.
But do you query the database every time you use the search box? I'm having trouble understanding how the app works sorry.
Yeah I query every time, while searching, I made somethng using if else statement . if user finds himself-> it returns the person_id and if he does not find, I pass id like(-1). You know in database there is no id with -1. Then it pass empty string to form to add new user.
So if you edit and save in database you should retrieve saved data from database. Are you certain you've made the database save correctly? make some log instead of populating the listview with your results.
|
0

use like this:

Create an instance of your custom adapter, so you can use it anywhere you like...

  public class ScoreList extends SherlockFragmentActivity {

    private ListView listViewScore;

    private ScoreListAdapter adapter;

    static List<Score> listScore = new ArrayList<Score>();
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.score_list);
        ctx = this;
        listScore = dbh.getAllScores();

        listViewScore = (ListView) findViewById(R.id.score_list);

        adapter = new ScoreListAdapter(ctx, R.layout.score_row_item, listScore);
        listViewScore.setAdapter(adapter);

((BaseAdapter) listViewScore.getAdapter()).notifyDataSetChanged(); 

    }
    }

By the way, if your listScore array is already loaded, then you do not need to use

adapter.notifyDatasetChanged();

15 Comments

If I am really understood. I cannot use this adapter.notifyDatasetChanged(); in my case because my listview is populated directly from the array of list. See my edited question. Can I really use this in my case?
brother you can use this method on adapter
after personNamesListView.setAdapter(traineeListAdapter); use traineeListAdapter.notifyDatasetChanged();
@user3180746 it doesn`t matter your data is populated from anywhere
Sorry friend. I can use this method if I use like this way: ((BaseAdapter) traineeListAdapter).notifyDataSetChanged(); But still it is not working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.