Android空列表视图与互联网连接不佳

时间:2015-10-26 18:18:34

标签: android listview

我有一个listview正在填充来自服务器的数据。如果我使用wifi连接一切正常。

我有什么办法可以改进这段代码,等到数据从服务器满载时出现连接错误,如3G或连接不良?

有时listview会变空。

public class LoadAsync extends AsyncTask<String, Boolean, Boolean>{
    public ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ListEvents.this);
        pDialog.setMessage("Loading...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Boolean doInBackground(String... params) {
        // Creating volley request obj
        JsonArrayRequest eventReq = new JsonArrayRequest(url,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());
                        pDialog.dismiss();

                        // Parsing json
                        for (int i = 0; i < response.length(); i++) {
                            try {

                                JSONObject obj = response.getJSONObject(i);
                                Event event = new Event();
                                event.setImovel_id(obj.getString("imovel_id"));
                                event.setThumbnailUrl(obj.getString("arquivo"));
                                event.setNegocio(obj.getString("negocio"));
                                event.setImovel(obj.getString("imovel"));
                                event.setMobilia(obj.getString("mobilia"));
                                event.setGaragem(obj.getString("garagem"));
                                event.setPreco(obj.getString("preco"));


                                city = obj.getString("city").trim();
                                statee = obj.getString("state").trim();
                                checkNegocio = obj.getString("negocio").trim();
                                checkImovel = obj.getString("imovel").trim();
                                checkMobilia = obj.getString("mobilia").trim();
                                checkGaragem = obj.getString("garagem").trim();
                                checkPreco = obj.getString("preco").trim();

                                checkPreco = checkPreco.replace("R", "");
                                checkPreco = checkPreco.replaceAll("[$.,]", "");
                                int serverprice = Integer.parseInt(checkPreco);

                                String app_price = checkP.getText().toString();
                                app_price = app_price.replace("R", "");
                                app_price = app_price.replaceAll("[$.,]", "");
                                int i_price = Integer.parseInt(app_price);

                                if(estado.getText().toString().trim().equalsIgnoreCase(statee) && 
                                        cidade.getText().toString().trim().equalsIgnoreCase(city) && 
                                        checkN.getText().toString().trim().equalsIgnoreCase(checkNegocio)){

                                    if(/*checkI.getText().toString().equalsIgnoreCase(checkImovel) || 
                                            checkM.getText().toString().equalsIgnoreCase(checkMobilia) || 
                                            checkG.getText().toString().equalsIgnoreCase(checkGaragem) ||*/ 
                                            serverprice <= i_price){

                                        // adding event to events array
                                        eventList.add(event);                                           
                                    }


                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } if(eventList.size() > 0){
                            listView.setAdapter(adapter);
                            listView.setTextFilterEnabled(true);
                        }else{
                            noEvent.setText("Nothing found.");                              
                        }

                        // notifying list adapter about data changes
                        // so that it renders the list view with updated data
                        adapter.notifyDataSetChanged();
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        pDialog.dismiss();

                    }
                });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(eventReq);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                String imovelID = ((TextView) view.findViewById(R.id.imovel_id)).getText().toString();

                ImageView eFile = ((ImageView) view.findViewById(R.id.thumbnail));
                String imgUrl = (String) eFile.getTag();

                String negocio = ((TextView) view.findViewById(R.id.negocio)).getText().toString();
                String imovel = ((TextView) view.findViewById(R.id.imovel)).getText().toString();
                String mobilia = ((TextView) view.findViewById(R.id.mobilia)).getText().toString();
                String garagem = ((TextView) view.findViewById(R.id.garagem)).getText().toString();

                String preco = ((TextView) view.findViewById(R.id.preco)).getText().toString();

                Intent i = new Intent(getApplicationContext(), EventDetails.class);

                i.putExtra(TAG_ID, imovelID);
                i.putExtra(TAG_ARQUIVO, imgUrl);
                i.putExtra(TAG_NEGOCIO, negocio);
                i.putExtra(TAG_IMOVEL, imovel);
                i.putExtra(TAG_MOBILIA, mobilia);
                i.putExtra(TAG_GARAGEM, garagem);
                i.putExtra(TAG_PRECO, preco);
                startActivity(i);

            }
        }); 

        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        //pDialog.dismiss();

    }   
}

1 个答案:

答案 0 :(得分:0)

显示不确定的ProgressBar,直到加载数据。加载完成后取消进度条

参见: http://developer.android.com/reference/android/widget/ProgressBar.html

另见Android indeterminate progress bar