Nick Mowen
Nick Mowen

Reputation: 2622

How to add data from a database to a recycler view using a cursor adapter?

So I have tried implementing my database with a recycler view and cursoradapter, but I am getting errors. I can not understand them well so I dont know if I did not implement the adapter right or if it is something else. Here are the java files:

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    public static final String EXTRA_MESSAGE = "com.nick.mowen.receiptmanager.LOCATION";
    public String Places[];
    public RecyclerView RV;
    private RVAdapter adapter;
    Cursor mCursor;
    ManagerDatabaseAdapter managerDatabaseAdapter;
    List<MainInfo> mainInfo = new ArrayList<MainInfo>();
    RecyclerView.LayoutManager layoutManager;
    private Context activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        managerDatabaseAdapter = new ManagerDatabaseAdapter(this);
        setContentView(R.layout.activity_main);
        adapter = new RVAdapter(getActivity(), managerDatabaseAdapter.getTheCursor());
        RV = (RecyclerView) findViewById(R.id.mainV);
        layoutManager = new LinearLayoutManager(getActivity());
        RV.setLayoutManager(layoutManager);
        RV.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
      //  getMenuInflater().inflate(R.menu.menu_main, li)
        return true;
    }

    public static void getData () {

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            Intent intent = new Intent(this,SettingsActivity.class);
            intent.putExtra(EXTRA_MESSAGE,true);
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /*@Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        TextView userText= (TextView) view;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }*/

    public void addInstance(View view) {
        Intent intent = new Intent(this,LocationAdder.class);
        intent.putExtra(EXTRA_MESSAGE,true);
        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }

    public Context getActivity() {
        return activity;
    }
}

Here is the recycler view adapter:

public class RVAdapter extends Adapter<RVAdapter.MyViewHolder> {

    private LayoutInflater inflater;
    CursorAdapter mCursorAdapter;
    Context mContext;
    //Cursor cursor;
    //public Cursor mCursor;
    MainInfo mainInfo;
    ManagerDatabaseAdapter.ManagerHelper managerHelper;
    public ManagerDatabaseAdapter managerDatabaseAdapter;
   // public List<MainInfo> mainInfoList = Collections.emptyList();

    public RVAdapter (Context context, Cursor cursor) {
        mContext = context;
        mCursorAdapter = new CursorAdapter(mContext, cursor, 0) {
            @Override
            public View newView(Context context, Cursor cursor, ViewGroup parent) {
                //Cursor mCursor = getCursor();

                final LayoutInflater inflater = LayoutInflater.from(context);
                View view = inflater.inflate(R.layout.custom_row, parent, false);

                int nameCol = cursor.getColumnIndex("Codes");
                String name = cursor.getString(nameCol);

                TextView nameText = (TextView) view.findViewById(R.id.mainV);
                if (nameText != null) {
                    nameText.setText(name);
                }
                return view;
            }

            @Override
            public void bindView(View view, Context context, Cursor cursor) {
                int nameCol = cursor.getColumnIndex("Code");
                String name = cursor.getString(nameCol);
                TextView nameText = (TextView) view.findViewById(R.id.mainV);
                if (nameText != null) {
                    nameText.setText(name);
                }
            }
        };
        //this.mainInfoList = mainInfoList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int i) {
        /*MainInfo mainInfo = mainInfoList.get(i);
        holder.Title.setText(mainInfo.get_Title());
        holder.Body.setText(mainInfo.get_SubT());*/
        mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
    }

    @Override
    public int getItemCount() {
        //return mainInfoList.size();
        return mCursorAdapter.getCount();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView Title;

         MyViewHolder(View itemView) {
            super(itemView);
            Title = (TextView) itemView.findViewById(R.id.Row_Header);
            //Body = (TextView) itemView.findViewById(R.id.Row_Footer);
        }
    }
}

Here is the method to get the cursor:

public Cursor getTheCursor() {
        String[] columns = {ManagerHelper.UID,ManagerHelper.NAME,ManagerHelper.CODE};
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor mCursor = db.query(ManagerHelper.TABLE_NAME, columns, null, null, null, null, null);
        return mCursor;
    }

And finally here is the error I am getting:

Error

Upvotes: 0

Views: 1663

Answers (2)

moxi
moxi

Reputation: 1452

The problem is that in onCreate you're using the method getActivity() but you're never initializing the activity object.

Please try to change your onCreate to something like this:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);            
        managerDatabaseAdapter = new ManagerDatabaseAdapter(this);
        setContentView(R.layout.activity_main);
        adapter = new RVAdapter(this, managerDatabaseAdapter.getTheCursor());
        RV = (RecyclerView) findViewById(R.id.mainV);
        layoutManager = new LinearLayoutManager(this);
        RV.setLayoutManager(layoutManager);
        RV.setAdapter(adapter);
    }

That should fix your NPE and you can see what other parts are missing. And remove the getActivity() method, you don't need that in an Activity it is used on a Fragment, so I supposed you copied that code from a Fragment implementation.

Upvotes: 1

Muhammad Waleed
Muhammad Waleed

Reputation: 2601

If you are running a query with a CursorLoader and you want RecyclerView instead of ListView.

You can try my CursorRecyclerViewAdapter CursorAdapter in RecyclerView

And also visit this reference link link

Upvotes: 0

Related Questions