加载ListView期间的进度栏

用户名

因此,我想在填充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;
}
}
统一

该行为的原因是您正在启动多个线程。

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的处理同步即可。如果您仍然想立即显示列表,但要保持微调框直到其全部完成,则在活动中保留一个计数器,并通过设置器在BitmapWorker的postExecute中增加它,并在它的postExecute中减少它。一旦计数器达到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);

    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

加载数据时,ListView不显示进度栏

来自分类Dev

AngularJS加载进度栏

来自分类Dev

ListView onItemClick水平进度栏

来自分类Dev

进度栏加载文件

来自分类Dev

使用进度栏加载表格

来自分类Dev

listView内的加载栏

来自分类Dev

进度栏,用于加载活动或加载按钮进度

来自分类Dev

如何更新ListView项中的进度栏

来自分类Dev

jQuery datatables:加载实时进度栏

来自分类Dev

如何使用PHP“单击”加载进度栏

来自分类Dev

进度栏加载两次WebView

来自分类Dev

回发期间的ASP.NET显示进度栏

来自分类Dev

使用NSFileManager在复制文件期间更新进度栏

来自分类Dev

长时间的javascript计算期间的css进度栏

来自分类Dev

如何在复制文件期间创建进度栏

来自分类Dev

android-在加载主布局之前加载进度栏布局

来自分类Dev

WPF:ListView中的进度栏未垂直对齐

来自分类Dev

WPF设置新值到我的ListView进度栏列

来自分类Dev

减慢进度栏进度

来自分类Dev

页面加载上的Javascript计时问题-和引导进度栏

来自分类Dev

使用DataTable将数据加载到DataGridView的进度栏

来自分类Dev

引导程序:进度栏加载无法正常工作

来自分类Dev

如何使WebView快速加载并向其中添加进度栏

来自分类Dev

Shopify页面加载进度栏永远不会完成

来自分类Dev

加载图像或进度栏以在XPages中提前输入

来自分类Dev

即使页面重新加载也更新jQuery进度栏

来自分类Dev

在列表中加载图像时如何处理进度栏

来自分类Dev

页面加载上的Javascript计时问题-和引导进度栏

来自分类Dev

自动刷新div jQuery +进度栏加载器