gpt4 book ai didi

java - 将更多数据加载到 ListView - Android

转载 作者:行者123 更新时间:2023-12-01 23:45:22 25 4
gpt4 key购买 nike

我已经实现了一个 ListView ,其中通过异步任务加载数据,为了加载更多数据,我使用了此代码发现here (完全复制粘贴)

主要 Activity

class load_data extends AsyncTask<Integer, Void, Void>{

// This asynctask takes in a page number as argument and depending on that page number
// data is loaded and stored in various String[] arrays and their length is extended as new items are loaded

protected void onPostExecute(Void result) {

if(adapter == null){
adapter = new Listadapter(Main.this,String[],String[],String[]);
list.setAdapter(adapter);

}
else if(adapter != null){

adapter.notifyDataSetChanged();
}

}
}

此任务称为 new load_data().execute(1);

在此代码之后,我从上面的链接加载了更多数据 fragment 一切工作完美,没有语法错误,在我的例子中,数据在达到给定阈值(20)后加载,但是没有显示新数据。如何通知适配器已添加更多数据或数据已更改谢谢!.

编辑:加载更多类

class EndlessScrollListener implements OnScrollListener {

private int visibleThreshold = 5;
private int currentPage = 1;
private int previousTotal = 0;
private boolean loading = true;

public EndlessScrollListener() {
}

public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
currentPage++;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
// I load the next page of gigs using a background task,
// but you can call any function here.
new load_data().execute(currentPage);
adapter.notifyDataSetChanged();
loading = true;
}
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
}

list.setOnScrollListener(new EndlessScrollListener(20)); // 20 being the threshold

最佳答案

终于!对于那些遇到同样问题的人,我有解决方案。

我注意到adapter.notifyDataSetChanged();如果您使用 String[] 数组来存储显示在 ListView 行中的数据,则不起作用。

如果您使用 List<String> list = new ArrayList<String>();并将数据存储在 ListView 将使用新数据更新:)

关于java - 将更多数据加载到 ListView - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17143042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com