UMA*_*MAR 423 android refresh android-listview
ListView添加/删除动态数据后如何刷新Android ?
Chr*_*Orr 522
调用notifyDataSetChanged()您的Adapter对象,一旦你在该适配器修改的数据.
notifyDataSetChanged()可以在此Google I/O视频中查看有关如何/何时通话的其他详细信息.
Bob*_*obs 203
你也可以使用这个:
myListView.invalidateViews();
Run Code Online (Sandbox Code Playgroud)
hcp*_*cpl 151
invalidate(),invalidateViews(),requestLayout(),...这个问题的答案.正确的做法(幸运的是,也标记为正确答案)是调用notifyDataSetChanged()适配器.
如果调用notifyDataSetChanged()不起作用,则所有布局方法都无济于事.相信我ListView已正确更新.如果找不到差异,则需要检查适配器中的数据来自何处.
如果这只是一个你在内存中保存的集合,请在调用之前检查你是否实际删除了项目或将项目添加到集合中notifyDataSetChanged().
如果您正在使用数据库或服务后端,则必须在调用之前调用该方法再次检索信息(或操作内存数据)notifyDataSetChanged().
事情是这notifyDataSetChanged只有在数据集发生变化时才有效.如果您没有找到更改,那么这就是您要查看的地方.如果需要调试.
我确实发现使用适配器来管理集合,就像BaseAdapter更好用.像ArrayAdapter这样的一些适配器已经管理了自己的集合,这使得更难获得适当的集合以进行更新.在大多数情况下,它实际上只是一个不必要的额外难度层.
确实,必须从UI线程调用它.其他答案有关于如何实现这一点的例子.但是,只有在从UI线程外部处理此信息时才需要这样做.这是来自服务或非UI线程.在简单的情况下,您将从按钮单击或其他活动/片段更新数据.所以仍然在UI线程内.无需始终弹出runOnUiTrhead.
可以在https://github.com/hanscappelle/so-2250770.git找到.只需在Android Studio(gradle)中克隆并打开项目即可.该项目的MainAcitivity构建了一个ListView包含所有随机数据的项目.可以使用操作菜单刷新此列表.
我为此示例ModelObject创建的适配器实现公开了数据集合
public class MyListAdapter extends BaseAdapter {
/**
* this is our own collection of data, can be anything we
* want it to be as long as we get the abstract methods
* implemented using this data and work on this data
* (see getter) you should be fine
*/
private List<ModelObject> mData;
/**
* our ctor for this adapter, we'll accept all the things
* we need here
*
* @param mData
*/
public MyListAdapter(final Context context, final List<ModelObject> mData) {
this.mData = mData;
this.mContext = context;
}
public List<ModelObject> getData() {
return mData;
}
// implement all abstract methods here
}
Run Code Online (Sandbox Code Playgroud)
MainActivity中的代码
public class MainActivity extends Activity {
private MyListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.list);
// create some dummy data here
List<ModelObject> objects = getRandomData();
// and put it into an adapter for the list
mAdapter = new MyListAdapter(this, objects);
list.setAdapter(mAdapter);
// mAdapter is available in the helper methods below and the
// data will be updated based on action menu interactions
// you could also keep the reference to the android ListView
// object instead and use the {@link ListView#getAdapter()}
// method instead. However you would have to cast that adapter
// to your own instance every time
}
/**
* helper to show what happens when all data is new
*/
private void reloadAllData(){
// get new modified random data
List<ModelObject> objects = getRandomData();
// update data in our adapter
mAdapter.getData().clear();
mAdapter.getData().addAll(objects);
// fire the event
mAdapter.notifyDataSetChanged();
}
/**
* helper to show how only changing properties of data
* elements also works
*/
private void scrambleChecked(){
Random random = new Random();
// update data in our adapter, iterate all objects and
// resetting the checked option
for( ModelObject mo : mAdapter.getData()) {
mo.setChecked(random.nextBoolean());
}
// fire the event
mAdapter.notifyDataSetChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
关于listViews强大功能的另一篇好文章可以在这里找到:http://www.vogella.com/articles/AndroidListView/article.html
Mar*_*ujo 42
随时调用runnable:
runOnUiThread(run);
Run Code Online (Sandbox Code Playgroud)
OnCreate(),你设置你的runnable线程:
run = new Runnable() {
public void run() {
//reload content
arraylist.clear();
arraylist.addAll(db.readAll());
adapter.notifyDataSetChanged();
listview.invalidateViews();
listview.refreshDrawableState();
}
};
Run Code Online (Sandbox Code Playgroud)
小智 11
我在listview的动态刷新方面遇到了一些问题.
在适配器上调用notifyDataSetChanged().
有关如何/何时调用notifyDataSetChanged()的一些其他细节可以在此Google I/O视频中查看.
在我的情况下,notifyDataSetChanged()无法正常工作[我从另一个类调用了notifyDataSetChanged].就在我在正在运行的Activity(Thread)中编辑ListView的情况下.该视频感谢克里斯托弗给出了最后的暗示.
在我使用的第二堂课
Runnable run = new Runnable(){
public void run(){
contactsActivity.update();
}
};
contactsActivity.runOnUiThread(run);
Run Code Online (Sandbox Code Playgroud)
从我的Activity访问update().此更新包括
myAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)
告诉适配器刷新视图.据我所知,工作得很好.
如果您对ListView Refreshment不满意,可以查看这个片段,这是用于从DB加载listView,实际上你要做的只是重新加载ListView,在你执行任何CRUD操作后它不是最好的方法代码,但它会刷新ListView,如你所愿..
它对我有用....如果你找到更好的解决方案,请分享......
.......
......
do your CRUD Operations..
......
.....
DBAdapter.open();
DBAdapter.insert_into_SingleList();
// Bring that DB_results and add it to list as its contents....
ls2.setAdapter(new ArrayAdapter(DynTABSample.this,
android.R.layout.simple_list_item_1, DBAdapter.DB_ListView));
DBAdapter.close();
| 归档时间: |
|
| 查看次数: |
475498 次 |
| 最近记录: |