-1

私はそれを試してみましたが、何も表示されません、私は別のAsyncTaskクラスからフラグメントクラスにonPostExecuteメソッドの結果を得る方法がわかりません。 別のAsynctaskクラスから別のフラグメントクラスへのonPostExecuteの結果を取得する方法は?

2 - AsyncTaskクラス...私を助けてください

public class ForecastTask extends AsyncTask<String, String, `List<MovieModel>> {` 

    private final String LOG_TAG = ForecastTask.class.getSimpleName(); 
    private List<MovieModel> movieModelList; 
    public AsyncResponse delegate=null; 

    public ForecastTask(AsyncResponse listener) { 
     delegate = listener; 
    } 

    @Override 
    protected List<MovieModel> doInBackground(String... params) { 
     if (params.length == 0) { 
      return null; 
     } 

     HttpURLConnection connection = null; 
     BufferedReader reader = null; 

     try { 
      URL url = new URL(param[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestMethod("GET"); 
      connection.connect(); 

      InputStream inputStream = connection.getInputStream(); 
      if (inputStream == null) { 
       return null; 
      } 

      reader = new BufferedReader(new InputStreamReader(inputStream)); 
      StringBuffer buffer = new StringBuffer(); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 

      JSONObject jsonObject = new JSONObject(buffer.toString()); 
      JSONArray jsonArray = jsonObject.getJSONArray("results"); 

      movieModelList= new ArrayList<>(); 
      //adding JSON Array data into MovieModel Class object 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject finalObject = jsonArray.getJSONObject(i); 
       MovieModel movieModel = new MovieModel(); 
       movieModel.setId(finalObject.getInt("id")); 
       movieModel.setTitle(finalObject.getString("title")); 
       movieModel.setPoster_path(finalObject.getString("poster_path")); 

      } 
      return movieModelList; 
     } catch (JSONException | IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if (reader != null) { 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    public ForecastTask() { 
     super(); 
    } 

    @Override 
    protected void onPostExecute(List<MovieModel> movieModels) { 
     delegate.processFinish(movieModels); 
    } 
} 

の3-フラグメントクラス

public class ForecastFragment extends Fragment implements AsyncResponse { 

    private String Popular = "http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxx"; 

    private List<MovieModel> movieModels; 
    private static final String STATE_MOVIES ="state_movies"; 
    CustomAdapter customAdapter=null; 
    GridView gridView=null; 
    View rootView=null; 
    ForecastTask forecastTask; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(true); 
    }  

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     rootView = inflater.inflate(R.layout.activity_main, container, false); 
     gridView=(GridView)rootView.findViewById(R.id.gridView); 
     movieModels=new ArrayList<MovieModel>(); 
      forecastTask=new ForecastTask(this); 
      forecastTask.delegate = this; 
      forecastTask.execute(Popular); 

     customAdapter = new CustomAdapter(getActivity(), movieModels); 
     gridView.setAdapter(customAdapter); 

     return rootView; 
    } 

    @Override 
    public void processFinish(List<MovieModel> movieModels) { 
     this.movieModels=movieModels; 
    } 
} 

、4- AsyncResponseインタフェース

public interface AsyncResponse { 
    void processFinish(List<MovieModel> movieModels); 
} 
+0

結果を別のAsynctaskクラスから別のフラグメントクラスに取得するにはどうすればよいですか?助けてください... – Santosh

答えて

0

あなたはそうではありませんあなたのを追加するの商品はmovieModelListです。だから、それは空のままです。

movieModelList= new ArrayList<>(); 
      //adding JSON Array data into MovieModel Class object 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject finalObject = jsonArray.getJSONObject(i); 
       MovieModel movieModel = new MovieModel(); 
       movieModel.setId(finalObject.getInt("id")); 
       movieModel.setTitle(finalObject.getString("title")); 
       movieModel.setPoster_path(finalObject.getString("poster_path")); 


movieModelList.add(movieModel); //Add this line 




      } 

はまた、あなたが設定したり、processFinish()コールバックで、アダプタを更新する必要があります - あなたは次のように変更する必要があります。 ForecastTaskは異なるスレッドで実行されるため、非同期です。

@Override 
    public void processFinish(List<MovieModel> movieModels) { 
     this.movieModels=movieModels; 
     customAdapter = new CustomAdapter(getActivity(), movieModels); 
     gridView.setAdapter(customAdapter); 
    } 
+0

実際に私はそれを追加しますが、動作しません。 – Santosh

+0

movieModelList = new ArrayList <>(); for(int i = 0; i Santosh

+0

githubのリンクでプロジェクトを確認してください:https://github.com/superssingh/MoviesInfoApp.git – Santosh

関連する問題