这里我试图从数据库中获取值并将它们列在ListView中,但在20个项目之后我想在用户滚动到ListView结束时向ListView添加更多项目,但是当用户滚动到结尾时ListView被完全删除,并添加新项目。但是我需要在上一个List本身的末尾添加新项目......
这是我的Android代码:
这将在onCreate()
执行 public void getLocalJobs() {
empty.setVisibility(TextView.GONE);
String url = ConfigCuboid.GET_JOBS_LOCAL;
String url1 = Tags;
String URL = url + url1;
StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSONLocal(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@TargetApi(Build.VERSION_CODES.M)
private void showJSONLocal(String response) {
ParseJSONLocal pj = new ParseJSONLocal(response);
pj.parseJSONLocal();
CustomListLocal c1 = new CustomListLocal(this, ParseJSONLocal.job_local_id, ParseJSONLocal.user_names, ParseJSONOnline.job_typess, ParseJSONLocal.job_titles, ParseJSONLocal.job_works,
ParseJSONLocal.job_dates, ParseJSONLocal.job_months, ParseJSONLocal.job_years,
ParseJSONLocal.job_times, ParseJSONLocal.job_periods, ParseJSONLocal.job_areas, ParseJSONLocal.job_rates,
ParseJSONLocal.user_ids, ParseJSONLocal.job_detailss, ParseJSONLocal.image, ParseJSONLocal.regtoken
, ParseJSONLocal.verify);
listViewWork.setAdapter(c1);
Toast.makeText(getApplicationContext(),"ListNo=="+CustomListLocal.LastLocalJobId,Toast.LENGTH_SHORT).show();
if (c1.isEmpty()) {
empty.setVisibility(TextView.VISIBLE);
pb.setVisibility(ProgressBar.GONE);
}
listViewWork.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int poss, long id) {
Intent is = new Intent(getApplicationContext(), WorkProfileLocal.class);
is.putExtra("Positions", poss);
startActivity(is);
}
});
listViewWork.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view,
int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//Algorithm to check if the last item is visible or not
final int lastItem = firstVisibleItem + visibleItemCount;
if (lastItem == totalItemCount) {
// you have reached end of list, load more data
// Toast.makeText(getApplicationContext(),"ListIsOver",Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"ListNo=="+CustomListLocal.LastLocalJobId,Toast.LENGTH_SHORT).show();
LoadLocalList();
}
}
});
}
在ListView结束时执行此方法:
public void LoadLocalList(){
empty.setVisibility(TextView.GONE);
String url = ConfigCuboid.GET_LOADED_JOBS_LOCAL;
String urll1="last_local_job_id=";
String urll11 = CustomListLocal.LastLocalJobId;
String urll="&job_title[]=";
String url1 = Tags;
String URL = url + urll1+urll11+urll+url1;
StringRequest stringRequest = new StringRequest(URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSONLocall(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@TargetApi(Build.VERSION_CODES.M)
private void showJSONLocall(String response) {
ParseJSONLocal pj = new ParseJSONLocal(response);
pj.parseJSONLocal();
CustomListLocal c2 = new CustomListLocal(this, ParseJSONLocal.job_local_id, ParseJSONLocal.user_names, ParseJSONOnline.job_typess, ParseJSONLocal.job_titles, ParseJSONLocal.job_works,
ParseJSONLocal.job_dates, ParseJSONLocal.job_months, ParseJSONLocal.job_years,
ParseJSONLocal.job_times, ParseJSONLocal.job_periods, ParseJSONLocal.job_areas, ParseJSONLocal.job_rates,
ParseJSONLocal.user_ids, ParseJSONLocal.job_detailss, ParseJSONLocal.image, ParseJSONLocal.regtoken
, ParseJSONLocal.verify);
listViewWork.setAdapter(c2);
Toast.makeText(getApplicationContext(),"ListNo=="+CustomListLocal.LastLocalJobId,Toast.LENGTH_SHORT).show();
if (c2.isEmpty()) {
empty.setVisibility(TextView.VISIBLE);
pb.setVisibility(ProgressBar.GONE);
}
listViewWork.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int poss, long id) {
Intent is = new Intent(getApplicationContext(), WorkProfileLocal.class);
is.putExtra("Positions", poss);
startActivity(is);
}
});
}
答案 0 :(得分:3)
此行重置整个列表
listViewWork.setAdapter(c2);
您可以直接添加到ArrayAdapter的任何实例,并且一次只能在ListView中有一个适配器。
换句话说,在onCreate中设置一个适配器,然后在其他任何地方添加到该适配器。不要再次调用setAdapter,并且当您调用adapter.add时,您将保留所有数据并且ListView将更新
我还建议您减少重复的代码。还不清楚为什么有两个单独的URL正在加载数据