带自定义列表的进度条

时间:2017-10-23 14:20:16

标签: android android-asynctask progress-bar

嘿我正在尝试使用持有者模式实现自定义电影列表。目前,我正在成功显示列表。我想在加载唯一的活动之前显示带有一些消息的进度条。我似乎无法用异步任务包围进度条,我已经按照一些示例但似乎无法理解它们,有人可以建议在哪里插入以及插入什么以使其工作。我真的很感激。

这是我唯一的活动

 public class MovieRatingsActivity extends ListActivity
    {
    private ArrayList<Movie> movies = new ArrayList<Movie>();
    private LayoutInflater mInflater;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initializeUI();
    }

    private void initializeUI()
    {
        mInflater = (LayoutInflater) 
           this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);      
        InputStream inputStream = getResources().openRawResource(   R.raw.ratings);
        movies = Movie.loadFromFile(inputStream);       
        setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));
    }

    //declare a static holder class
    public static class ViewHolder{
        ImageView icon;
        TextView movieText,votesText;
    }

    /** Custom row adatper -- that displays an icon next to the movie name */
    class RowIconAdapter extends ArrayAdapter<Movie> 
    {
        private ArrayList<Movie> movies;        
        public RowIconAdapter(Context c, int rowResourceId, int textViewResourceId, 
                ArrayList<Movie> items)
        {
            super(c, rowResourceId, textViewResourceId, items);
            movies  = items;
        }

        public View getView(int pos, View convertView, ViewGroup parent)
        {

            //declare a holder object
            ViewHolder holder;
            Movie currMovie = movies.get(pos);

            //nly inflate view once and get all the views once
            if (convertView == null)
            {
                convertView = mInflater.inflate(R.layout.listrow, parent, false);
                holder = new ViewHolder();
                holder.icon = (ImageView) convertView.findViewById(R.id.row_icon);
                holder.movieText = (TextView) convertView.findViewById(R.id.row_label);
                holder.votesText= (TextView) convertView.findViewById(R.id.row_subtext);

                //set the holder class
                convertView.setTag(holder);
            }

            else{
                //get all the views once done
                holder = (ViewHolder) convertView.getTag();
            }

            //set all the values in the list
            holder.movieText.setText(currMovie.getName());
            String votesStr = currMovie.getVotes()+" votes";
            holder.votesText.setText(votesStr);
            Bitmap movieIcon = getMovieIcon(currMovie.getName(), currMovie.getRating());
            holder.icon.setImageBitmap(movieIcon);

            return convertView;
        }
    }

我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:id="@android:id/list" 
        android:layout_height="fill_parent"  
        android:layout_width="match_parent" />
</LinearLayout>

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

在启动加载栏时创建一个asynctask,当asynctask完成时,只需将其解除。

公共类MyAsync扩展了AsyncTask&gt; {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Show you progress bar
    }

    @Override
    protected Void doInBackground(Void... voids) {
          InputStream inputStream = getResources().openRawResource(   
                R.raw.ratings);
          movies = Movie.loadFromFile(inputStream);            
          return movies;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        //Hide your progress bar.
        setListAdapter(new RowIconAdapter(this, R.layout.listrow, R.id.row_label, movies));

    }
}

然后在OnCreate或您想要显示电影列表的任何地方调用此AsyncTask。

答案 1 :(得分:1)

onPreExecute 中初始化您的进度条并执行以下操作:

 @Override
protected void onPreExecute() {
    super.onPreExecute();
    try {
        createProgressBar(context);   // add methodology of your progressbar here,     
       }
    }catch (Exception e){
        Log.d(TAG, "Exception: >> "+e.toString());
    }

onPostExecute 中获得结果后,取消进度条

   @Override
protected void onPostExecute(Object o) {
    super.onPostExecute(o);

    try {
        cancelProgressBar();   // here add your progressbar to be dismissed.
    }catch (Exception e){
        Log.d(TAG, "Exception: >> "+e.toString());
    }

    Log.d(TAG, "On_Post");
}

我希望它能让你知道如何做到这一点。

相关问题