2017-11-01 2 views
0
public void serverCall(String Id, final String title, final String descrpiton) { 
    ReUseComponets serverCall = new ReUseComponets(new ShopProductlisteners() { 
     @Override 
     public void showShopProductListnerResult(List<ShopProductModel> result) { 
      if (result != null) { 
       if(adapter==null) { 
        List<ShopProductModel> shopProductModelList = result; 
        recyclerView.setHasFixedSize(true); 
        gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); 
        recyclerView.setLayoutManager(gaggeredGridLayoutManager); 
        adapter = new ShopSubCateogryAdapter(getActivity(), shopProductModelList, title, descrpiton); 
        emptytextView.setVisibility(View.GONE); 
        recyclerView.setVisibility(View.VISIBLE); 
        recyclerView.setAdapter(adapter); 
        adapter.notifyDataSetChanged(); 
       } 
      } else { 
       emptytextView.setVisibility(View.VISIBLE); 
       recyclerView.setVisibility(View.GONE); 
      } 
      progressDialog.dismiss(); 
     } 

     @Override 
     public void showError(Throwable error) { 
      progressDialog.dismiss(); 
     } 
    }); 
    serverCall.asyncPostShopProduct(Id, "en_IN"); 
} 

Das ist mein Server Anruf-Code istadapter.notifyDataSetChanged() funktioniert nicht in recyclerview in android

@Override 
public void onReceived(int requestCode, int resultCode, Intent data) { 
    Toast.makeText(getActivity(), "Click", Toast.LENGTH_LONG).show(); 
    String title = this.getArguments().getString(Constants.TITLE); 
    String id = this.getArguments().getString(Constants.ID); 
    String descrption = this.getArguments().getString(Constants.DESCRIPTION); 
    serverCall(id, title, descrption); 
    adapter.notifyDataSetChanged(); 
} 

diesen Code verwenden Ich versuche Server Aufruf zu rufen. Wenn ich von der nächsten Aktivität zum vorherigen Fragment zurückkomme, wird meine Datenliste im Adapter nicht aktualisiert. Kann mir jemand bitte vorschlagen, was mache ich falsch?

+0

Hinzufügen adapter.notifyDataSetChanged(); in serverCall() in sonst Teil –

+0

sonst Teil, wenn Daten nicht da ist, dann sollte es Recyclerview leeren –

+0

Siehe meine gegebene Antwort –

Antwort

-1

prüfen Voll-Code

package com.cognihealth.cognicare.shopproductcateogryfragment; 

     import android.app.Activity; 
     import android.app.ProgressDialog; 
     import android.content.Intent; 
     import android.os.Bundle; 
     import android.support.annotation.Nullable; 
     import android.support.v4.app.Fragment; 
     import android.support.v7.widget.RecyclerView; 
     import android.support.v7.widget.StaggeredGridLayoutManager; 
     import android.view.LayoutInflater; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.widget.TextView; 
     import android.widget.Toast; 

     import com.cognihealth.cognicare.R; 
     import com.cognihealth.cognicare.activities.ShopProductActvity; 
     import com.cognihealth.cognicare.adapters.ShopSubCateogryAdapter; 
     import com.cognihealth.cognicare.cogniutils.Constants; 
     import com.cognihealth.cognicare.cogniutils.StorageManager; 
     import com.cognihealth.cognicare.models.ShopProductModel; 
     import com.cognihealth.cognicare.models.ShopSubCateogryModel; 
     import com.cognihealth.cognicare.network.ReUseComponets; 
     import com.cognihealth.cognicare.network.listeners.DataReceivedListener; 
     import com.cognihealth.cognicare.network.listeners.OnClickofquantitylistner; 
     import com.cognihealth.cognicare.network.listeners.ShopProductlisteners; 

     import java.util.ArrayList; 
     import java.util.List; 

/** 
* Created by anil on 27/8/17. 
*/ 

