

Android AsyncTask Incorrectly Built - codesd.com
source link: https://www.codesd.com/item/android-asynctask-incorrectly-built.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Android AsyncTask Incorrectly Built
In the application that I'm building, I have to load some datas from a database.
I load the datas when the user select an item from an alert dialog.
I use an AsyncTask
class to connect to the database. Here's the code:
public class GetTask extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... arg0) {
getProdotti();
return null;
}
@Override
protected void onPostExecute(Void result) {
pg.dismiss();
for(int w=0;w<all_id.length;w++){
_id.add(all_id[w]);
nomi.add(all_names[w]);
foto.add(all_images[w]);
prezzi.add(all_prices[w]);
descr.add(all_desc[w]);
}
lista = (ListView)findViewById(R.id.lista_prodotti);
ListViewAdapter lva = new ListViewAdapter(nomi , foto , prezzi , _id , descr , context);
lista.setAdapter(lva);
}
protected void onPreExecute(Void result) {
pg = ProgressDialog.show(context, "", "Caricamento in corso...");
}
@Override
protected void onProgressUpdate(Void... values) {
}
}/**/
And i call it so
GetTask cat = new GetTask();
cat.execute();
The progress dialog is shown, but it doesn't disappear and the ListView is not populated.
What i'm doing wrong?
Here's getProdotti():
private void getProdotti(){
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost(host_url);
datas = new ArrayList<NameValuePair>(1);
datas.add(new BasicNameValuePair("categoria",selected));
Log.d("INVIO",selected);
httppost.setEntity(new UrlEncodedFormEntity(datas));
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
}catch (Exception e){
Toast.makeText(Catalogo.this, "error"+e.toString(), Toast.LENGTH_LONG).show();
}
if(inputStream != null){
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
result = sb.toString();
Log.d("RESULT",result);
}catch(Exception e){
Log.e("TEST", "Errore nel convertire il risultato "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
all_id = new String[jArray.length()];
all_names = new String[jArray.length()];
all_prices = new String[jArray.length()];
all_images = new String[jArray.length()];
all_desc = new String[jArray.length()];
for(int i=0;i<jArray.length();i++){
JSONObject json_prod = jArray.getJSONObject(i);
Log.d("Debug",json_prod.getString("id_prodotto")+"--"+json_prod.getString("nome_prodotto"));
all_id[i]=json_prod.getString("id_prodotto");
all_names[i]=json_prod.getString("nome_prodotto");
all_prices[i]=json_prod.getString("prezzo");
all_images[i]=json_prod.getString("foto_prodotto");
all_desc[i]=json_prod.getString("descrizione");
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
Try adding lva.notifyDataSetChanged()
inside onPostExecute
like this :
lista = (ListView)findViewById(R.id.lista_prodotti);
ListViewAdapter lva = new ListViewAdapter(nomi , foto , prezzi , _id , descr , context);
lista.setAdapter(lva);
lva.notifyDataSetChanged();
Hope this helps, good luck ^^
Recommend
-
95
版权声明:本文为博主原创文章,未经博主允许不得转载 源码:github.com/AnliaLee 大家要是看到有错误的地方或者有啥好的建议,欢迎留言评论 前言 本章我们将结合之前几篇博客,来研究研究多线程知识综合应用程度很高的AsyncTask类(Andro
-
42
前言 AsyncTask 在Android开发中是一个经常用到的类,允许用户在工作线程上完成后台计算等任务,之后将结果同步UI线程,比起 Thread 和 Handler 模型使用起来方便一些。 AsyncTask 使用起来如此方便了,那么有什么需...
-
75
Android中的线程机制是非常重要的,在很多情况下为了使APP不卡顿,我们需要将很多事情放到子线程中执行,使主线程尽量没有耗时操作,否则会导致ANR.Android中的线程几乎完全采用了Java中的线程机制,那么创建、销毁、调度线程对线程的了解就很...
-
37
For years now, Android’s AsyncTask has been a staple tool for beginner and expert developers alike. If you’ve ever Googled atutorial for any sort of asynchronous logic in Android, chances are that the first few results suggest...
-
23
For the past decade, AysncTask has been one of the most widely used solutions for writing concurrent code in Android. However, it earned very controversial reputation. On the one hand, AsyncTask powered, and still powers,...
-
22
Thread 这一篇文章主要记录 AsyncTask 相关的知识点。 大部分内容来自官方文档,
-
19
Android AsyncTask is Deprecated: Here’s another wayIf you are into android development then I am pretty sure that you know about Android AsyncTask. The AsyncTask class helped us in executing some code in the backgrou...
-
9
Android中AsyncTask的执行过程 – Android开发中文站你的位置:Android开发中文站 > Android开发 >
-
9
Android: Run asynctask once per session advertisements OK, I have been researching this problem for a little while and I haven't...
-
10
Android Threads, Handlers and AsyncTaskJune 14, 2012 · 1 min · oopsmonk | Suggest Changes先看過
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK