Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently working on a task app. I have saved data like task name, time and priority from the user into the room. Now, I created a custom adapter and a custom list view. Now I want to connect the data from the room so that I can put it into the custom list view. How can I do that? This is the code for the adapter class :

Java
<pre>package com.example.taskmasterv3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.Insert;

import java.util.ArrayList;


public class SummaryListAdapter extends ArrayAdapter<SummaryList> {


    private final Context context;
    private ArrayList<SummaryList> values;

    public SummaryListAdapter(Context context, ArrayList<SummaryList> list) {

        super(context, 0, list);
        this.context = context;
        this.values = list;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.task_summary_item, parent, false);
        }

        TextView tvloltaskname = convertView.findViewById(R.id.tvloltaskname);
        ImageView ivlolPri = convertView.findViewById(R.id.ivlolPri);
        ImageView ivlolTime = convertView.findViewById(R.id.ivlolTime);


        // code to connect Room to this


        AppDatabase.databaseWriteExecutor.execute(new Runnable() {
            @Override
            public void run() {
                AppDatabase.getDatabase(context).userDao().getAll();



            }
        });






        return convertView;
    }

}


Code for custom list item :

XML
<pre><?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_margin="16dp"
    android:background="@color/white"
    android:clickable="false"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tvloltaskname"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="3"
        android:fontFamily="@font/roboto"
        android:gravity="center_horizontal"
        android:text="subtask_name"
        android:textColor="@color/black"
        android:textSize="15sp" />

    <ImageView
        android:id="@+id/ivlolPri"
        android:layout_width="0dp"
        android:layout_height="32dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        app:srcCompat="@drawable/priority_high" />

    <ImageView
        android:id="@+id/ivlolTime"
        android:layout_width="0dp"
        android:layout_height="32dp"
        android:layout_gravity="center"
        android:layout_weight="1"
        app:srcCompat="@drawable/time_symbol_more" />
</LinearLayout>


What I have tried:

I have never done this before, this is my first time. So please pardon me if I made a silly mistake.
Posted
Comments
David Crow 9-Jun-21 22:47pm    
Have you seen this?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900