public class SubCateogry1 extends Fragment implements OnClickofquantitylistner, DataReceivedListener { 
    public static final String ID = "ID"; 
    private RecyclerView recyclerView; 
    private ProgressDialog progressDialog; 
    private TextView emptytextView; 
    private StaggeredGridLayoutManager gaggeredGridLayoutManager; 
    private ShopSubCateogryAdapter adapter; 
    List<ShopProductModel> shopProductModelList = new ArrayList<ShopProductModel>(); 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.subcateogry, container, false); 
     recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView); 
     recyclerView.setHasFixedSize(true); 
     gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); 
     recyclerView.setLayoutManager(gaggeredGridLayoutManager); 
     String title = this.getArguments().getString(Constants.TITLE); 
     String id = this.getArguments().getString(Constants.ID); 
     String descrption = this.getArguments().getString(Constants.DESCRIPTION); 
     adapter = new ShopSubCateogryAdapter(getActivity(), shopProductModelList, title, descrpiton); 
     recyclerView.setAdapter(adapter); 
     emptytextView = (TextView) rootView.findViewById(R.id.emptytextView); 
     progressDialog = new ProgressDialog(getActivity(), R.style.MyActivityTheme); 
     progressDialog.setCancelable(false); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     progressDialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small); 
     ((ShopProductActvity) getActivity()).setDataReceivedListener(this); 

     serverCall(id, title, descrption); 
     return rootView; 
    } 

    public static final SubCateogry1 newInstance(String id, String title, String descrpition) { 
     SubCateogry1 f = new SubCateogry1(); 
     Bundle bdl = new Bundle(1); 
     bdl.putString(Constants.TITLE, title); 
     bdl.putString(Constants.ID, id); 
     bdl.putString(Constants.DESCRIPTION, descrpition); 
     f.setArguments(bdl); 
     return f; 
    } 


    public void serverCall(String Id, final String title, final String descrpiton) { 
     ReUseComponets serverCall = new ReUseComponets(new ShopProductlisteners() { 
      @Override 
      public void showShopProductListnerResult(List<ShopProductModel> result) { 
       if (result != null) { 
        shopProductModelList.clear(); 
        shopProductModelList.addAll(result); 
        if (!shopProductModelList.isEmpty()) { 
         emptytextView.setVisibility(View.GONE); 
         recyclerView.setVisibility(View.VISIBLE); 
        }else{ 
         emptytextView.setVisibility(View.VISIBLE); 
         recyclerView.setVisibility(View.GONE); 
        } 
        adapter.notifyDataSetChanged(); 

       } else { 

        emptytextView.setVisibility(View.VISIBLE); 
        recyclerView.setVisibility(View.GONE); 
       } 
       progressDialog.dismiss(); 
      } 

      @Override 
      public void showError(Throwable error) { 
       progressDialog.dismiss(); 
      } 
     }); 


     serverCall.asyncPostShopProduct(Id, "en_IN"); 
    } 

    @Override 
    public void onclickquantity(int position, int quantity) { 
     String title = this.getArguments().getString(Constants.TITLE); 
     String id = this.getArguments().getString(Constants.ID); 
     String descrption = this.getArguments().getString(Constants.DESCRIPTION); 
     serverCall(id, title, descrption); 
    } 

    @Override 
    public void onReceived(int requestCode, int resultCode, Intent data) { 
     Toast.makeText(getActivity(), "Click", Toast.LENGTH_LONG).show(); 
     String title = this.getArguments().getString(Constants.TITLE); 
     String id = this.getArguments().getString(Constants.ID); 
     String descrption = this.getArguments().getString(Constants.DESCRIPTION); 
     serverCall(id, title, descrption); 
    } 
} 
+0

immer noch nicht funktioniert, müssen wir Adapter-Null in onReceived setzen? @J Ramesh –

+0

@ManojKumar Check aktualisiert ans –

+0

