android – 想要实现notifyDataSetChanged或autorefresh到我的RecyclerView但它无法正常工作

我想实现notifyDataSetChanged或自动刷新到我的RecyclerView,但它无法正常工作,我已经尝试了很多方法解决这个问题但不幸的是所有方法最终都崩溃,有些方法无法正常工作.我是新手,所以,PLZ帮助我解决这个问题.
并且
想知道,这是创建DataAapter引用的正确方法吗?

DataAdapter dataadaper; (完整代码如下)

如果不是那么怎么样?

使用此代码(下面给出)我的应用程序在打开时崩溃,想知道我做错了什么,这样我就可以从错误中吸取教训. :) 谢谢

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

LOG

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

解决方法

Shadab的信息是正确的,但是关于初始化,我建议从DataAdapter类中删除dataAdapter变量并将其添加到您打算访问它的活动中;在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);
}

在myAndroidData中添加删除项目后,请调用myDataAdapter.notifyDataSetChanged();刷新你的显示器.
由于notifyDataSetChanged()是父级RecyclerView类中的公共函数,因此不需要myNotifyDataSetChanged()函数,可以将其删除.避免在创建适配器后调用myAndroidData = new ArrayList< AndroidVariables>(),因为dataAdapter.android仍会引用您初始化它的列表,而notifyDataSetChanged()将不再按您的意愿工作.

还建议您不要从onBindViewHolder()调用notifyDataSetChanged(). onBindViewHolder只应用于设置视图持有者中显示内容,而不能用于设置RecyclerView本身.

将onBindViewHolder()函数更新为

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

}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...