我想使用asynctask在片段中实现我的recyclerview

时间:2016-07-26 15:14:11

标签: android android-asynctask fragment android-recyclerview recycler-adapter

我的代码实际上是来自活动中的recyclerview,现在我希望它在片段上实现..但是当它将启动主片段时,应用程序崩溃..是否可以使用这些相同类型的代码执行此操作?

主要片段

public class MainFragment extends Fragment {


public MainFragment() {
    // Required empty public constructor
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    BackgroundTask backgroundTask = new BackgroundTask(getActivity());
    backgroundTask.execute();
    return inflater.inflate(R.layout.main_fragment_layout, container, false);
}

BackgroundTask.java

public class BackgroundTask extends AsyncTask<Void, Products, Void> {
Context ctx;
Activity activity;
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Products> arrayList = new ArrayList<>();
public BackgroundTask(Context ctx){
    this.ctx = ctx;
    activity = (Activity)ctx;
}
String json_string="http://192.168.0.18/project/json_get_data.php";
@Override
protected void onPreExecute() {
    recyclerView = (RecyclerView)activity.findViewById(R.id.recycleView);
    layoutManager = new LinearLayoutManager(ctx);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapter = new RecyclerAdapter(arrayList);
    recyclerView.setAdapter(adapter);

}

@Override
protected Void doInBackground(Void... params) {
    try {
        URL url = new URL(json_string);
        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine())!= null){
            stringBuilder.append(line + "\n");
        }
        httpURLConnection.disconnect();
        String json_string = stringBuilder.toString().trim();
        JSONObject jsonObject = new JSONObject(json_string);
        JSONArray jsonArray = jsonObject.getJSONArray("server_response");
        int count = 0;
        while (count<jsonArray.length()){
            JSONObject JO = jsonArray.getJSONObject(count);
            count++;
            Products products = new Products(JO.getString("name"), JO.getString("type"),JO.getString("price"));
            publishProgress(products);
        }
        Log.d("JSON_STRING", json_string);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();


    } catch (JSONException e) {
        e.printStackTrace();
    }

    return null;
}

@Override
protected void onProgressUpdate(Products... values) {
    arrayList.add(values[0]);
    adapter.notifyDataSetChanged();
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
}

提前谢谢你..

1 个答案:

答案 0 :(得分:0)

更明确的答案是转移这一行:

BackgroundTask backgroundTask = new BackgroundTask(getActivity());
backgroundTask.execute();

到你想要的片段中的onStart()