I am beginner and I do not know how to populate and set up RecyclerView with Sugar database. I would appreciate yours help. Thank a lot. I have tried some tutorials but they did not work for me.
My Fragment
public class ContactListFragment extends Fragment {
protected RecyclerView rvRecyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
public static ContactListFragment newInstance() {
Bundle args = new Bundle();
ContactListFragment fragment = new ContactListFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contact_list, container, false);
//recycler view setup
rvRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
//initialize recycler view here
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
//show actionBar
((MainActivity) getActivity()).getSupportActionBar().show();
//show title
((MainActivity) getActivity()).getSupportActionBar().setTitle(R.string.app_name);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
}
And ContactsAdapter (RecyclerAdapter)
public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder> {
ArrayList<Contact> arrayList = new ArrayList<>();
static Context context;
ContactsAdapter(Context context, ArrayList<Contact> arrayList) {
this.arrayList = arrayList;
this.context = context;
}
@Override
public ContactsViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
ContactsViewHolder contactsViewHolder = new ContactsViewHolder(view);
return contactsViewHolder;
}
@Override
public void onBindViewHolder(ContactsViewHolder holder, int position) {
Contact contact = arrayList.get(position);
holder.Name.setText(contact.getName());
}
@Override
public int getItemCount() {
return arrayList.size();
}
public static class ContactsViewHolder extends RecyclerView.ViewHolder {
TextView Name;
ContactsViewHolder(View view) {
super(view);
Name = (TextView)view.findViewById(R.id.contact_name);
}
}
}
Aucun commentaire:
Enregistrer un commentaire