我试图从sqlite显示listview。在Listview的末尾,我想从sqlite加载下一个10个数据,这个过程将继续,直到数据在SQLite中结束。但问题是第一次成功加载数据,当我滚动第三次页面/数据没有加载时,在LocalListPageIndex = 2之后,进度条继续运行。
这是我的ListView滚动代码。
listView.removeFooterView(progressBar);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, final int totalItemCount) {
lastInScreen = firstVisibleItem + visibleItemCount;
if (X != firstVisibleItem) {
if (LocalListPageIndex <= 5) {
Toast.makeText(getApplicationContext(), "Your Last Item." + lastInScreen, Toast.LENGTH_SHORT).show();
if (lastInScreen == totalItemCount) {
listView.addFooterView(progressBar);
// Execute some code after 15 seconds have passed
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
LocalListPageIndex += 1;
int OFFSET_SCROLL = 10
List<All_Post> allDesc = dbhelper.getAllDescriptions(OFFSET_SCROLL);
for (All_Post all_Post : allDesc) {
descArray.add(all_Post);
}
if (adapter != null) {
adapter.notifyDataSetChanged();
// adapter = new AllPostAdapter(getApplicationContext(), R.layout.allpostlist, descArray);
listView.setAdapter(adapter);
//listView.invalidateViews();
listView.setSelection(totalItemCount);
}
}
}, 15000);
}
} if (LocalListPageIndex == 5) {
Log.e("hide footer", "footer hide");
listView.removeFooterView(progressBar);
}
}
X = firstVisibleItem;
}
});
答案 0 :(得分:0)
使用无限加载更多listview加载数据,请检查此网址https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews
答案 1 :(得分:0)
您可以尝试使用RecyclerView: RecyclerView小部件是ListView的更高级和灵活的版本。此窗口小部件是一个容器,用于显示可以通过维护有限数量的视图而非常有效地滚动的大型数据集。如果数据集合的元素在运行时根据用户操作或网络事件而发生更改,请使用RecyclerViewwidget。
布局管理器将项目视图放在RecyclerView内部,并确定何时重用用户不再可见的项目视图。要重用(或回收)视图,布局管理器可能会要求适配器使用与数据集不同的元素替换视图的内容。以这种方式回收视图可以避免创建不必要的视图或执行昂贵的查询查询(查找)来提高性能 例如:
将此视图添加到您的布局:
public class MyActivity extends Activity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
// specify an adapter (see also next example)
// Set your myDataSet with the url of your images.
mAdapter = new MyAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
}
...
}
将>> RecyclerView小部件添加到布局后,获取对象的句柄,将其连接到布局管理器,并附加适合要显示的数据的适配器:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private MySQLClass mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public ViewHolder(TextView v) {
super(v);
textView = v;
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(MySQLClass myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_image_view, parent, false);
// set the view's size, margins, paddings and layout parameters
...
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
textView.setText(mDataset.get(i));
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
创建适配器以管理回收站视图:
CardDetails Top_Exixts_Card = new CardDetails();
DataSet Top_up_Dataset = new DataSet();
Top_Exixts_Card.CardId = Convert.ToInt32(Top_up_Dataset.Tables[0].Rows[0][0]);
Top_Exixts_Card.AccountNo = Top_up_Dataset.Tables[0].Rows[0][1] + "";
来自:
的信息http://developer.android.com/training/material/lists-cards.html
答案 2 :(得分:0)
当您从数据库中检索数据时,最好使用loaders。加载器将根据您的查询加载所有细节,并以Cursor的形式提供给您。现在您可以简单地给出该游标到CursorAdapter(如果您正在使用Cursor适配器)或者您可以将数据从游标提取到列表并将该列表提供给普通适配器。