2017-10-23 9 views
1

こんにちは私はホルダーパターンを使用してカスタムムービーリストを実装しようとしています。現在、リストを正常に表示しています。唯一のアクティビティがロードされる前に、いくつかのメッセージとともにプログレスバーを表示したい。私は非同期タスクと進行状況バーの周りに私の頭を包み込むように見えることができないと私はいくつかの例に従っているが、それらを理解するように見えることができない、誰かがそれを動作させるために挿入するためにどこにアドバイスしてください。私は本当にそれを感謝します。ここでカスタムリストのプログレスバー

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> 

任意の助けいただければ幸いです。

+0

サービスを実行してデータを取得するときに進行状況バーを挿入し、完了時に進行状況バーを閉じることができます。 – Umair

+0

非同期タスクのサンプルコードを理解しにくいものとして表示できますか? Cheers – Sam

+0

コード内のプログレスバー機能の使い方を知るための答えを追加します。 – Umair

答えて

1

ロードバーを開始する際にasynctaskを作成し、asynctaskが終了したら、それをただ終了します。

パブリッククラスMyAsyncはAsyncTask> {

@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を呼び出しますか。

+0

クイック質問この非同期タスクはどのような種類のパラメータをとりますか?目的は何ですか? – Sam

+0

Fakher

+0

このチュートを見てください:http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html#asynctask – Fakher

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"); 
} 

私はそれはあなたのアイデアを与える希望を却下どのようにそれを行うことができます。

関連する問題