微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如何让notifyDatasetChanged()与ListAdapter一起使用?

现在我使用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;
    }   
}
最佳答案
创建自定义适配器的实例,以便您可以随意使用它…

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,listscore);
    listViewscore.setAdapter(adapter);
    adapter.notifyDatasetChanged(); 
}
}

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

adapter.notifyDatasetChanged(); 

原文地址:https://www.jb51.cc/android/430797.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