¿Quieres implementar notifyDataSetChanged o autorefresh a mi RecyclerView pero no funciona

Quiero implementar notifyDataSetChanged o actualizar automáticamente a mi RecyclerView pero no funciona, he intentado muchos métodos para resolver esto, pero desafortunadamente todos los métodos terminan con fallos, algunos no funcionaron. Soy nuevo en el desarrollo por lo que, plz me ayudan a averiguar esto. Y también quieren saber, esta es la manera correcta de crear la referencia del DataAapter?

DataAdapter dataadaper; (Código completo dado abajo)

Si no entonces cómo?

Con este código (dado abajo) Mi aplicación se bloquea cuando está abierto, quiero saber qué estoy haciendo mal para poder aprender de mis errores. 🙂 Gracias

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> { public static String Camp_ID[] = new String[15]; private ArrayList<AndroidVariables> android; private Context context; AnimatorSet set; DataAdapter dataAdapter String url = "_URL"; public DataAdapter(ArrayList<AndroidVariables> android) { this.context = context; this.android = android; if (OffersFragment.srl.isRefreshing()) { OffersFragment.srl.setRefreshing(false); } } @Override public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { if (android.get(i).getCamp_ID().equals("1")) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_list_offer_big, viewGroup, false); return new ViewHolder(view); } View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_list, viewGroup, false); return new ViewHolder(view); } @Override public int getItemViewType(int i) { return i; } public void myNotifyDataSetChanged() { this.notifyDataSetChanged(); } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { Context context = viewHolder.p_img.getContext(); viewHolder.p_name.setText(android.get(i).getName()); viewHolder.p_quant.setText(android.get(i).getVer()); viewHolder.p_cat.setText(android.get(i).getApi()); viewHolder.p_earn.setText("₹" + android.get(i).getCash()); Camp_ID[i] = android.get(i).getCamp_ID(); Picasso.with(context).load(url + android.get(i).getImage()).into(viewHolder.p_img); dataAdapter.myNotifyDataSetChanged(); } @Override public int getItemCount() { return android.size(); } public class ViewHolder extends RecyclerView.ViewHolder { private TextView p_name, p_quant, p_cat, p_earn; private ImageView p_img, plus; public ViewHolder(View view) { super(view); p_name = (TextView) view.findViewById(R.id.list_product); p_quant = (TextView) view.findViewById(R.id.list_quantity); p_cat = (TextView) view.findViewById(R.id.list_category); p_earn = (TextView) view.findViewById(R.id.earning); p_img = (ImageView) view.findViewById(R.id.list_productimg); plus = (ImageView) view.findViewById(R.id.plusoffers); plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { plus.setImageResource(R.drawable.check_small); //DO_ANY_THING } }); } } } 

INICIAR SESIÓN

 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: FATAL EXCEPTION: main 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: Process: earn.free.cashback, PID: 24357 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'void earn.free.cashback.DataAdapter.myNotifyDataSetChanged()' on a null object reference 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at earn.free.cashback.DataAdapter.onBindViewHolder(DataAdapter.java:94) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at earn.free.cashback.DataAdapter.onBindViewHolder(DataAdapter.java:30) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.jav a:5453) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java: 5486) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView. java:4723) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView. java:4599) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManag er.java:1988) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.ja va:1384) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1347 ) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManag er.java:574) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:300 3) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2881) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3265) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:59 6) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.ja va:1087) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout. java:797) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java: 811) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1695) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScro llingViewBehavior.java:122) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavio r.java:42) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(A ppBarLayout.java:1149) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java: 810) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1187) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.layout(View.java:15671) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.ViewGroup.layout(ViewGroup.java:5038) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 04-02 02:17:11.820 24357-24357/? E/AndroidRuntime: at android.view.View.l 

2 Solutions collect form web for “¿Quieres implementar notifyDataSetChanged o autorefresh a mi RecyclerView pero no funciona”

La información de Shadab es correcta, aunque en lo que respecta a la inicialización, recomiendo que elimine la variable dataAdapter de su clase DataAdapter y la añada a la actividad a la que desea acceder; Inicializándolo en onCreate ().

 public void onCreate(Bundle savedInstanceState) { //Initialize data set myAndroidData = new ArrayList<>(); //Initialize data adapter and assign it to a recycler view myDataAdapter = new DataAdapter(myAndroidData); myRecyclerView = (RecyclerView) findViewById(R.id.recycler_view); myRecyclerView.setAdapter(myDataAdapter); super.onCreate(savedInstanceState); } 

Después de agregar o quitar elementos de myAndroidData, llame a myDataAdapter.notifyDataSetChanged(); Para actualizar la pantalla. Dado que notifyDataSetChanged() es una función pública en la clase primaria RecyclerView, su función myNotifyDataSetChanged() no es necesaria y puede eliminarse. Evite llamar a myAndroidData = new ArrayList<AndroidVariables>() después de crear el adaptador ya que dataAdapter.android todavía se referiría a la lista con la que lo inicializó y notifyDataSetChanged() ya no funcionaría como le gustaría.

También se recomienda no llamar a notifyDataSetChanged() de onBindViewHolder() . OnBindViewHolder sólo debe utilizarse para configurar el contenido que se muestra en los titulares de la vista y no en el propio RecyclerView.

Actualice su función onBindViewHolder() para

 @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { Context context = viewHolder.p_img.getContext(); viewHolder.p_name.setText(android.get(i).getName()); viewHolder.p_quant.setText(android.get(i).getVer()); viewHolder.p_cat.setText(android.get(i).getApi()); viewHolder.p_earn.setText("₹" + android.get(i).getCash()); Camp_ID[i] = android.get(i).getCamp_ID(); Picasso.with(context).load(url + android.get(i).getImage()).into(viewHolder.p_img); } 

Está intentando llamar a myNotifyDataSetChanged() en una instancia de null adapter . Puede llamar directamente a notifyDataSetChanged() lugar de llamar a dataAdapter.myNotifyDataSetChanged();

  • Mantener el foco en TextView después de notifyDatasetChanged () fue llamado en listview personalizado?
  • RecyclerViewAdapter.notifyDataSetChanged () en una referencia de objeto nulo
  • Cómo actualizar algunos datos en un ListView sin utilizar notifyDataSetChanged ()?
  • Hace notificydatasetchanged llamada onCreateViewHolder al usar RecyclerView
  • NotifyDataSetChange no funciona desde un adaptador personalizado
  • Android getChildView no se llama después de notifyDataSetChanged
  • NotifyDataSetChanged no puede actualizar ListView
  • Android ParseQueryAdapter notifyDataSetChanged no funciona
  • Cómo añadir sugerencias dinámicamente a autocompletetextview con preservar el estado de los caracteres
  • Cómo utilizar ListView, ArrayList, ArrayAdapter y notifyDataSetChanged junto con un runnable, y un Handler de mensaje en el hilo principal
  • Android: detecta si un ListView tiene la barra de desplazamiento (después de configurar nuevos datos)
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.