为什么我没有从单独的AsyncTask类到Fragment Class获得List <moviemodel> ArrayList数据?

时间:2016-03-15 22:37:58

标签: android android-asynctask android-arrayadapter parcelable parcel

请帮助我,我错了。我对这个问题感到厌倦。 我有一个类ForcastTask扩展了AsyncTask,另一个类ForcastFragment扩展了Fragment实现的AsyncResponse和Main Activity类。

我想从AsyncTask类获取onPostExcute(List data)并使用AsyncResponse接口发送到ForcastFragment类。

但是它只显示null,我在json viewer online中检查了我的查询参数它是否有效,当我在单个类中尝试它然后它正常工作。

public class ForecastTask extends AsyncTask<String, String, List<MovieModel>> {
    private final String LOG_TAG = ForecastTask.class.getSimpleName();    
    public AsyncResponse delegate=null;
    public List<MovieModel> movieModelList=null;
    @Override
    protected List<MovieModel> doInBackground(String... params) {
        if (params.length == 0) {
            return null;
        }

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

        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(Popularity);
            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");

            //adding JSON Array data into MovieModel Class
            movieModelList = new ArrayList<>();
            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"));
                    movieModel.setRelease_date(finalObject.getString("release_date"));
                    movieModel.setVote_average((float) finalObject.getDouble("vote_average"));
                    movieModel.setOverview(finalObject.getString("overview"));
                movieModelList.add(movieModel);
            }

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

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

2- ForecastFragment类

public class ForecastFragment extends Fragment implements AsyncResponse{
    private List<MovieModel> movieModelList=null;
    private static final String STATE_MOVIES ="state_movies";
    private CustomAdapter customAdapter =null;
    private GridView gridView=null;
    private View rootView=null;

    ForecastTask forecastTask=new ForecastTask();

    //Constructor
    public ForecastFragment() {
    }

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

        forecastTask.delegate=this;
        forecastTask.execute("Popular");
    }
    @Override
    public void processFinish(List<MovieModel> movieModels) {
        movieModelList=movieModels;
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelableArrayList(STATE_MOVIES, (ArrayList<? extends Parcelable>) movieModelList);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.most_Popular:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    new ForecastTask().execute("Popular");
                    return true;
                }
            case R.id.High_rated:
                if(item.isChecked()){
                    item.setChecked(false);
                }else{
                    item.setChecked(true);
                    new ForecastTask().execute("TOP_RATED");
                    return true;
                }
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_main, container, false);

        customAdapter= new CustomAdapter(
                getActivity(),
                R.layout.list_image_forecast,
                movieModelList);
        gridView=(GridView)rootView.findViewById(R.id.gridView);

        if (movieModelList==null){
            Toast.makeText(getActivity(),"Null",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getActivity(), "Data", Toast.LENGTH_SHORT).show();

            gridView.setAdapter(customAdapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String movie_name = movieModelList.get(position).getTitle();
                    String poster_path = movieModelList.get(position).getPoster_path();
                    String release_date = movieModelList.get(position).getRelease_date();
                    Float users_rating = movieModelList.get(position).getVote_average();
                    String overview = movieModelList.get(position).getOverview();

                    Intent intent = new Intent(getActivity(), DetailActivity.class)
                            .putExtra("movie_Name", movie_name)
                            .putExtra("poster_Path", poster_path)
                            .putExtra("release_Date", release_date)
                            .putExtra("users_Rating", users_rating)
                            .putExtra("overview", overview);
                    startActivity(intent);
                }
            });
        }
        return rootView;
    }
}

3-异步接口

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

4-Movie DB

public class MovieModel implements Parcelable {
    private int id;
    private String title;
    private String poster_path;
    private String release_date;
    private float vote_average;
    private String overview;

    private List<MovieModel> models;
    public MovieModel(){

    }

    public List<MovieModel> getModels() {
        return models;
    }

    public void setModels(List<MovieModel> models) {
        this.models = models;
    }

    public MovieModel(int id, String title, String poster_path, String release_date, float vote_average, String overview) {
        this.id = id;
        this.title = title;
        this.poster_path = poster_path;
        this.release_date = release_date;
        this.vote_average = vote_average;
        this.overview = overview;
    }

    protected MovieModel(Parcel in) {
        id = in.readInt();
        title = in.readString();
        poster_path = in.readString();
        release_date = in.readString();
        vote_average = in.readFloat();
        overview = in.readString();
    }

    public static final Creator<MovieModel> CREATOR = new Creator<MovieModel>() {
        @Override
        public MovieModel createFromParcel(Parcel in) {
            return new MovieModel(in);
        }

        @Override
        public MovieModel[] newArray(int size) {
            return new MovieModel[size];
        }
    };

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getPoster_path() {
        return poster_path;
    }
    public void setPoster_path(String poster_path) {
        this.poster_path = poster_path;
    }

    public String getRelease_date() {
        return release_date;
    }
    public void setRelease_date(String release_date) {
        this.release_date = release_date;
    }

    public float getVote_average() {
        return vote_average;
    }
    public void setVote_average(float vote_average) {
        this.vote_average = vote_average;
    }

    public String getOverview() {
        return overview;
    }
    public void setOverview(String overview) {
        this.overview = overview;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(id);
        out.writeString(title);
        out.writeString(poster_path);
        out.writeString(release_date);
        out.writeFloat(vote_average);
        out.writeString(overview);
    }

}

5-主要活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Call the ForecastFragment Class by getFragmentManager
        getFragmentManager()
                .beginTransaction()
                .add(R.id.Fragment_Container, new ForecastFragment(),
                        ForecastTask.class.getSimpleName())
                .commit();
    }

2 个答案:

答案 0 :(得分:0)

您只是在定义为片段成员的delegate中分配forecastTask一次。但是,您可以在其他位置创建多个任务,而无需分配其委托。您可能需要为每个任务分别为每个任务分配委托以在onPostExecute中进行更改,否则它将因NullPointerException而崩溃。

答案 1 :(得分:0)

我建议你将AsyncResponse的一个对象传递给ForecastTask构造函数。

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

在您的片段中,您可以执行此操作。

ForecastTask forecastTask = new ForecastTask(this);
forecastTask.execute("Popular");