4

Android AsyncTask Incorrectly Built - codesd.com

 2 years ago
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

advertisements

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 ^^


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK