0

在我正在构建的应用程序中,我必须从数据库中加载一些数据。
当用户从警报对话框中选择一个项目时,我会加载数据。
我使用一个AsyncTask类连接到数据库。这是代码:

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



     }/**/

我这样称呼它

GetTask cat = new GetTask();
cat.execute();

显示了进度对话框,但它并没有消失,ListView 也没有被填充。
我做错了什么?

这是 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());
                }
    }

}
4

2 回答 2

0

实际上,在将适配器设置为列表调用后,您必须先告诉进度条消失您有两种方式

@Override
protected void onPostExecute(Void result) {
     .....
    pg.dismiss();

或者您可以使用处理程序定义一个处理程序,例如

Handler handler = new Handler (){
 @Override
protected void handleMessage(int what){
    if(pg.isShowing()){
      pg.dismiss();
    }
}
}

并在 onPostExecute 方法调用

handler.sendEmptyMessage(0);

这会有所帮助!

于 2013-05-27T12:54:16.877 回答
0

尝试像这样lva.notifyDataSetChanged()在里面添加onPostExecute

        lista = (ListView)findViewById(R.id.lista_prodotti);
        ListViewAdapter lva = new ListViewAdapter(nomi , foto , prezzi , _id , descr , context);
        lista.setAdapter(lva);
        lva.notifyDataSetChanged();

希望对你有帮助,祝你好运^^

于 2013-05-27T12:41:08.320 回答