我正在创建具有滚动侦听器的应用程序,该侦听器从多个URL添加数据 但是当我滚动列表跳转到第一个位置而不是加载URL时我知道每次加载新的URL任务时我的同步任务中的适配器都会被执行但我不知道如何修复它< / p>
public class jsontask extends AsyncTask<String, String, List<newsmodel>> {
@Override
protected List<newsmodel> doInBackground(String... params) {
BufferedReader reader = null;
HttpURLConnection connection = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
if (connectiondetector.isConnected()) {
connection.addRequestProperty("Cache-Control", "max-age=0");
} else {
moviemodelList.addAll((List<newsmodel>) cacheThis.readObject(
technology.this, fileName));
}
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finaljson = buffer.toString();
JSONObject parentobject = new JSONObject(finaljson);
JSONArray parentarray = parentobject.getJSONArray("articles");
for (int i = 0; i < parentarray.length(); i++) {
JSONObject finalobject = parentarray.getJSONObject(i);
newsmodel newsmodel = new newsmodel();
newsmodel.setAuthor(finalobject.getString("author"));
if (finalobject.isNull("author")) {
}
newsmodel.setDescription(finalobject.getString("description"));
newsmodel.setTitle(finalobject.getString("title"));
newsmodel.setImage(finalobject.getString("urlToImage"));
newsmodel.setUrl(finalobject.getString("url"));
newsmodel.setPublishedAt(finalobject.getString("publishedAt"));
cacheThis.writeObject(technology.this, fileName, moviemodelList);
moviemodelList.add(newsmodel);
}
return moviemodelList;
} catch (MalformedURLException e) {
e.printStackTrace();
return moviemodelList;
} catch (IOException e) {
e.printStackTrace();
return moviemodelList;
} catch (JSONException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (connection != null) {
}
try {
if (reader != null) {
reader.read();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return moviemodelList;
}
@Override
protected void onPostExecute(List<newsmodel> result) {
super.onPostExecute(result);
newsadapter adapter = new newsadapter(getApplicationContext(), R.layout.row, result);
lvnews.setAdapter(adapter);
}
}
public class newsadapter extends ArrayAdapter {
private List<newsmodel> moviemodelList;
private int resource;
private LayoutInflater inflater;
public newsadapter(Context context, int resource, List<newsmodel> objects) {
super(context, resource, objects);
moviemodelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}`
这就是我执行同步任务的方式
protected void onStart() {
super.onStart();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
moviemodelList.clear();
new jsontask().execute("https://newsapi.org/v1/articles?source=engadget&sortBy=top&apiKey=ade8f00a634b4825a028837ec107afae");
lvnews.setOnScrollListener(new EndlessScrollListener() {
@Override
public boolean onLoadMore(int page, int totalItemsCount) {
new jsontask().execute("url1");
new jsontask().execute("url2");
new jsontask().execute("url3");
new jsontask().execute("url4");
new jsontask().execute("url5");
new jsontask().execute("url6");
return false;
}
});
}
答案 0 :(得分:1)
从onPostExecute中删除这些行
newsadapter adapter = new newsadapter(getApplicationContext(), R.layout.row, result);
lvnews.setAdapter(adapter);
执行doInBackground后,您正在设置适配器eveytime。你只需要做一次。你可以在onCreate
中完成android AsyncTask类中使用的基本方法定义如下 :
doInBackground():此方法包含需要在后台执行的代码。在这种方法中,我们可以发送多个结果 通过publishProgress()方法到UI线程的次数。通知那个 后台处理已经完成我们只需要使用了 退货声明
onPreExecute():此方法包含在后台处理开始之前执行的代码
onPostExecute():在doInBackground方法完成处理后调用此方法。 doInBackground的结果传递给 这个方法
onProgressUpdate():此方法从doInBackground方法接收进度更新,该方法通过publishProgress方法发布, 并且此方法可以使用此进度更新来更新UI线程
请参阅此link以了解Async任务
的方式答案 1 :(得分:0)
获得第一个url响应后,调用第二个 new jsontask()。execute(“url2”); 然后调用第三个,第四个等等。
制作一个公共代码,一次又一次地重复使用它。
试试此链接:http://www.devexchanges.info/2015/03/android-listview-dynamically-load-more.html