【问题标题】:How do I get notifyDatasetChanged() to work with a ListAdapter?如何让 notifyDatasetChanged() 与 ListAdapter 一起工作?
【发布时间】:2013-02-13 17:48:52
【问题描述】:

现在我使用 setAdapter 来更新我的 ListView,但我认为正确的方法是使用 notifiyDatasetChanged() 并且我无法让它在我的主类中工作(它在适配器中)。这是错误:

未定义 ListAdapter 类型的 notifyDatasetChanged() 方法

我猜有更好的方法来做到这一点 - 谁能指出我正确的方向?

这是我的代码的相关部分:

public class ScoreList extends SherlockFragmentActivity {

    private ListView listViewScore;

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

        listViewScore.setAdapter(new ScoreListAdapter(ctx,
                R.layout.score_row_item, listScore));
        listViewScore.getAdapter().notifyDatasetChanged(); //this is where I get the error
    }
}

这是适配器:

public class ScoreListAdapter extends ArrayAdapter<Score> {
    private int resource;
    private LayoutInflater inflater;

    public ScoreListAdapter(Context ctx, int resourceId, List<Score> objects) {
        super(ctx, resourceId, objects);
        resource = resourceId;
        inflater = LayoutInflater.from(ctx);
        //context = ctx;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        convertView = (LinearLayout) inflater.inflate(resource, null);

        Score score = getItem(position);

        TextView txtName = (TextView) convertView.findViewById(R.id.name);
        txtName.setText(score.getName());

        TextView txtScoreChange = (TextView) convertView
                .findViewById(R.id.scoreChange);
        int scoreChange = Integer.parseInt(score.getScoreChange());
        if (scoreChange > 0)
            txtScoreChange.setText("+" + scoreChange);
        else if (scoreChange < 0)
            txtScoreChange.setText("" + scoreChange);
        else
            txtScoreChange.setText("");

        TextView txtScoreTotal = (TextView) convertView
                .findViewById(R.id.scoreTotal);
        txtScoreTotal.setText(score.getScoreTotal());

        final LinearLayout currentRow = (LinearLayout) convertView
                .findViewById(R.id.scoreRowLayout);                 

        notifyDataSetChanged();
        return convertView;
    }   
}

【问题讨论】:

  • 它是 notifyDataSetChanged() - 注意大写的 S。您显示的行中有一个小写的“s”有错误。
  • 首先你需要知道notifyDatasetChanged的使用...通知附加的观察者底层数据已经改变并且任何反映数据集的View都应该刷新自己。在实施之前了解这一点... notifyDatasetChanged() 将在您的适配器数据中的数据更新或简单更改时使用..即与适配器关联的集合已更改。无需在创建中调用它
  • 谢谢,我剪掉了很多代码,所以我没有在创建时调用它,它只是在这里看起来那样。

标签: java android listview android-arrayadapter


【解决方案1】:

创建自定义适配器的实例,以便您可以在任何您喜欢的地方使用它...

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

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

adapter.notifyDatasetChanged(); 

【讨论】:

  • 谢谢,这是一个非常简单的解释,而且效果很好(除了我必须在 notifyDataSetChanged() 中将 Set 大写;正如其他人指出的那样)
  • 啊,是的,我只是忽略了它 :) 但正如我所说,如果您的列表已经加载,则没有必要在您的情况下使用它。
【解决方案2】:

不要调用 notifyDataSetChanged();创建时的方法。

仅在您的 listViewScore 内容发生变化时调用它......并在那时使用它-

替换

listView.getAdapter().notifyDatasetChanged();

((ScoreListAdapter)listView.getAdapter()).notifyDataSetChanged();

看看魔法......

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-02
    • 2017-06-25
    • 2020-12-13
    • 2012-02-02
    • 2011-02-22
    • 2013-03-24
    • 2016-07-22
    • 2011-08-17
    相关资源
    最近更新 更多