在 recyclerView 中显示来自 SQLite DB Android 的 ArrayList

Display ArrayList from SQLite DB Android in recyclerView

当我尝试在 recyclerview 中显示 ArrayList 时,我有 NullPointerExeption,我检查了在日志中显示 Arraylist 并且效果很好,我认为我的错误是在 RecyclerViewAdapter。所以请帮我找出我的错误并解释

我在数据库中创建的方法Arraylist:

public List<Cards> getData(){

    List<Cards> list = new ArrayList<>();
    SQLiteDatabase sqLiteDatabase= helper.getWritableDatabase();
    Cursor cursor= sqLiteDatabase.rawQuery(helper.QUERY,null);
    while (cursor.moveToNext()){

        int index0=cursor.getColumnIndex(helper.UID);

        int index1=cursor.getColumnIndex(helper.KEY_NAME);
        int index2=cursor.getColumnIndex(helper.KEY_VALUE);
        int index3=cursor.getColumnIndex(helper.KEY_FORMAT);
        int index4=cursor.getColumnIndex(helper.KEY_COUNTRY);

        int cid = cursor.getInt(index0);
        String name = cursor.getString(index1);
        String value = cursor.getString(index2);
        String format = cursor.getString(index3);
        String country = cursor.getString(index4);
        Cards cards = new Cards(cid,name,value,format,country);

        list.add(cards);

    }

    return list;

}

卡片是我的 getter & setter class,

RecyclerViewAdapter:

public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.MyViewHolder> {
private static final String TAG = "recyclerViewAdapter";


private List<Cards> items;
private int itemLayout;

public RecycleViewAdapter(List<Cards> items, int itemLayout) {

    this.items = items;
    this.itemLayout= itemLayout;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

   View view=LayoutInflater.from(parent.getContext()).inflate(itemLayout,parent,false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {


    Cards item=items.get(position);
    holder.title.setText(item.getName());

}

@Override
public int getItemCount() {

    return items.size();
}
public static class  MyViewHolder extends RecyclerView.ViewHolder{

    TextView title;
    public MyViewHolder(View itemView) {

        super(itemView);

        title= (TextView) itemView.findViewById(R.id.listText);
    }
}




`MyCardsFragment` , where i have `recyclerView`:

    public class MyCardsFragment extends Fragment {
    public DataBaseAdapter data;
    private static final String TAG = "MyApp";

    private RecyclerView recyclerView;
    DataBaseAdapter dataBaseHelper;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my_cards, container, false);


        recyclerView = (RecyclerView) view.findViewById(R.id.recycleView);

        recyclerView.setHasFixedSize(true);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.setAdapter(new RecycleViewAdapter(dataBaseHelper.getData(),R.layout.custom_row));
        return view;

    }
}

NullPointerExeption

recyclerView.setAdapter(newrecycleViewAdapter(dataBaseHelper.getData(),R.layout.custom_row));

在使用从 DataBaseAdapter 调用 getData() 方法之前忘记初始化 dataBaseHelper class:

dataBaseHelper=new DataBaseAdapter(...); //<<< initialize here
    recyclerView.setAdapter(new RecycleViewAdapter(
               dataBaseHelper.getData(),R.layout.custom_row));