更新列表数据时自动更新Arraylist

时间:2014-01-22 10:28:51

标签: android listview android-listview android-arrayadapter

插入listview时会自动刷新,但更新listview中的项目时不会更新。它只更新数据库。我可以看到列表视图在关闭应用程序并再次打开时更新,或者从之前的活动中返回。

我发现了一些与我的问题相关的讨论。喜欢:Refresh ListView with ArrayAdapter after editing an Item。她发现创建了一个新方法来填充Listview并在活动的onResume方法中调用它。 问题已经解决了。但我不知道如何制作像那里提到的新方法。有人能帮助我让它变得可以理解吗?

我在活动类中的代码:

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();
}

这样我在模型类中使用方法stringToString()填充了我的personNamesListView;

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;
}

我将数据插入并将数据更新到一个方法中:

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");

    }

}

3 个答案:

答案 0 :(得分:1)

每当您更新代表traineeListAdapter.notifyDataSetChanged()中的项目的ArrayList时,您都应致电ListView

答案 1 :(得分:0)

有一个类似的问题here可以给你一些帮助。

虽然我已经使用

完成了类似的事情

yourlistview.invalidateViews() 更改数据后显示在列表视图中

当notifyDataSetChanged()不起作用时。

修改

在我要显示的数据中进行所有操作后,我只需设置适配器并尝试通过调用invalidateViews()来刷新listview。

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

在我的工作案例中,不必再次设置适配器。

答案 2 :(得分:0)

像这样使用:

创建自定义适配器的实例,以便您可以随意使用它...

  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(); 

    }
    }

顺便说一句,如果您的listScore数组已经加载,那么您不需要使用

adapter.notifyDatasetChanged();