IrApp IrApp - 29 days ago 7
Android Question

Android recyclerView data binding adapter data

I am implementing a

Recycler View
and its respective
adapter
using
Android Data Binding
. The problem is that in all the tutorials I have seen, they initialize the data of the adapter with a
Collection
of
View model
class, like this:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> implements View.OnClickListener {

private ArrayList<ViewModel> items;

public RecyclerViewAdapter(ArrayList<ViewModel> items) {
this.items = items;
}

.....
}


But the data I want to pass to the
Recycler View
is a
Collection
of
records
from my
database
:

private ArrayList<Record> items;


How can I do that??

Thanks in advance!

EDIT

li_item.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>
<variable
name="report"
type="viewmodel.ReportVM"/>
</data>

....

<TextView
android:id="@+id/tv_report_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="18sp"
android:paddingBottom="6dp"
android:text="@{report.name}"
tools:text="Report 1"
/>

....


ADAPTER

public class ReportRVAdapter extends RecyclerView.Adapter<ReportRVAdapter.ReportViewHolder> {

private List<ReportDb> data;


public ReportRVAdapter(final List<ReportDb> reportData) {
this.data = reportData;
}

@Override
public ReportRVAdapter.ReportViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.li_item, parent, false);
return new ReportViewHolder(v);
}

@Override
public void onBindViewHolder(final ReportViewHolder holder, final int position) {
ReportDb item = data.get(position);

holder.getBinding().setVariable(BR.name, item.getName());
holder.getBinding().setVariable(BR.contractor, item.getContractor());

//make binding happen immediately
holder.getBinding().executePendingBindings();
}

@Override
public int getItemCount() {
return data == null ? 0 : data.size();
}


public class ReportViewHolder extends RecyclerView.ViewHolder {
private ViewDataBinding binding;

public ReportViewHolder(final View rowView) {
super(rowView);

binding = DataBindingUtil.bind(rowView);
}

public ViewDataBinding getBinding() {
return binding;
}
}
}


MODEL VIEW

public class ReportVM extends BaseObservable {

public String name;
public String contractor;

public ReportVM() {
}

@Bindable
public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}

@Bindable
public String getContractor() {
return contractor;
}

public void setContractor(final String contractor) {
this.contractor = contractor;
}
}

Answer

You can use any collection in RecyclerView, It depends on you how you bind value with the View(TextView, ImageView). Example:-

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
            // - get element from your dataset at this position
            // - replace the contents of the view with that element
            final String name = mDataset.get(position);
            holder.txtHeader.setText(mDataset.get(position));
}

in above example, it depends on you, if you use Map then you need to get value using key. For set same get method can be used.