2016-02-21 94 views
0

好了,到目前爲止,我的MainActivity這個簡單的應用程序是這樣的:Android應用程序不會從第一正常運行,嘗試

public class MainActivity extends AppCompatActivity { 

ListView moviesListView; 
ArrayList <Movie> moviesList; 
MoviesAdapter moviesAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    moviesListView = (ListView) findViewById(R.id.moviesListView); 
    moviesList = new ArrayList<Movie>(); 

    runProgram(); 
    moviesAdapter = new MoviesAdapter(getApplicationContext(), R.layout.movies_layout, moviesList); 
    moviesListView.setAdapter(moviesAdapter); 
} 

private void runProgram() { 

    String url = "https://api.themoviedb.org/3/" + 
      "discover/movie?primary_release_date.gte=2016-02-01&primary_release_date.lte=2016-02-18" + 
      "&api_key=..............................."; 

    JsonObjectRequest jsObjRequest = new JsonObjectRequest 
      (Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        getMovies(response); 
       } 
      }, new Response.ErrorListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 
        // TODO Auto-generated method stub 

       } 
      }); 

    // Access the RequestQueue through your singleton class. 
    MySingleton.getInstance(this).addToRequestQueue(jsObjRequest); 
} 


private void getMovies(JSONObject response) { 
    JSONArray jsonArray = null; 
    String imageBaseURL = "http://image.tmdb.org/t/p/w500"; 
    try { 
     jsonArray = response.getJSONArray("results"); 
     for (int i = 0; i < 5; i++){ 
      Movie movie = new Movie(jsonArray.getJSONObject(i).getString("title"), 
        jsonArray.getJSONObject(i).getString("overview"), 
        imageBaseURL + jsonArray.getJSONObject(i).getString("poster_path") 
        + "?&api_key=6b54bd769063ac68b99fac57aa334eae"); 
      moviesList.add(movie); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

}

我想在這裏做的就是要求電影來自TMDB,並將它們顯示在一個ListView中。但我的問題是,你第一次運行應用程序,它不顯示任何東西,你只會看到白屏。 現在,如果您再次運行它,它會在ListView中顯示電影。但是,如果您清除應用程序的緩存並嘗試再次運行一次,則會再次看到白色屏幕......。我對這裏發生的事情感到困惑。 我試着調試過它,它看起來好像是獲取數據的速度非常快,所以無論發生什麼事情發生之後。

而且,這裏是我的適配器的情況下,代碼:

public class MoviesAdapter extends ArrayAdapter<Movie> { 

    ArrayList<Movie> moviesList; 
    int adapterResource; 
    Context adapterContext; 
    LayoutInflater layoutInflator; 

    public MoviesAdapter(Context context, int resource, ArrayList<Movie> objects) { 
     super(context, resource, objects); 

     moviesList = objects; 
     adapterResource = resource; 
     adapterContext = context; 
     layoutInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     final ViewHolder viewHolder; 
     if (convertView == null){ 
      convertView = layoutInflator.inflate(adapterResource, null); 
      viewHolder = new ViewHolder(); 

      viewHolder.movieImage = (ImageView) convertView.findViewById(R.id.movieImage); 
      viewHolder.movieName = (TextView) convertView.findViewById(R.id.movieName); 
      viewHolder.movieOverview = (TextView) convertView.findViewById(R.id.movieOverview); 

      convertView.setTag(viewHolder); 
     } 
     else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     new DownloadImageTask(viewHolder.movieImage).execute(moviesList.get(position).getImageURL()); 
     viewHolder.movieName.setText(moviesList.get(position).getName()); 
     viewHolder.movieOverview.setText(moviesList.get(position).getOverview()); 
     return convertView; 
    } 


    public static class ViewHolder{ 
     public ImageView movieImage; 
     public TextView movieName; 
     public TextView movieOverview; 
    } 

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
     ImageView bmImage; 

     public DownloadImageTask(ImageView bmImage) { 
      this.bmImage = bmImage; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      String urldisplay = urls[0]; 
      Bitmap mIcon11 = null; 
      try { 
       InputStream in = new java.net.URL(urldisplay).openStream(); 
       mIcon11 = BitmapFactory.decodeStream(in); 
      } catch (Exception ex) { 
       Log.e("Error", ex.getMessage()); 
       ex.printStackTrace(); 
      } 
      return mIcon11; 
     } 

     protected void onPostExecute(Bitmap result) { 
      bmImage.setImageBitmap(result); 
     } 

    } 

} 

任何幫助將非常感激。

回答

1

每次調用getView()時,都會啓動一個新的DownloadTask。這是完全錯誤的,因爲相同的圖像可能已經被下載/在這個過程中

使用通用圖像裝載機或畢加索的下載:

取而代之的是:

new DownloadImageTask(viewHolder.movieImage).execute(moviesList.get(position).getImageURL()); 

用途:

Picasso.with(context) 
    .load(moviesList.get(position).getImageURL()) 
    .into(viewHolder.movieImage); 

另外,看起來你的getMovies()函數是在主線程上執行的。既然你在這裏解析JSON,通過使用AsyncTask

再次將其移動到後臺線程,你做​​後,你需要調用notifyDataSetChange()moviesAdapter

+0

代碼應該去哪裏是什麼呢?而不是什麼?謝謝,我會盡快讓你知道。 –

+0

@ billy_b29查看編輯 –

+0

太棒了!它通過添加畢加索庫和moviesAdapter.notifyDataSetChange()來工作。現在我將閱讀有關多線程的內容,以便添加它。感謝您的幫助!我一次學會了3件新事物。 –

相關問題