0

J'utilise complètement des fragments dans mon application.Premièrement une activité est chargée, puis des fragments viennent sur cette activité et ensuite détruire.Mon problème ici, je suis en train de charger support mapfragment à l'intérieur de l'adaptateur ListView lorsque vous cliquez dessus sur un bouton à l'intérieur élément de la liste, il fonctionne très bien pour la première fois, mais la deuxième foisFragment de mappage de carte à l'intérieur de l'élément d'élément de liste retournant la deuxième fois

 gmap = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap(); 

cette ligne est de retour nulle dans mon adapter.what dois-je faire pour remédier à ce problème. J'appelle fragment de carte à l'intérieur de l'adaptateur comme ceci dans la méthode oncreateviewholder de mon adaptateur.

ProductListAdapter public class étend RecyclerView.Adapter {

private Activity context; 
List<ListDetails> products; 
SharedPreference sharedPreference; 
public static com.android.volley.toolbox.ImageLoader imageLoader; 
LayoutInflater inflater; 
ViewHolder holder; 
protected String Day = ""; 
public static View alertLayout; 
private static GMapV2Direction gmapdirection; 
public static GoogleMap gmap; 
protected Document doc; 

public ProductListAdapter(Context context, List<ListDetails> products) { 

     this.context = (Activity)context; 
     this.products = products; 
     sharedPreference = new SharedPreference(); 
     imageLoader = AppController.getInstance().getImageLoader(); 

     inflater = (LayoutInflater) context 
       .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

    } 


@Override 
public long getItemId(int position) { 
    return position; 
} 



public static class ContactViewHolder extends RecyclerView.ViewHolder { 
    TextView productNameTxt; 
    TextView Ratingsno; 
    TextView productPriceTxt; 
    TextView timedist; 
    TableLayout distancesection; 
    TextView categoryname; 
    ImageView favoriteImg; 
    RatingBar rb; 

    ImageView OfferImage; 
    ImageView MapImage; 

    NetworkImageView listimage; 
    public TextView distance; 
    public ImageView indicator; 
    TextView timer; 
    TextView Postedon; 




     public ContactViewHolder(View v) { 

     super(v); 

     productNameTxt = (TextView) v 
       .findViewById(R.id.name); 
     productPriceTxt = (TextView) v 
       .findViewById(R.id.listview_description); 
     distance = (TextView) v 
       .findViewById(R.id.listview_distane); 
     timedist = (TextView) v 
       .findViewById(R.id.timedist); 
     Ratingsno = (TextView) v 
       .findViewById(R.id.Ratingsno); 
     Postedon = (TextView) v 
       .findViewById(R.id.Postedon); 
     favoriteImg = (ImageView) v 
       .findViewById(R.id.fav_checkbox); 

     rb = (RatingBar) v 
       .findViewById(R.id.ratingbar); 
     timer = (TextView) v.findViewById(R.id.time); 
     categoryname = (TextView) v 
       .findViewById(R.id.Categoryname); 
     listimage = (NetworkImageView) v 
       .findViewById(R.id.list_image); 
     distancesection = (TableLayout) v 
       .findViewById(R.id.distancesection); 


     MapImage = (ImageView) v 
       .findViewById(R.id.imgmap); 

     } 
    } 

@Override 
public int getItemCount() { 
    return products.size(); 
} 

@Override 
public void onBindViewHolder(ContactViewHolder contactViewHolder, final int position) { 

    try { 


     String text = "<font color=#222222>"+products.get(position).getShopName()+"</font>"+"<small><font color=#0091EA> (</font></small>"+" <small><font color=#0091EA>"+products.get(position).getAreaName()+"</font></small>"+"<small><font color=#0091EA>) </font></small>"; 
     contactViewHolder.productNameTxt.setText(Html.fromHtml(text)); 

     //holder.productNameTxt.setText(product.getShopName() + " " + "(" + Html.fromHtml("<font color='#0091EA'>" + product.getAreaName() + "</font>") + ")"); 
     contactViewHolder.productPriceTxt.setText(products.get(position).getDeal() + ""); 
     contactViewHolder.rb.setRating(Float.valueOf(products.get(position).getRatingAvg())); 
     contactViewHolder.Ratingsno.setText(products.get(position).getTotalRatings() + " Ratings"); 
     contactViewHolder.timedist.setText(products.get(position).getTime()); 
     contactViewHolder.distance.setText(String.valueOf(products.get(position).getDistance())+ " Km"); 
     contactViewHolder.categoryname.setText(products.get(position).getCategoryName()); 
     contactViewHolder.listimage.setImageUrl(products.get(position).getListImage(), imageLoader); 


     contactViewHolder.MapImage.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       try { 

        final Dialog dialog = new Dialog(context); 

        if (alertLayout != null) { 
         ViewGroup parentViewGroup = (ViewGroup) alertLayout 
           .getParent(); 
         if (parentViewGroup != null) { 
          parentViewGroup.removeView(alertLayout); 
         } 
        } 

         //dialog.setContentView(R.layout.list_map_dialog); 

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        dialog.setContentView(alertLayout); 

         // dialog.setTitle("Map Directions"); 




         ImageView cancel = (ImageView) dialog 
           .findViewById(R.id.cancel); 


         cancel.setOnClickListener(new View.OnClickListener() { 

          @Override 
          public void onClick(View arg0) { 
           dialog.cancel(); 
          } 

         }); 

         dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 

         dialog.show(); 

         LoadPlaceDetails(products.get(position).getLat(),products.get(position).getLng(),products.get(position).getAreaName(),products.get(position).getCategoryName()); 
        } catch (Exception e) { 
         e.printStackTrace();    
         PostLogcatErrors ple = new PostLogcatErrors(); 
        ple.PostLogcatErorrs(e); 
       } 
      } 
     }); 

     if (checkFavoriteItem(products.get(position))) { 
      contactViewHolder.favoriteImg.setImageResource(R.drawable.checked); 
      contactViewHolder.favoriteImg.setTag(context.getString(R.string.red_favcolor)); 
     } else { 
      contactViewHolder.favoriteImg.setImageResource(R.drawable.unchecked); 
      contactViewHolder.favoriteImg.setTag(context.getString(R.string.grey_favcolor)); 
     } 
     contactViewHolder.Postedon.setText("Posted On" + " " + products.get(position).getPostedDate()); 

     contactViewHolder.timer.setText(products.get(position).getOfferEndTime()); 



    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 



