列表视图不notifydatasetchanged更新()调用 [英] listview not updating with notifydatasetchanged() call

查看:148
本文介绍了列表视图不notifydatasetchanged更新()调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code

listview =(ListView) findViewById(R.id.lv1);


    ArrayList<SClass> Monday = new ArrayList<SClass>();

    SClass s1=new SClass();
    s1.sName="samp";
    s1.salary=1000;
    Monday.add(s1);
    temp=Monday;
    adapter = new CustomAdap(this, temp);
    listview.setAdapter(adapter);

以上code工作fine.But当我改变我的code本

The above code works fine.But when i change my code to this

    listview =(ListView) findViewById(R.id.lv1);


    adapter = new CustomAdap(this, temp);

    SClass s1=new SClass();
    s1.sName="samp";
    s1.salary=1000;
    Monday.add(s1);
    temp=Monday;

    listview.setAdapter(adapter);
    adapter.notifyDataSetChanged();

列表视图不显示anything.what问题?

Listview does not show anything.what is the problem?

推荐答案

它看起来像你改变了集合,您初始化适配器。我会改变你的code以这种方式:

It looks like you're changing the collection that you initialized adapter with. I would change your code in this way:

// initial setup
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
adapter = new CustomAdap(this, Monday);
listview.setAdapter(adapter);

// change your model Monday here, since it is what the adapter is observing
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);

// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();

请注意,如果您的CustomAdap是ArrayAdapter的一个子类,你也可以做

Note that if your CustomAdap was a subclass of ArrayAdapter, you could also have done

// change your array adapter here
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
adapter.add(s1);

// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();

编辑:据我了解越多,你现在想做感谢您的意见是什么。你可能想与你不同的ArrayList则适配器替换其内容。我会让你CustomAdap是ArrayAdapter的一个子类。

I understand more what you want to do now thanks to your comment. You'll probably want to have the adapter replace its contents with that your different ArrayLists then. I would make your CustomAdap be a subclass of ArrayAdapter.

然后就可以利用这种方式:

Then you can utilize it this way:

// replace the array adapters contents with the ArrayList corresponding to the day
adapter.clear();
adapter.addAll(MONDAY);

// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();

这篇关于列表视图不notifydatasetchanged更新()调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