bedeutet, es sollte global ?? Ich habe versucht, damit auch sein nicht funktioniert –

0

Wenn Ihr Adapter nicht null ist, bist du nichts zu tun. Sie müssen Ihre Liste aktualisieren und rufen Sie dann notifyDataSetChanged()

1

afaik, notifyDataSetChanged() nur innerhalb der Adapter-Klasse arbeiten. Sie können es also lösen, indem Sie eine Methode hinzufügen, um alle Objektlisten im Adapter zu ändern. So etwas wie diese (hier sind wir mit swap()-Methode):

public class ShopSubCateogryAdapter extends 
    RecyclerView.Adapter<ShopSubCateogryAdapter.ViewHolder> { 

    ... 

    private List<ShopProductModel> shopProductModelList; 

    public void swap(List<ShopProductModel> shopProductModelList) { 
    // use this. to refer your variable with class scope 
    // if your parameter has the same name with the variable class scope. 
    this.shopProductModelList.clear(); 
    this.shopProductModelList.addAll(shopProductModelList); 
    notifyDataSetChanged(); 
    } 
    .. 
} 

Dann können Sie es mit verwenden:

adapter.swap(result); 
+0

Ok lass mich das versuchen –

+0

https://paste.ofcode.org/JgnSBXt5d5cDpXB7S7qwDW, https://paste.ofcode.org/tYVjHUEbYeJUFBa8AfF3C Ich versuche mit diesem aber immer noch nicht wotking bitte helfen @ ששש אאבב אאךך –

+0

gibt es einen kleinen Fehler in Ihrem Code. Sie müssen 'this.shopProductModelList' verwenden, um auf Ihre Klassenvariable zu verweisen. –

0

aktualisieren Ihre Methode wie folgt aus:

public void serverCall(String Id, final String title, final String descrpiton) { 
    ReUseComponets serverCall = new ReUseComponets(new ShopProductlisteners() { 
     @Override 
     public void showShopProductListnerResult(List<ShopProductModel> result) { 
      if (result != null) { 
       if(adapter==null) { 
        //shopProductModelList initialize it globally 
        List<ShopProductModel> shopProductModelList = result; 
        recyclerView.setHasFixedSize(true); 
        gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); 
        recyclerView.setLayoutManager(gaggeredGridLayoutManager); 
        adapter = new ShopSubCateogryAdapter(getActivity(), shopProductModelList, title, descrpiton); 
        emptytextView.setVisibility(View.GONE); 
        recyclerView.setVisibility(View.VISIBLE); 
        recyclerView.setAdapter(adapter); 
        adapter.notifyDataSetChanged(); 
       } 
       else{ 
        // do what you want to add to your list like this: 
        // if you intialize shopProductModelList it globally then you can call it here 
        shopProductModelList.clear(); 
        shopProductModelList = result; 


        adapter.notifyDataSetChanged(); 
        } 
      } else { 
       emptytextView.setVisibility(View.VISIBLE); 
       recyclerView.setVisibility(View.GONE); 
      } 
      progressDialog.dismiss(); 
     } 

und Entfernen Sie diese Zeile von Ihrer onReceived() Methode

@Override 
public void onReceived(int requestCode, int resultCode, Intent data) { 
    Toast.makeText(getActivity(), "Click", Toast.LENGTH_LONG).show(); 
    String title = this.getArguments().getString(Constants.TITLE); 
    String id = this.getArguments().getString(Constants.ID); 
    String descrption = this.getArguments().getString(Constants.DESCRIPTION); 
    serverCall(id, title, descrption); 
    // no need this line here. 
    //adapter.notifyDataSetChanged(); 
} 
+0

Ich versuchte dies immer noch nicht arbeiten dude @ Hassan Usman –

+0

Haben Sie etwas in Ihrem Adapter hinzufügen? – HassanUsman

+0

NEIN ich habe keine Sache hinzugefügt –

Verwandte Themen