dimanche 31 janvier 2016

Load SQLite Table Items into a ReyclerView

I am trying to load some items from a table into a RecyclerView on my Android application. Some of the methods cause my application to crash and I have struggled to find a solution.

Here is my code for my function that sets the items:

public void loadItems() {
    List<ListsRecyclerViewList> list = Arrays.asList(new ListsRecyclerViewList("item", "item"));
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview_activity_lists_view_all_lists);
    LinearLayoutManager linearLayoutManager = new    LinearLayoutManager(this);
    ListsRecyclerViewListAdapter adapter = new ListsRecyclerViewListAdapter(list);

    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(adapter);
}

Here is my adapter:

List<ListsRecyclerViewList> list;

public ListsRecyclerViewListAdapter(List<ListsRecyclerViewList> list) {
    this.list = list;
}

@Override
public ListsRecyclerViewListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recyclerview_view_list, parent, false);
    ListsRecyclerViewListViewHolder viewHolder = new ListsRecyclerViewListViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(ListsRecyclerViewListViewHolder holder, int position) {
    holder.name.setText(list.get(position).getName());
    holder.date.setText(list.get(position).getDate());
}

@Override
public int getItemCount() {
    return list.size();
}

public static class ListsRecyclerViewListViewHolder extends RecyclerView.ViewHolder {
    TextView name;
    TextView date;

    public ListsRecyclerViewListViewHolder(View view) {
        super(view);

        this.name = (TextView) view.findViewById(R.id.layout_recyclerview_view_list_name);
        this.date = (TextView) view.findViewById(R.id.layout_recyclerview_view_list_date);
    }
}

Also, this is the code for the RecyclerView items:

public String name;
public String date;

public ListsRecyclerViewList(String name, String date) {
    this.name = name;
    this.date = date;
}

public String getName() {
    return name;
}

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

public String getDate() {
    return name;
}

public void setDate(String date) {
    this.date = date;
}

And finally, this is what I am trying to implement but throws errors:

public void loadItems() {
    List<ListsRecyclerViewList> list = Arrays.asList(new ListsRecyclerViewList("item", "item"));
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview_activity_lists_view_all_lists);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    ListsRecyclerViewListAdapter adapter = new ListsRecyclerViewListAdapter(list);

    ListsDatabaseClass database = new ListsDatabaseClass(this);
    database.open();

    Cursor cursor = database.loadLists();
    list.add(new ListsRecyclerViewList(cursor.getString(1), cursor.getString(2)));
    database.close();

    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setAdapter(adapter);
}

Thanks for any suggestions.

Aucun commentaire:

Enregistrer un commentaire