Android 如何从一个单独的Asynctask类到另一个fragment类获取onPostExecute的结果?
我尝试了它,但它并没有显示任何内容,我想我不知道如何从单独的AsyncTask类到fragment类获取onPostExecute方法的结果。 请帮帮我Android 如何从一个单独的Asynctask类到另一个fragment类获取onPostExecute的结果?,android,android-fragments,android-studio,android-asynctask,asynctaskloader,Android,Android Fragments,Android Studio,Android Asynctask,Asynctaskloader,我尝试了它,但它并没有显示任何内容,我想我不知道如何从单独的AsyncTask类到fragment类获取onPostExecute方法的结果。 请帮帮我 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager()
.beginTransaction()
.add(R.id.Fragment_Container, new ForecastFragment(),
ForecastTask.class.getSimpleName())
.commit();
}
2-异步任务类
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);
}
}
公共类ForecastTask扩展了AsyncTask{`
private final String LOG_TAG=ForecastTask.class.getSimpleName();
私有列表电影模型列表;
公共异步响应委托=null;
公共预测任务(异步响应侦听器){
代表=听众;
}
@凌驾
受保护列表doInBackground(字符串…参数){
如果(params.length==0){
返回null;
}
HttpURLConnection=null;
BufferedReader reader=null;
试一试{
URL=新URL(参数[0]);
connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“GET”);
connection.connect();
InputStream InputStream=connection.getInputStream();
如果(inputStream==null){
返回null;
}
reader=新的BufferedReader(新的InputStreamReader(inputStream));
StringBuffer=新的StringBuffer();
弦线;
而((line=reader.readLine())!=null){
buffer.append(行);
}
JSONObject JSONObject=新的JSONObject(buffer.toString());
JSONArray JSONArray=jsonObject.getJSONArray(“结果”);
movieModelList=新的ArrayList();
//将JSON数组数据添加到MovieModel类对象中
for(int i=0;i
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;
}
}
公共类ForecastFragment扩展片段实现异步响应{
私有字符串Popular=”http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxx";
私人名单电影模型;
私有静态最终字符串STATE\u MOVIES=“STATE\u MOVIES”;
CustomAdapter=null;
GridView GridView=null;
视图rootView=null;
预报任务预报任务;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
设置选项菜单(真);
}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
rootView=充气机。充气(R.layout.activity\u main,container,false);
gridView=(gridView)rootView.findviewbyd(R.id.gridView);
movieModels=newarraylist();
forecastTask=新的forecastTask(此);
forecastTask.delegate=此;
forecastTask.execute(流行);
customAdapter=新的customAdapter(getActivity(),movieModels);
setAdapter(customAdapter);
返回rootView;
}
@凌驾
public void processFinish(列出电影模型){
this.movieModels=movieModels;
}
}
4-异步响应接口
public interface AsyncResponse {
void processFinish(List<MovieModel> movieModels);
}
公共接口异步响应{
void processFinish(列出电影模型);
}
您没有在电影模型列表中添加电影
项。这就是为什么它仍然是空的。您必须修改如下内容-
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
}
如何从单独的Asynctask类到单独的fragment类获取结果?请帮助我…movieModelList=new ArrayList();对于(int i=0;i @Override
public void processFinish(List<MovieModel> movieModels) {
this.movieModels=movieModels;
customAdapter = new CustomAdapter(getActivity(), movieModels);
gridView.setAdapter(customAdapter);
}