5

现在我使用 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;
    }   
}
4

2 回答 2

3

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

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(); 
于 2013-02-13T18:12:02.003 回答
1

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

仅当您的 listViewScore 的内容发生更改时才调用它..并在那时使用它-

代替

listView.getAdapter().notifyDatasetChanged();

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

看看魔法……

谢谢。

于 2013-02-13T18:02:47.137 回答