我試圖追加更多的項目到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()追加項目的例子,但它似乎對我沒有任何影響。
你沒有追加新的數據適配器,你只是創建新的數據新adpater。 – 2015-03-03 09:57:58