“我的应用”使用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);
}
类CustomList
和GetAllData
不是必需的,因为CustomList
为ListView
提供了Details,而GetAllData
从PHP脚本获取了值。 / p>