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

时间:2016-04-01 20:05:10

标签: android adapter android-recyclerview android-adapter notifydatasetchanged

我想实现notifyDataSetChanged或自动刷新到我的RecyclerView但它不起作用,我已经尝试了很多方法来解决这个问题,但遗憾的是所有方法都崩溃了,有些方法没有用。我是开发新手,所以,请帮我解决这个问题。 并且 想知道,这是创建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, 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
            }
        });
    }
}
}

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

2 个答案:

答案 0 :(得分:1)

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()将不再像您一样工作。

还建议您不要从notifyDataSetChanged()致电onBindViewHolder()。 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);

}

答案 1 :(得分:0)

您正试图在myNotifyDataSetChanged()个实例上致电null adapter。 您可以直接致电notifyDataSetChanged()而不是致电dataAdapter.myNotifyDataSetChanged();