Android的ListView追加更多的项目notifyDataSetChanged()不工作
问题描述:
我试图追加更多的项目到Android项目的列表视图。这是我的,我使用的代码:Android的ListView追加更多的项目notifyDataSetChanged()不工作
public class NewsFeedActivity extends ListActivity implements FetchDataListener{
boolean loadingMore = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
SharedPreferences shared = getSharedPreferences(MainActivity.class.getSimpleName(),Context.MODE_PRIVATE);
@SuppressWarnings("unused")
String storedUID = (shared.getString(UID, ""));
final ListView list = (ListView)findViewById(android.R.id.list);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setColorSchemeResources(R.color.black, R.color.white, R.color.black, R.color.white);
swipeLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
loadingMore = true;
initView();
}
}, 1000);
}
});
list.setOnScrollListener(new OnScrollListener(){
private int currentFirstVisibleItem;
private int currentVisibleItemCount;
private int totalItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.isScrollCompleted();
}
private void isScrollCompleted() {
// TODO Auto-generated method stub
int lastInScreen = currentFirstVisibleItem + currentVisibleItemCount;
if((lastInScreen == totalItem) && !(loadingMore)){
//Toast.makeText(getApplicationContext(), "Loading more...", Toast.LENGTH_LONG).show();
loadingMore = true;
initView();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItem = totalItemCount;
}
});
initView();
}
,这里是我用来调用列表中的其他两个功能:
private void initView() {
// show progress dialog
if(!loadingMore == true){
dialog = ProgressDialog.show(this, "", "Loading...");
}
String url = SERVER_URL+getBlackCards;
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
if(loadingMore == true){
adapter.notifyDataSetChanged();
}
loadingMore = false;
}
当前的行为本质上只是一个刷新所有项目被替换,我滚动回列表的顶部。在做了一些四处寻找之后,我发现了很多使用notifyDataSetChanged()追加项目的例子,但它似乎对我没有任何影响。
答
当前的行为本质上只是其中的所有项目 被替换的刷新,我滚动回升到列表顶部
因为在这里:
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
setListAdapter(adapter);
每次创建adapter
类的新对象,而不是在ListView的当前适配器中添加新项目。
做得一样:
1.创建ApplicationAdapter
对象在类级别为:
ApplicationAdapter adapter;
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
if(adapter==null){
adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
}else{
// update current adapter
}
loadingMore = false;
}
2.在ApplicationAdapter
类创建addAllItems
方法:
public void addAllItems(List<Application> data){
this.data.addAll(data);
this.notifyDataSetChanged();
}
3.现在叫addAllItems
在onFetchComplete
方法的其他部分:
adapter.addAllItems(data);
+0
绝对正确!非常感谢你 :) – jampez77 2015-03-03 22:02:32
你没有追加新的数据适配器,你只是创建新的数据新adpater。 – 2015-03-03 09:57:58