我试图在我的应用中实现此功能,我知道之前已经问过这个问题但是我没有得到它。此刻,我按照JSON请求检索所有项目。
myTicketsFragment.java
private class PrefetchData extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Downloading..");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpReader httpReader = new HttpReader();
httpReader
.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
@Override
public void resultReady(String result) {
JsonHelper jsonHelper = new JsonHelper();
tickets = jsonHelper.getTickets(result);
TicketAdapter ticketAdapter = new TicketAdapter(
getActivity(), tickets);
final ListView listViewTickets = (ListView) rootView
.findViewById(R.id.listViewTickets);
listViewTickets.setAdapter(ticketAdapter);
listViewTickets
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(
AdapterView<?> parentView,
View childView, int position,
long id) {
if (isNetworkAvailable()) {
Bundle bundle = new Bundle();
bundle.putInt("ticketId",
tickets.get(position)
.getId());
Intent ticketDetail = new Intent(
getActivity(),
TicketActivity.class);
ticketDetail.putExtras(bundle);
startActivity(ticketDetail);
} else {
showNoInternetDialog();
}
}
});
listViewTickets.setEmptyView(rootView
.findViewById(R.id.empty));
dialog.dismiss();
}
});
httpReader
.execute("http://aa.aaaa.aa:8882/desk/API/v1/json.php?auth=platformt&a=getTickets&uauth="
+ authCode + "&uid=" + uId + "&mode=mytickets");
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
我想我需要知道显示哪个最后一个项目并检索新项目..我只是没有真正掌握这项技术而且我找不到一个好的例子。
如果有人能引导我走向正确的方向,那就太棒了!
答案 0 :(得分:1)
我建议您将ListView
移出AsyncTask
。如果你这样做,你可以用这种方式实现延迟加载(这是一个基本的想法):
public class MyActivity extends Activity {
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = findViewById(R.id.listView);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
boolean loadMore =
firstVisibleItem + visibleItemCount >= totalItemCount;
if (loadMore) {
AsyncTask<Integer, Void, JSONObject> asyncTask = new AsyncTask<Integer, Void, JSONObject>() {
@Override
protected void onPreExecute() {
//do stuff, show loading image
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
myAdapter.data = jsonObject;
myAdapter.notifyDataSetChanged();
}
@Override
protected JSONObject doInBackground(Integer... params) {
int startValue = params[0];
//make a request with pointing offset = startValue
return new JSONObject();
}
};
int startValue = totalItemCount;
asyncTask.execute(startValue);
}
}
});
}
}
因此,当您看到ListView
的最后一项时,只需下载其他数据并将其推入适配器。