Loader问题
请问老师,我给fragment使用loader接口了,但是运行程序时,程序不会向网络申请数据,我是不是把getLoaderManager().initLoader(0,null,this);放错位置了?getLoaderManager().initLoader(0,null,this);应该放在f阿fragment哪一个方法才对呀?
fragment
package com.example.movieimooc.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.movieimooc.R; import com.example.movieimooc.bean.MovieData; import java.util.List; import static android.content.ContentValues.TAG; public class MonvieFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<MovieData>> { private RecyclerView mhotRecycle; public MonvieFragment() { super(); } @Override public void onCreate(@Nullable Bundle savedInstanceState) { Log.e(TAG, "onCreate: " ); super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_movie,container,false); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // mhotRecycle = (RecyclerView) view.findViewById(R.id.id_hot_moview_recycle); getLoaderManager().initLoader(0,null,this); Log.e(TAG, "onCreateView: " ); } @Override public Loader<List<MovieData>> onCreateLoader(int id, Bundle args) { return null; } @Override public void onLoadFinished(Loader<List<MovieData>> loader, List<MovieData> data) { Log.e(TAG, "onLoadFinished: "+data.get(0).getTitle() ); } @Override public void onLoaderReset(Loader<List<MovieData>> loader) { } }
activity
package com.example.movieimooc; import android.os.Bundle; import android.support.v4.app.LoaderManager; import android.support.v4.content.Loader; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.widget.EditText; import android.widget.Spinner; import com.example.movieimooc.adpter.MovieAdapter; import com.example.movieimooc.bean.MovieData; import com.example.movieimooc.fragment.MonvieFragment; import com.example.movieimooc.net.MovieLoader; import java.util.ArrayList; import java.util.List; import static android.content.ContentValues.TAG; public class MainActivity extends AppCompatActivity { private List<MovieData> mdatas=new ArrayList<>(); private MovieAdapter ovieAdapter; private RecyclerView mrecycle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText edit = (EditText) findViewById(R.id.id_ed_movie_name); Spinner spinner = (Spinner) findViewById(R.id.id_movie_sp_type); // mrecycle = (RecyclerView) findViewById(R.id.id_movie_recycle); // getSupportLoaderManager().initLoader(0,null,this); // mrecycle.setLayoutManager(new GridLayoutManager(this,2)); MonvieFragment hotFragment=new MonvieFragment(); getSupportFragmentManager().beginTransaction() .add(R.id.id_list_content,hotFragment) .commit(); } }
loader
package com.example.movieimooc.net; import android.content.Context; import android.content.IntentFilter; import android.graphics.Movie; import android.support.v4.content.AsyncTaskLoader; import android.util.Log; import android.widget.LinearLayout; import com.example.movieimooc.Config.Config; import com.example.movieimooc.bean.MovieData; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import static android.content.ContentValues.TAG; public class MovieLoader extends AsyncTaskLoader<List<MovieData>> { public MovieLoader(Context context) { super(context); } @Override protected void onStartLoading() { super.onStartLoading(); if (isStarted()){ forceLoad(); } } @Override public List<MovieData> loadInBackground() { Log.e(TAG, "准备申请数据了"); List<MovieData> mmData=new ArrayList<MovieData>(); try { Log.e(TAG, "我进来了1" ); URL url=new URL("http://www.imooc.com/api/movie"); Log.e(TAG, "我进来了2" ); HttpURLConnection conn= (HttpURLConnection) url.openConnection(); Log.e(TAG, "我进来了3" ); conn.setRequestMethod("POST"); Log.e(TAG, "我进来了4" ); conn.setConnectTimeout(3000); Log.e(TAG, "我进来了5" ); Log.e(TAG, "responseCode"+conn.getResponseCode() ); if (conn.getResponseCode()==HttpURLConnection.HTTP_OK){ Log.e(TAG, "conn成功"); BufferedReader buff=new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; StringBuilder build=new StringBuilder(); while ((line=buff.readLine())!=null){ build.append(line); } JSONObject object=new JSONObject(build.toString()); String movies = object.getString("movies"); Gson gson=new Gson(); Log.e(TAG, "准备回调了"); mmData=gson.fromJson(movies, new TypeToken<List<MovieData>>(){}.getType()); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return mmData; } }
log
05-03 19:12:22.593 4888-4888/com.example.movieimooc E/ContentValues: onCreate: 05-03 19:12:22.603 4888-4888/com.example.movieimooc E/ContentValues: onCreateView: 05-03 19:12:22.681 4888-4888/com.example.movieimooc E/RecyclerView: No adapter attached; skipping layout 05-03 19:12:22.894 4888-4888/com.example.movieimooc E/RecyclerView: No adapter attached; skipping layout 05-03 19:12:25.398 4888-4888/com.example.movieimooc E/RecyclerView: No adapter attached; skipping layout
7
收起
正在回答
1回答
每个Activity或Fragment只有一个LoaderManager,可以在Activity的onCreate()或者Fragment的onActivityCreated()里初始化一个Loader,调用initLoader()。
Android数据通信开发与应用2018版
- 参与学习 人
- 提交作业 147 份
- 解答问题 687 个
本专题介绍了Android开发核心组件:广播、服务及全局应用。教会你如何使用AIDL、Thread、Socket、蓝牙解决进程线程间通信问题。利用Glide等实现异步加载及NDK原生代码开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星