向下滚动时如何加载更多ListView项目?

时间:2018-06-27 14:19:03

标签: java android json listview

“我的应用”使用HTTP连接(PHP)从数据库获取值,并以ListView显示它们。我的问题是,该应用只能显示8-10个值(ListView个项目)。该应用程序崩溃,包含11个或更多项目。 ListView无法从数据库中加载无限值是正常的。

我认为解决方案是,仅显示10个项目,并滚动到ListView的末尾,弹出ProgressDialog并显示10个项目。

我该怎么做?

这是我已经拥有的代码: (是一个片段)

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.list, container, false);

    listView1 = (ListView) view.findViewById(R.id.listview);
    listView1.setOnItemClickListener(this);
    getURLs();



    return view;
}



private void getImages(){
    class GetImages extends AsyncTask<Void,Void,Void>{
        ProgressDialog loading;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(getActivity(),"Loading...","Please wait...",false,false);
        }

        @Override
        protected void onPostExecute(Void v) {
            super.onPostExecute(v);
            loading.dismiss();
            //Toast.makeText(ImageListView.this,"Success",Toast.LENGTH_LONG).show();
            customList = new CustomList(getActivity(),GetAllData.value1,GetAllData.value2,GetAllData.value3);
            listView1.setAdapter(customList);
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                getAllData.getAllImages();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    GetImages getImages = new GetImages();
    getImages.execute();
}

public void getURLs() {
    class GetURLs extends AsyncTask<String,Void,String>{
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(getActivity(),"Loading...","Please Wait...",true,true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            getAllData = new GetAllData(s);
            getImages();
        }

        @Override
        protected String doInBackground(String... strings) {
            BufferedReader bufferedReader = null;
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                StringBuilder sb = new StringBuilder();

                bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String json;
                while((json = bufferedReader.readLine())!= null){
                    sb.append(json+"\n");
                }

                return sb.toString().trim();

            }catch(Exception e){
                return null;
            }
        }
    }
    GetURLs gu = new GetURLs();
    gu.execute(GET_IMAGE_URL);
}

CustomListGetAllData不是必需的,因为CustomListListView提供了Details,而GetAllData从PHP脚本获取了值。 / p>

0 个答案:

没有答案