gpt4 book ai didi

java - 添加新项目时Listview跳转到顶部

转载 作者:行者123 更新时间:2023-12-01 09:12:18 26 4
gpt4 key购买 nike

我正在制作具有滚动监听器的应用程序,该监听器可以从多个 URL 添加数据但是当我滚动列表跳转到第一个位置并加载 URL 时,我知道每次加载新项目时加载新 URL 任务时都会执行同步任务中的适配器,但我不知道如何修复它

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;
}
});

}

最佳答案

从 onPostExecute 中删除这些行

newsadapter adapter = new newsadapter(getApplicationContext(), R.layout.row, result);
lvnews.setAdapter(adapter);

执行 doInBackground 后,您正在设置适配器 eveytime。您只需要做一次。你可以在onCreate中完成

The basic methods used in an android AsyncTask class are defined below :

doInBackground() : This method contains the code which needs to be executed in background. In this method we can send results multiple times to the UI thread by publishProgress() method. To notify that the background processing has been completed we just need to use the return statements

onPreExecute() : This method contains the code which is executed before the background processing starts

onPostExecute() : This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method

onProgressUpdate() : This method receives progress updates from doInBackground method, which is published via publishProgress method, and this method can use this progress update to update the UI thread

请引用此link了解如何异步任务

关于java - 添加新项目时Listview跳转到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40843805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com