2016-12-22 4 views
0

Ich habe Fragment mit Asynctask-Klasse, die die Asynctask aus meiner Recyclerview-Adapter-Klasse erneut ausführen muss.Wie kann ich Asynctask innerhalb Fragment von recyclerview erneut starten

Hinweis: Ich möchte wieder AsyncTask laufen innerhalb von viewHolder.buttonnext.setOnClickListener(new View.OnClickListener() { in onBindViewHolder und wenn diese Linie verwenden.

new maghalat.GetContacts().execute(); //maghalt is name of fragment contain GetContacts asynctask function

Gib mir rote Linie unter dieser Linie ohne Vorschlag

das ist mein Adapter:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> { 
    private Context context; 
    List<jsonContent> jcontent; 



    public DataAdapter(Context context, List<jsonContent> jcontent) { 

     this.context=context; 
     this.jcontent=jcontent; 

    } 



    @Override 
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 

     View view ; 
     if(i == R.layout.card_row) { 
      view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false); 
     }else { 
      view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.button_card, viewGroup, false); 
     } 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(final ViewHolder viewHolder,int i) { 

     if(i == jcontent.size()) { 

      viewHolder.buttonnext.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

     //try This 

          ((maghalat)context).asyncRun(); 


       } 
      }); 
      viewHolder.buttonprev.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        Toast.makeText(context, "pre", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      viewHolder.go.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

       } 
      }); 

      viewHolder.cardView.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

       } 
      }); 

      viewHolder.pages.setText(Integer.toString(jcontent.get(i-1).pages)); 


     } 
     else { 

      viewHolder.title.setText(jcontent.get(i).title); 

      Picasso.with(context).load(jcontent.get(i).imgurl).resize(300, 400).into(viewHolder.imageView); 

     } 
    } 



    @Override 
    public int getItemCount() { 
      return jcontent.size()+1; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return (position == jcontent.size()) ? R.layout.button_card : R.layout.card_row; 
    } 


    public class ViewHolder extends RecyclerView.ViewHolder{ 

     private TextView title,pages; 

     private ImageView imageView; 

     private Button buttonnext,buttonprev,go; 

     private CardView cardView; 

     private EditText editText; 



     public ViewHolder(final View view) { 
      super(view); 



      title = (TextView)view.findViewById(R.id.post_title); 
      imageView=(ImageView)view.findViewById(R.id.img); 

      buttonnext =(Button)view.findViewById(R.id.next); 
      buttonprev=(Button)view.findViewById(R.id.prev); 
      go=(Button)view.findViewById(R.id.go); 

      editText=(EditText)view.findViewById(R.id.et_go_page); 

      cardView=(CardView)view.findViewById(R.id.cvv); 

      pages=(TextView)view.findViewById(R.id.number_pages); 

     } 
    } 
} 

dies mein Fragment ist:

public class maghalat extends Fragment { 

    private RecyclerView recyclerView; 
    private DataAdapter adapter; 
    private View myFragmentView; 

    private String TAG = MainActivity.class.getSimpleName(); 
    private ProgressDialog pDialog; 

    private int numpage=0; 

    public int sag; 

    private String url = "http://memaraneha.ir/category/%d9%85%d9%82%d8%a7%d9%84%d8%a7%d8%aa/page/"+String.valueOf(sag)+"/?json=get_posts"; 

    List<jsonContent> listcontent=new ArrayList<>(); 



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


