Search code examples
cursorlistactivitylistadapterandroid-loadermanager

getLoaderManager: in an activity


I am writing an app for a WIMMOne watch: no orientation changes, a small simple screen, and Android version 7.

I got my first Android APP running, but based on a book, it had a fragment. But fragments added complications I do not want to deal with.

My second app has a similar structure: list of items then select an item to see or update details. So I am trying to just read a cursor, and create an array then call "setListAdapter(lstAdapter);".

I started with:

getLoaderManager().initLoader(0, null, this);

Got this error:

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) in the type LoaderManager is not applicable for the arguments (int, null, PhoneListActivity)

So I tried their suggestion:

getLoaderManager().initLoader(0, null, (android.app.LoaderManager.LoaderCallbacks<D>) this);

Got this error:

The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) from the type LoaderManager refers to the missing type D

So I tried this:

getLoaderManager().initLoader(0, null, (android.app.LoaderManager.LoaderCallbacks) this);

and got this error:

Type safety: Unchecked invocation initLoader(int, null, LoaderManager.LoaderCallbacks) of the generic method initLoader(int, Bundle, LoaderManager.LoaderCallbacks) of type LoaderManager

Several postings said to use this for version below 11

getSupportLoaderManager().initLoader(0, null, this);

but that gives me this error:

The method getSupportLoaderManager() is undefined for the type PhoneListActivity

I have also seen not to use "getLoaderManager: in an activity.

So what am I doing wrong?

  • or how do I just read from a cursor and create a list without creating complexities of a fragment?
  • Am I just missing an import?

Many thanks, Clark

Below is what I currently have:

package com.wimmone.phonenos;

import android.view.Menu;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.database.Cursor;
import android.net.Uri;
import android.app.Application;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager; 
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener; 
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;  
import android.widget.Toast;  
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import android.os.Environment;
import com.wimm.framework.app.Dialog;

public class PhoneListActivity extends ListActivity
                               implements
                              LoaderCallbacks<Cursor>
{
 private SimpleCursorAdapter mAdapter;
 List strRecId = new ArrayList();

 private ListAdapter lstAdapter;

 String strPhoneGroup = "";
 String strPhoneName = "";
 String strPhoneHome = "";
 String strPhoneCell = "";
 String strPhoneWork = "";


 private Button btn_Add;
 List strRecord = new ArrayList(3);

  @Override
  protected void onCreate(Bundle savedInstanceState)
    {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.phone_list_activity);

     String[] strItems = new String[] {PhoneProvider.COLUMN_PHONE_NAME};
     int[] iCnt = new int[] { R.id.text1 };
     mAdapter = new SimpleCursorAdapter(this, R.layout.phone_row,
                null, strItems, iCnt, 0);

     Log.d("PHONENOS***", "X onCrt: x02 START");
     setListAdapter(mAdapter);
     //getLoaderManager().initLoader(0, null,  this);
     //getLoaderManager().initLoader(0, null, this);
     getSupportLoaderManager().initLoader(0, null, this);

     //-----------------------------------------------
     // define add button
     Button btnAdd = (Button) findViewById(R.id.btn_Add);

     btnAdd.setOnClickListener(new OnClickListener()
        {
         @Override
         public void onClick(View v)
            {
             Log.d("PHONENOS***", "S create view btnAddEvent:");
            }
        }     );

       //-----------------------------------------------
       // define Exit button

     Button btnExit = (Button) findViewById(R.id.btn_Exit);
     btnExit.setOnClickListener(new OnClickListener()
        {
         @Override
         public void onClick(View v)
            {
             Log.d("PHONENOS***", "S create view btnEXIT:");
                 //Toast.makeText(this, "Good bye",
                 //Toast.LENGTH_SHORT).show();
                 // Application.onTerminate();
             finish();
            }
        }    );

     //-----------------------------------------------
     // define EXPORT button

     Button btnExport . . . 
    }




//===============================================================
// DATA BASE ROUTINES TO LOAD FROM CURSOR 
 @Override
 public Loader<Cursor> onCreateLoader(int ignored, final Bundle args)
    {
      Log.d("EventLst","S LoadCSR");
      return new CursorLoader(this, PhoneProvider.CONTENT_URI, null,
                null, null, null);
         // Log.d("PHONENOS***","X LoadCSR");
     }


 @Override
 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
    {
        // Log.d("PHONENOS***","0 LoadFin");

     int iRecNo = 0;
     int RowNO = 0;
     int iBuCnt = 0;
     String strPath = "";

     mAdapter.swapCursor(cursor);

     strRecId.clear();
    strRecord.clear();
    strRecord.add(0, "mm-dd-yy: Event name");
    cursor.moveToFirst();
    Log.d("EventLst","1 LoadFin DO");

              // Read from cursor and add to list
    while (cursor.isAfterLast() == false)
       {
        iRecNo = iRecNo + 1;
                  // - Table has 4 columns, read them into string array: strC
        String strC[] = { (cursor.getString(0)), (cursor.getString(1)),
                          (cursor.getString(2)), (cursor.getString(3)),
                          (cursor.getString(4)), (cursor.getString(5))
                        };
        String strRowNo = (cursor.getString(0));
        strPhoneGroup = (cursor.getString(1));
        strPhoneName = (cursor.getString(2));
        strPhoneHome = (cursor.getString(3));
        strPhoneCell = (cursor.getString(4));
        strPhoneWork = (cursor.getString(5));

        Log.d("PHONENOS***","4 LoadCSR:" + "I:" + iRecNo + "=" + strRowNo + "N:" + strPhoneName);


                  // - Concatenate Group and name into one string, add to table
        strRecord.add(iRecNo, strPhoneGroup + "/" + strPhoneName);

                  // - save record number for each event in strRecId
                  // - Records are sorted by date, so we need to save RowId to pass
                  //   to edit screen
        strRecId.add((cursor.getString(0)));


        iBuCnt = iBuCnt + 1;

        cursor.moveToNext();
       }   // --------------------end of while loop

                 //cursor.close();

         lstAdapter = new ArrayAdapter<String>(this,
                R.layout.phone_row, R.id.text1, strRecord);

            // * Call to SetListAdapter()informs ListFragment how to fill ListView
            // * here use ArrayAdapter
         setListAdapter(lstAdapter);
            // Log.d("EventLst","8 LoadCSR:" + "ALLDONE");           
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader)
    // This is called when the last Cursor provided to onLoadFinished() above
    // is about to be closed
    {
        // Log.d("EventLst","x LoadRst");
        // INITRUN REMOVE BELOW
        mAdapter.swapCursor(null);

    }

}    

Solution

  • Since no one answered, I re-did my program and got it working. I really needed to use "extends LauncherActivity" (for the WIMMOne). Since I am dealing with a small database on a smart watch, the loader isn't really needed anyway, so I just dropped the use of the loader and used the cursor directly. It actually seems faster.