0

Mein Code ist eigentlich von einem Recyclerview in einer Aktivität und jetzt möchte ich es auf einem Fragment implementieren..aber wenn es Hauptfragment startet die App abstürzt .. ist es möglich, dies mit der gleichen Art von Code zu tun?Ich möchte meine Recyclerview in einem Fragment mit asynctask implementieren

Haupt Fragment

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

danken Ihnen im Voraus ..

+0

ich löste mein Problem, indem ich die Hintergrundaufgabe auf onStart() stellte ... danke, Problem bereits gelöst – Franzen

Antwort

0

Je klarer Antwort ist diese Zeilen übertragen:

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

zu onStart() in y unser gewünschtes Fragment