     if(isNetworkConnected()) { 
      asyncRun(); 
     }else 
     { 
      Toast.makeText(getActivity().getApplicationContext(), "دستگاه شما به اینترنت متصل نیست!", Toast.LENGTH_LONG).show(); 
     } 
     return myFragmentView; 
    } 


    public void asyncRun(){ 
      new GetContacts().execute(); 
      } 

      public class GetContacts extends AsyncTask<Void, Void, Void> { 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        // Showing progress dialog 
        pDialog = new ProgressDialog(getActivity()); 
        pDialog.setMessage("Please wait..."); 
        pDialog.setCancelable(false); 
        pDialog.show(); 
       } 

       @Override 
       protected Void doInBackground(Void... arg0) { 
        HttpHandler sh = new HttpHandler(); 

        sag=numpage+1; 
        // Making a request to url and getting response 
        String jsonStr = sh.makeServiceCall(url); 

        Log.e(TAG, "Response from url: " + jsonStr); 

        if (jsonStr != null) { 
         try { 

          JSONObject jsonObj = new JSONObject(jsonStr); 


          int id=jsonObj.getInt("pages"); 


          JSONArray posts = jsonObj.getJSONArray("posts"); 
          for (int i = 0; i < posts.length(); i++) { 
           JSONObject c = posts.getJSONObject(i); 

           jsonContent jsonContent=new jsonContent(); 

           jsonContent.title=c.getString("title"); 

           //img 
           JSONObject post_img=c.getJSONObject("thumbnail_images"); 
           for (int j=0;j<post_img.length();j++) 
           { 
            JSONObject v=post_img.getJSONObject("mom-portfolio-two"); 
            jsonContent.imgurl=v.getString("url"); 
           } 
           jsonContent.pages=id; 
           listcontent.add(jsonContent); 

          } 




         } catch (final JSONException e) { 
          Log.e(TAG, "Json parsing error: " + e.getMessage()); 
          getActivity().runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            Toast.makeText(getActivity().getApplicationContext(), 
              "Json parsing error: " + e.getMessage(), 
              Toast.LENGTH_LONG) 
              .show(); 
           } 
          }); 
         } 
        } else { 
         Log.e(TAG, "Couldn't get json from server."); 
         getActivity().runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           Toast.makeText(getActivity().getApplicationContext(), 
             "Couldn't get json from server. Check LogCat for possible errors!", 
             Toast.LENGTH_LONG) 
             .show(); 
          } 
         }); 
        } 
        return null; 
       } 

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

        pDialog.dismiss(); 

        recyclerView=(RecyclerView)myFragmentView.findViewById(R.id.recycler_view); 
        recyclerView.setHasFixedSize(true); 
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); 
        recyclerView.setLayoutManager(layoutManager); 
        adapter=new DataAdapter(getActivity(),listcontent); 
        recyclerView.setAdapter(adapter); 
       } 
      } 


    private boolean isNetworkConnected() { 
     ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo ni = cm.getActiveNetworkInfo(); 
     if (ni == null) { 
      // There are no active networks. 
      return false; 
     } else 
      return true; 
    } 
} 

enter image description here

+0

Zeigen Sie Ihren Code. – Kunu

+0

@kunu ist egal? ich definiere mein Problem klar – erfan

+0

@erfan http://stackoverflow.com/questions/26779273/call-asynctask-in-the-parent-activity-from-fragment http://stackoverflow.com/questions/26822697/ Call-Inner-Asynctask-From-Outside-Fragment –

Antwort

1
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> { 
private Context context; 
List<jsonContent> jcontent; 
private maghalat fragment; 



public DataAdapter(Context context, List<jsonContent> jcontent, maghalat frag) { 

    this.context=context; 
    this.jcontent=jcontent; 
    this.fragment = frag; 
} 



@Override 
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 

    View view ; 
    if(i == R.layout.card_row) { 
     view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false); 
    }else { 
     view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.button_card, viewGroup, false); 
    } 
    return new ViewHolder(view); 
} 

@Override 
public void onBindViewHolder(final ViewHolder viewHolder,int i) { 

    if(i == jcontent.size()) { 

     viewHolder.buttonnext.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

    //try This 

         (fragment).asyncRun(); 


      } 
     }); 
     viewHolder.buttonprev.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(context, "pre", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
     viewHolder.go.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

     viewHolder.cardView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

      } 
     }); 

     viewHolder.pages.setText(Integer.toString(jcontent.get(i-1).pages)); 


    } 
    else { 

     viewHolder.title.setText(jcontent.get(i).title); 

     Picasso.with(context).load(jcontent.get(i).imgurl).resize(300, 400).into(viewHolder.imageView); 

    } 
} 



@Override 
public int getItemCount() { 
     return jcontent.size()+1; 
} 

@Override 
public int getItemViewType(int position) { 
    return (position == jcontent.size()) ? R.layout.button_card : R.layout.card_row; 
} 


public class ViewHolder extends RecyclerView.ViewHolder{ 

    private TextView title,pages; 

    private ImageView imageView; 

    private Button buttonnext,buttonprev,go; 

    private CardView cardView; 

    private EditText editText; 



