• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

How To Display Database Items Using Room and An ArrayAdapter

ShatterStar

Newbie
Hi,

I have the following code where I am using an async tassk to load rows from a database using room, I am trying to insert them into an ArrayList so that I can display it in the ui however I keep getting an error.

Code:
ArrayList<TableRemindersClass> tableReminderArrayList;
ListView viewReminderListView;
AppDatabase db;

ArrayAdapter<String> mArrayAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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

    db = AppDatabase.getAppDatabase(getActivity().getApplicationContext());

    View v = inflater.inflate(R.layout.contacts_list_view, container, false);
    viewReminderListView = (ListView) v.findViewById(R.id.ActivityViewReminderList);
    return v;
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    new GetRemindersFromDBtask().execute();

}

private class GetRemindersFromDBtask extends AsyncTask<Void, Integer, Integer> {

    @Override
    protected Integer doInBackground(Void... params) {


        tableReminderArrayList = db.userDao().getAll();

        return (tableReminderArrayList.size() > 0) ? 1 : 0 ;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {

    }

    @Override
    protected void onPostExecute(Integer result) {

        if(result == 1)

            // *** This next line produces the error ***
            mArrayAdapter = new ArrayAdapter<String>(this, viewReminderListView, tableReminderArrayList);

    }
}

What am I doing wrong?
 
Your first port of call for help in situations like this is the Android Javadocs

https://developer.android.com/refer...ter.html#ArrayAdapter(android.content.Context, int, java.util.List<T>)

If you look at the available public constructors for this class, none of them takes a ListView as the second parameter.
As the documentation says:

Parameters
context Context: The current context.
This value must never be null.

resource int: The resource ID for a layout file containing a TextView to use when instantiating views.

objects List: The objects to represent in the ListView.
This value must never be null.
 
Your first port of call for help in situations like this is the Android Javadocs

https://developer.android.com/refer...ter.html#ArrayAdapter(android.content.Context, int, java.util.List<T>)

If you look at the available public constructors for this class, none of them takes a ListView as the second parameter.
As the documentation says:

Parameters
context Context: The current context.
This value must never be null.

resource int: The resource ID for a layout file containing a TextView to use when instantiating views.

objects List: The objects to represent in the ListView.
This value must never be null.


@LV426 I'm really going to have to pick your brain sometime ! lol
you are a real fountain of GOOD info for these types of things!

:thinking:
 
I take no credit for that one. The clever people at Google have worked all this out.

and again , Credit where it's due....

but you always have good answers to Coding questions and that's what I like about the way you work those posts.

Thumbs up Bro.
 
Back
Top Bottom