【问题标题】:How to display unlimited list of data in Android ListView如何在 Android ListView 中显示无限的数据列表
【发布时间】:2014-05-14 06:00:39
【问题描述】:

如果这个问题已经得到解答,我真的很抱歉。 对于我所面临的问题,我没有找到任何合适的解决方案。

我有大量数据。

我想在列表中显示什么,这些数据包含 图片、网址、标题、类型和价格

项目列表可以多于10,000

所以如果我要实现Lazy Loading,我需要所有的网址和其余数据,以便我可以在运行时加载图像。

但是我如何加载包含网址、标题、类型和价格表的数据呢?

或者我可以通过延迟加载先加载 20 条记录。如果是那怎么办?

如果您有更好的方法来完成这件事,请提出建议。

提前致谢。

【问题讨论】:

标签: android android-listview android-asynctask lazy-loading


【解决方案1】:

您可以先加载 20 条记录,然后在滚动 ListView 时加载更多记录,方法是将 ScrollListener 用于您的 ListViewcheck this answer

【讨论】:

    【解决方案2】:

    我如何做到这一点是我使用 AsyncTask 在 ArrayList 中加载数据(假设 20 项),然后我使用 setAdapter 和在 ListAdapter 的 getView 方法中将它们设置为列表视图,因为它到达倒数第二项我 再次执行 AsyncTask 但这次不是 setAdapter 我使用的是 adapter.notifyDataSetChanged(); 它应该是怎样的: getView 方法应该是这样的:

    public View getView(int arg0, View arg1, ViewGroup arg2) 
    {
        //your data inflater or set text...
        if(arg0==yourList.size()-1)
           {
               new YourAsyncTask().execute());
           }
    }
    

    AsyncTask 应该是这样的

    //first initialize your list out of asynctask 
    ArrayList<String> yourList=new ArrayList<String>();
    
    class YourAsyncTask extends AsyncTask(Void,Void,Void)
     {
         protected Void doInBackground(String ... arg0) {
            // TODO Auto-generated method stub
                //get your data and add to arraylist
                 yourlist.add(data);
          }
    
        protected void onPostExecute(Void result) {
                 if(yourlist.size<20){
                       listview.setAdapter(yourAdapter);
                    }
                  else
                     {
                        yourAdapter.notifyDataSetChanged();
                     }
          }
    }       
    

    【讨论】:

      【解决方案3】:

      这完全取决于您如何构建应用程序。我更喜欢的一种方法是向服务器发出一个请求,该请求将以 json 格式返回您的 url、标题数据。

      {
          "dummy_info": "blah blah blah",
          "data": [
              {
                  "url": "http://www.google.com",
                  "title": "Sample",
                  "type": "Education",
                  "price": "50$"
              },
              {
                  "url": "http://www.google.com",
                  "title": "Sample",
                  "type": "Education",
                  "price": "50$"
              }
          ]
      }
      

      获得特定数据后,您可以使用 Android SwipeRefreshLayout 或延迟列表加载实现批量获取数据。

      【讨论】:

      • 这就是我正在实施的。首先我想获取数据然后加载图像但时间滚动。但是如果数据/列表太长,那么它将如何工作?
      猜你喜欢
      • 1970-01-01
      • 2010-10-02
      • 2016-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      相关资源
      最近更新 更多