加载ListView期间的进度栏

时间:2014-06-02 19:43:58

标签: java android listview android-listview android-asynctask

因此,我希望在填充ListView时显示旋转加载指示器。我成功地实现了进度条,但由于某种原因,它会在显示所有列表之前消失。我想要的是在列表的TOTAL加载时间内出现的进度条。基本上,它看起来像每个列表一次显示一个,而不是全部加载时。 我正在做的是   1.创建新的自定义适配器类   2.使用此适配器类在AsyncTask中填充ListView   3.将ListView设置为此适配器

这样可以正常工作,进度条会在显示所有列表之前消失。有没有人有任何想法?

活动类:

public class MainActivity extends ActionBarActivity {

ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
// public static Bitmap bitmap;
Button refresh;
ProgressBar progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    progress=(ProgressBar)findViewById(R.id.progressbar_loading);
    // Construct the data source
    arrayOfLocations = new ArrayList<Location>();

    // Create the adapter to convert the array to views
    adapter = new LocationAdapter(this, arrayOfLocations);

    FillLocations myFill = new FillLocations();
    myFill.execute();



    refresh = (Button) findViewById(R.id.refresh);
    refresh.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            finish();
            startActivity(getIntent());
        }
    });

}


private class FillLocations extends AsyncTask<Integer, Void, String> {
    String msg = "Done";

    protected void onPreExecute() {
        progress.setVisibility(View.VISIBLE);
    }

    // Decode image in background.
    @Override
    protected String doInBackground(Integer... params) {

        String result = "";
        InputStream isr = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://afs.spotcontent.com/"); // YOUR
                                                                                // PHP
                                                                                // SCRIPT
                                                                                // ADDRESS
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
            // resultView.setText("connected");
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(isr, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            isr.close();

            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error  converting result " + e.toString());
        }

        // parse json data
        try {
            JSONArray jArray = new JSONArray(result);

            for (int i = 0; i < jArray.length(); i++) {
                final JSONObject json = jArray.getJSONObject(i);

                try {

                    BitmapWorkerTask myTask = new BitmapWorkerTask(
                            json.getInt("ID"), json);
                    myTask.execute();

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error Parsing Data " + e.toString());
        }
        return msg;

    }

    protected void onPostExecute(String msg) {
        // Attach the adapter to a ListView
        ListView listView = (ListView) findViewById(R.id.listView1);

        // View header = (View) getLayoutInflater().inflate(
        // R.layout.listview_header, null);
        // listView.addHeaderView(header);

        listView.setAdapter(adapter);
        progress.setVisibility(View.GONE);

    }
}
}

适配器类:

public class LocationAdapter extends ArrayAdapter<Location> {
public LocationAdapter(Context context, ArrayList<Location> locations) {
   super(context, R.layout.item_location, locations);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   // Get the data item for this position
   Location location = getItem(position);    
   // Check if an existing view is being reused, otherwise inflate the view
   if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_location, parent, false);
   }

   // Lookup view for data population
   TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
   TextView tvDetails = (TextView) convertView.findViewById(R.id.tvDetails);
   TextView tvDistance = (TextView) convertView.findViewById(R.id.tvDistance);
   TextView tvHours = (TextView) convertView.findViewById(R.id.tvHours);
   ImageView ivIcon = (ImageView) convertView.findViewById(R.id.imgIcon);

   // Populate the data into the template view using the data object
   tvName.setText(location.name);
   tvDetails.setText(location.details);
   tvDistance.setText(location.distance);
   tvHours.setText(location.hours);
   ivIcon.setImageBitmap(location.icon);
   // Return the completed view to render on screen
   return convertView;
}
}

3 个答案:

答案 0 :(得分:0)

这种行为的原因是你正在启动多个线程。

FillLocations preExecute --> SHOW ProgressBar
BitmapWorkerTask_1 --> new thread
BitmapWorkerTask_2 --> new thread
...
BitmapWorkerTask_N --> new thread

FillLocations postExecute --> HIDE ProgressBar
BitmapWorkerTask_K --> continue execution
BitmapWorkerTask_K+1 --> continue execution

如果希望列表显示直到全部加载,只需使BitmapWorker的处理同步即可。如果您仍然希望立即显示列表但保留微调器直到它全部完成,那么在您的活动中保留一个计数器并在preexecute中增加它并通过setter在postExecute of BitmapWorker中减少它。一旦计数器达到0,删除隐藏progressBar。

活动:

private int asynchCounter = 0;
private void updateCounter(int delta){
    asynchCounter+=delta;
    if(asynchCounter<=0){
        progress.setVisibility(View.GONE);
    }else{
        progress.setVisibility(View.VISIBLE);
    }
}

而不是BitmapWorkerTask使用

class CountedBitmapWorkerTask extends BitmapWorkerTask {
protected void onPreExecute() {
    super.onPreExecute();
    updateCounter(1);
}
protected void onPostExecute(String msg) {
        super.onPostExecute();
        updateCounter(-1);

    }
}

答案 1 :(得分:-1)

我遇到了这个问题,为了解决这个问题,我必须编写AsyncTask完整的监听器。其中向UI线程发送通知,该数据已加载且必须更改某些内容,在这种情况下隐藏ProgressBar

这是应该如何看的基本示例。我不确定这会在你复制到你的项目之后对你有用,但是你需要完整的监听器,所以在研究了这个案例后你应该能够找到解决方案。

AsyncTaskCompleteListener.java - 侦听器界面。

public interface AsyncTaskCompleteListener {
       public void onTaskComplete();
}

<强> LoadDataTask.java

class LoadDataTask extends AsyncTask<Object, Object, Object> { 
    /* Your object types according to your task. */

    private AsyncTaskCompleteListener callback; // Callback field

    public LoadDataTask(AsyncTaskCompleteListener cb){
        this.callback = cb; 
    }

    @Override
        protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(String... urls) {
        /* Your task here */
        return result;
    }

    @Override
    protected void onPostExecute(Object o) {
           callback.onTaskComplete(); // Set the Callback
    }
}

<强> MainActivity.java

   public class MainActivity implements AsyncTaskCompleteListener{
       /* ...Other methods and fields... */


       /* onTaskComplete method which fires after your data is loaded. */
       @Override
       public void onTaskComplete(){
           // Hide ProgressBar
       }
   }

答案 2 :(得分:-1)

自我插件:https://github.com/horvste/EasyWebPageDownloadForAndroid

这会将线程与实现分开并解决您的问题。这与Tony建议的非常相似,除非它已经为你实现了。

Github自述文件:

适合连接REST API,HTML解析和许多其他用途。使用这个库很容易:

  1. 创建一个实现OnProgressUpdate的类
  2. 公共类SampleClass实现OnProgressUpdate {

        @Override
        public void onUpdate(Integer percentProgress) {
    
        }
    
        @Override
        public void onUpdateFailure() {
    
        }
    
        @Override
        public void onSuccess(StringBuilder result) {
    
        }
    
        @Override
        public void onFailure() {
    
        }
    }
    

    } 实例化DownloadWebPage对象

    DownloadWebPage webPage = new DownloadWebPage(new SampleClass(), myUrl);
    

    从DownloadWebPage

    调用.downloadHtml()
    webPage.downloadHtml();
    

    此外,如果asynchtask正确更新并且项目数量很大,请查看: listing a listview is taking too much time and memory in android

    另一种选择是只列出一定数量的项目然后有一个下一页按钮或手势来处理ListView加载太慢。