    public ViewHolder(final View view) { 
     super(view); 



     title = (TextView)view.findViewById(R.id.post_title); 
     imageView=(ImageView)view.findViewById(R.id.img); 

     buttonnext =(Button)view.findViewById(R.id.next); 
     buttonprev=(Button)view.findViewById(R.id.prev); 
     go=(Button)view.findViewById(R.id.go); 

     editText=(EditText)view.findViewById(R.id.et_go_page); 

     cardView=(CardView)view.findViewById(R.id.cvv); 

     pages=(TextView)view.findViewById(R.id.number_pages); 

    } 
} 
} 

Ihr Fragment

public class maghalat extends Fragment { 

private RecyclerView recyclerView; 
private DataAdapter adapter; 
private View myFragmentView; 

private String TAG = MainActivity.class.getSimpleName(); 
private ProgressDialog pDialog; 

private int numpage=0; 

public int sag; 

private String url = "http://memaraneha.ir/category/%d9%85%d9%82%d8%a7%d9%84%d8%a7%d8%aa/page/"+String.valueOf(sag)+"/?json=get_posts"; 

List<jsonContent> listcontent=new ArrayList<>(); 



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


    if(isNetworkConnected()) { 
     asyncRun(); 
    }else 
    { 
     Toast.makeText(getActivity().getApplicationContext(), "دستگاه شما به اینترنت متصل نیست!", Toast.LENGTH_LONG).show(); 
    } 
    return myFragmentView; 
} 


public void asyncRun(){ 
     new GetContacts().execute(); 
     } 

     public class GetContacts extends AsyncTask<Void, Void, Void> { 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       // Showing progress dialog 
       pDialog = new ProgressDialog(getActivity()); 
       pDialog.setMessage("Please wait..."); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 

      @Override 
      protected Void doInBackground(Void... arg0) { 
       HttpHandler sh = new HttpHandler(); 

       sag=numpage+1; 
       // Making a request to url and getting response 
       String jsonStr = sh.makeServiceCall(url); 

       Log.e(TAG, "Response from url: " + jsonStr); 

       if (jsonStr != null) { 
        try { 

         JSONObject jsonObj = new JSONObject(jsonStr); 


         int id=jsonObj.getInt("pages"); 


         JSONArray posts = jsonObj.getJSONArray("posts"); 
         for (int i = 0; i < posts.length(); i++) { 
          JSONObject c = posts.getJSONObject(i); 

          jsonContent jsonContent=new jsonContent(); 

          jsonContent.title=c.getString("title"); 

          //img 
          JSONObject post_img=c.getJSONObject("thumbnail_images"); 
          for (int j=0;j<post_img.length();j++) 
          { 
           JSONObject v=post_img.getJSONObject("mom-portfolio-two"); 
           jsonContent.imgurl=v.getString("url"); 
          } 
          jsonContent.pages=id; 
          listcontent.add(jsonContent); 

         } 




        } catch (final JSONException e) { 
         Log.e(TAG, "Json parsing error: " + e.getMessage()); 
         getActivity().runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           Toast.makeText(getActivity().getApplicationContext(), 
             "Json parsing error: " + e.getMessage(), 
             Toast.LENGTH_LONG) 
             .show(); 
          } 
         }); 
        } 
       } else { 
        Log.e(TAG, "Couldn't get json from server."); 
        getActivity().runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getActivity().getApplicationContext(), 
            "Couldn't get json from server. Check LogCat for possible errors!", 
            Toast.LENGTH_LONG) 
            .show(); 
         } 
        }); 
       } 
       return null; 
      } 

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

       pDialog.dismiss(); 

       recyclerView=(RecyclerView)myFragmentView.findViewById(R.id.recycler_view); 
       recyclerView.setHasFixedSize(true); 
       RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); 
       recyclerView.setLayoutManager(layoutManager); 
       adapter=new DataAdapter(getActivity(),listcontent,maghalat.this); 
       recyclerView.setAdapter(adapter); 
      } 
     } 


private boolean isNetworkConnected() { 
    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo ni = cm.getActiveNetworkInfo(); 
    if (ni == null) { 
     // There are no active networks. 
     return false; 
    } else 
     return true; 
} 
} 
+0

tun, dass aber Fehler Lookat mein Fehler Bild Ich bearbeite meine Frage – erfan

+0

@erfan jetzt versuchen. Ich machte wenige Änderungen –

+0

geben Fehler von dieser Linie: geben rote Linie von Maghalat (Ausdruck erwartet) in Adapter = neuer DataAdapter (getActivity(), listcontent, maghalat); – erfan

Verwandte Themen