@Override 
public ContactViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) { 
    View itemView = LayoutInflater. 
      from(arg0.getContext()). 
      inflate(R.layout.list_item4, arg0, false); 

    LayoutInflater li = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 



    //Here i am adding Map layout 


    if (alertLayout == null) 
      alertLayout = li.inflate(R.layout.list_map_dialog, null); 





return new ContactViewHolder(itemView); 
} 

public boolean checkFavoriteItem(ListDetails checkProduct) { 
    boolean check = false; 
    List<ListDetails> favorites = sharedPreference.getFavorites(context); 
    if (favorites != null) { 
     for (ListDetails product : favorites) { 
      if (product.equals(checkProduct)) { 
       check = true; 
       break; 
      } 
     } 
    } 
    return check; 
} 

private void LoadPlaceDetails(double destlat, double destlng, String areaname,String Category) { 
    try { 
     LatLng fromPosition = new LatLng(SplashScreen.nwLocation.getLatitude(), SplashScreen.nwLocation.getLongitude()); 

     LatLng toPosition = new LatLng(destlat, destlng); 

     gmapdirection = new GMapV2Direction(); 

     FragmentManager fm = ((FragmentActivity) context) 
       .getSupportFragmentManager(); 



     //Here i am loading map after click on Map button 


     gmap = ((SupportMapFragment) fm.findFragmentById(R.id.map)).getMap(); 




     gmap.clear(); 
     gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(fromPosition,13)); 

     gmap.addMarker(new MarkerOptions() 
       .position(fromPosition) 
       .title("Current Location") 
       .icon(BitmapDescriptorFactory 
         .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))).showInfoWindow();; 

         switch (Category) { 
         case "book_store": 
          gmap.addMarker(new MarkerOptions() 
           .position(toPosition) 
           .title(areaname) 
           .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_book, "")))); 
          break; 
         case "beauty_salon": 
          gmap.addMarker(new MarkerOptions() 
           .position(toPosition) 
           .title(areaname) 
           .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_fashion, "")))); 
          break; 
         case "electronics_store": 
          gmap.addMarker(new MarkerOptions() 
           .position(toPosition) 
           .title(areaname) 
           .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_electronic, "")))); 
          break; 
         case "food": 
          gmap.addMarker(new MarkerOptions() 
           .position(toPosition) 
           .title(areaname) 
           .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_food, "")))); 
          break; 
         case "home_goods_store": 
          gmap.addMarker(new MarkerOptions() 
           .position(toPosition) 
           .title(areaname) 
           .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_home, "")))); 
          break; 
         case "restaurant": 
          gmap.addMarker(new MarkerOptions() 
           .position(toPosition) 
           .title(areaname) 
           .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_job, "")))); 
          break; 
         case "real_estate_agency": 
          gmap.addMarker(new MarkerOptions() 
           .position(toPosition) 
           .title(areaname) 
           .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_realestate, "")))); 
          break; 
         case "travel_agency": 
          gmap.addMarker(new MarkerOptions() 
           .position(toPosition) 
           .title(areaname) 
           .icon(BitmapDescriptorFactory.fromBitmap(writeTextOnDrawable(R.drawable.ic_vehicle, "")))); 
          break; 
        } 



     Document doc = gplaces.getDocument(fromPosition, toPosition, 
       GMapV2Direction.MODE_DRIVING,gmapdirection,gmap, context); 

    } catch (NumberFormatException e) { 
     e.printStackTrace();    
     PostLogcatErrors ple = new PostLogcatErrors(); 
     ple.PostLogcatErorrs(e); 
    } 

} 

}

que je fais quelque chose de mal ici? me suggérer là-dessus .. merci d'anticipation

Répondre

0

Probablement vous êtes en train de supprimer SupportMapFragment à l'intérieur R.id.map. Mais vous nous montrez trop peu de code pour savoir ce qui ne va pas.

+0

S'il vous plaît voir mon code édité @maciej –

+0

Maintenant, je ne peux sugest changer ((FragmentActivity) contexte) .getSupportFragmentManager(); à (contexte (FragmentActivity)) .getChildFragmentManager(); – maciej

+0

Il ne fonctionne pas dans mon cas @maciej –