gpt4 book ai didi

android - 在Android中执行两个asynctask实例

转载 作者:行者123 更新时间:2023-12-03 17:37:39 30 4
gpt4 key购买 nike

我正在执行两个asyntask实例,其中一个在onCreate中,另一个在onScroll处理程序中。该应用程序旨在通过Web服务下载X信息量,并在滚动到底部时继续加载信息。信息加载良好,但有时会保持加载状态,并且应用程序崩溃。

这是doInBackground代码:

protected String doInBackground(String... args) {
// Declaro los parametros a enviar a la BD
cargando=true;
List<NameValuePair> params = new ArrayList<NameValuePair>();
//parametro enviado (ciudad)
params.add(new BasicNameValuePair("ciudad", "5"));
params.add(new BasicNameValuePair("inicio", Integer.toString(inicio)));
params.add(new BasicNameValuePair("limite", Integer.toString(delante)));

JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

String def="default";

// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());

try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
if (primeraEjecucion){
String MAX = json.getString(TAG_MESSAGE);
totalConsultas=Integer.parseInt(MAX);
}

// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);

String date = c.getString(TAG_DATE);
if (date.equals(def)){
//tratar la fecha
}
else{
boolean mostrarEvento=compararFechas(date);
if (mostrarEvento){
Evento e=new Evento();
String id = c.getString(TAG_ID);
String name = c.getString(TAG_POST_TITLE);
String post = c.getString(TAG_POST);
String image = c.getString(TAG_IMAGE);
String place = c.getString(TAG_PLACE);
String category = c.getString(TAG_CATEGORY);

e.setID(Integer.parseInt(id));
e.setTitulo(name);
e.setPost(post);
e.setImagen(image);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date fecha1 = sdf.parse(date , new ParsePosition(0));
e.setFecha(fecha1);
e.setLugar(place);
e.setCategoria(category);
eventos.add(e);
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e1) {
e1.printStackTrace();
}
return null;
}

在postExecute上,我正在更新适配器。我已经打印了日志,有时似乎,该应用尝试在不触摸屏幕的情况下多次执行onScroll上的asynctask。我已经用 bool(boolean) 值保护了asynctask的执行。

日志:
07-22 16:53:06.662: E/AndroidRuntime(391): FATAL EXCEPTION: main
07-22 16:53:06.662: E/AndroidRuntime(391): java.lang.IllegalStateException: The content of the adapter
has changed but ListView did not receive a notification.
Make sure the content of your adapter is not modified
from a background thread, but only from the UI thread.
[in ListView(2131099648, class android.widget.ListView)
with Adapter(class android.widget.HeaderViewListAdapter)]
07-22 16:53:06.662: E/AndroidRuntime(391): at android.widget.ListView.layoutChildren(ListView.java:1510)
07-22 16:53:06.662: E/AndroidRuntime(391): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
07-22 16:53:06.662: E/AndroidRuntime(391): at android.view.View.layout(View.java:7175)
07-22 16:53:06.662: E/AndroidRuntime(391): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)

onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_products);
new LoadAllProducts().execute();
lv = (ListView) findViewById(R.id.lista);
lv.setOnScrollListener(new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int elementoFinal=firstVisibleItem + visibleItemCount;
if (inicio<=totalConsultas){
if (elementoFinal==totalItemCount && !primeraEjecucion && !cargando){
new LoadAllProducts().execute();
}
}else{
if (control){
lv.removeFooterView(footerView);
control=false;
}

}
}
});
}

最佳答案

每次更改其数据时,请调用adapter.notifyDataSetChanged()

关于android - 在Android中执行两个asynctask实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17793420/

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