Android: Unable to get document ID while using RecyclerView

#1

All of the existing code examples utilize ListView and arrayAdapters for presenting the data and getting a reference to the document via QueryRow.

I would like to do something like what is being used in the GrocerySync example:

QueryRow row = (QueryRow) adapterView.getItemAtPosition(position);
    final Document clickedDocument = row.getDocument();
    String itemText = (String) clickedDocument.getCurrentRevision().getProperty("text");

I have also reviewed the QueryRow documentation and there appears to be a function GetDocument:

      public String getDocumentId()

but I am a loss on how to access this method and get the resulting docID so I can update my rows recyclerView with data from the database. Here is the code fragment I am working in:

 public class MainListFragment extends Fragment {

protected MyApplication app;

private RecyclerView mRecyclerView;


private Database getDatabase() {
    app = (MyApplication) getActivity().getApplication();
    return app.getDatabase();
}

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

    View v = inflater.inflate(R.layout.fragment_list_main, container, false);
    mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.setAdapter(new MyAdapter());

    return v;
}

   public class MyHolder extends ViewHolder {

    TextView tv;

    public MyHolder(View itemView) {
        super(itemView);

        tv = (TextView) itemView.findViewById(R.id.email_task_title);

    }
}

private class MyAdapter extends RecyclerView.Adapter<MyHolder> {

    private static final int EMPTY_VIEW = 10;

    private class myRefreshTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            notifyDataSetChanged();
            super.onPostExecute(aVoid);
        }
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int pos) {

        View view;
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.email_list_item, parent, false);
        return new MyHolder(view);
    }
 
    @Override
    public void onBindViewHolder(MyHolder holder, int pos) {

        Document row = getDatabase().getDocument(String.valueOf(getId()));
        String id = row.getId();

        new myRefreshTask().execute();
        if (getItemCount() == 0) {
            holder.tv.setText("No tasks scheduled.");
        }
        holder.tv.setText(id);
    }

    @Override
    public int getItemViewType(int position) {
        if (getDatabase().getDocumentCount() == 0) {
                return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public int getItemCount() {
        return getDatabase().getDocumentCount();
    }

    @Override
    public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        super.registerAdapterDataObserver(observer);
        notifyDataSetChanged();
    }

    @Override
    public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        super.unregisterAdapterDataObserver(observer);
    }


}

}
#2

I think it may be that the ArrayAdapter and RecyclerView adapter have different init methods.

Here’s an example of a recycler view with a live query https://medium.com/@jamiltz/a-look-at-material-design-and-couchbase-lite-on-android-c07d95466972

The RecyclerView adapter is in FeedAdapter.java https://github.com/Jamiltz/InstaMaterial-Couchbase-Lite/blob/master/app/src/main/java/android/couchbaselabs/com/instamaterial/FeedAdapter.java

1 Like
#3

Thank you kindly sir! :+1: This is exactly what I was looking for. I didn’t think anybody would have any code examples illustrating how to do this.

Thanks again!