【问题标题】:loading large database in listview在列表视图中加载大型数据库
【发布时间】:2013-07-27 05:51:30
【问题描述】:

我有一个包含大约 12000 条记录的数据库,我想将它们加载到 ListView 中。我正在使用 BaseAdapter。当我在 ListView 中加载项目时,它需要很长时间。
有什么办法可以减少这个时间,例如我看到一个应用程序只加载有限数量的项目,直到滚动条到达 ListView 的末尾,然后它再次加载更多项目。

【问题讨论】:

标签: android database listview large-data


【解决方案1】:

您应该在列表中设置 onScrollListener 并跟踪项目的可见性和偏移量。我在这里给你举个例子:

// Adapter for the custom list
adapter = new Adapter(this, activityList);
setListAdapter(adapter);
registerForContextMenu(getListView());
getListView().setOnScrollListener(new OnScrollListener(){
    public void onScroll(AbsListView lw, final int firstVisibleItem,
    final int visibleItemCount, final int totalItemCount) {

        switch(lw.getId()) {
            case android.R.id.list:     

                // Make your calculation stuff here. You have all your
                // needed info from the parameters of this function.

                // Sample calculation to determine if the last 
                // item is fully visible.
                final int lastItem = firstVisibleItem + visibleItemCount;
                if(lastItem == totalItemCount) {
                    // Last item is fully visible.
                    Log.i("a", "last item fully visible...");

                    try {
                        if(offset > 0){
                            int newLimit;
                            int oldOffset = offset;
                            if(offset >= limit){
                            newLimit = limit;
                            offset = offset - limit;
                        }
                        else{
                            newLimit = length;
                            offset = 0;
                        }
                        for (int i=0; i < newLimit; i++)
                        {
                            JSONObject item = jFeed.getJSONObject(i + length - oldOffset);
                            // Pulling items from the array

                            // Get list info
                            String sInfo = item.getString(TAG_INFO);
                            Log.i(MainActivity.class.getName(), "Info: " + sInfo);

                            // Populate the dynamic custom list
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put(KEY_INFO, sInfo);

                            activityList.add(map);
                        }
                        adapter.notifyDataSetChanged();

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
        }
    }

    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        if(scrollState == 0) 
            Log.i("a", "scrolling stopped...");
    }
});

【讨论】:

    【解决方案2】:

    这可能很有用,带有加载更多按钮的ListView

    http://www.androidhive.info/2012/03/android-listview-with-load-more-button/

    你也可以看看这里:

    How can I implement paging in listview in android?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2015-05-12
      相关资源
      最近更新 更多