getLoaderManager:在一个活动中

时间:2013-05-12 23:40:59

标签: cursor listactivity listadapter android-loadermanager

我正在为WIMMOne手表编写应用程序:没有方向更改,小巧的简单屏幕和Android版本7.

我的第一个Android APP正在运行,但根据一本书,它有一个片段。但是碎片增加了我不想处理的复杂情况。

我的第二个应用程序具有类似的结构:项目列表然后选择要查看或更新详细信息的项目。所以我试图只是读取一个游标,然后创建一个数组,然后调用“setListAdapter(lstAdapter);”。

我开始时:

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

出现此错误:

  

LoaderManager类型中的方法initLoader(int,Bundle,LoaderManager.LoaderCallbacks)     不适用于参数(int,null,PhoneListActivity)

所以我尝试了他们的建议:

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

出现此错误:

  

该方法的initLoader(int,Bundle,LoaderManager.LoaderCallbacks)方法     LoaderManager引用缺少的类型D

所以我尝试了这个:

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

并收到此错误:

  

类型安全:未经检查的调用initLoader(int,null,LoaderManager.LoaderCallbacks)     LoaderManager类型的泛型方法initLoader(int,Bundle,LoaderManager.LoaderCallbacks)

有几个帖子称其用于11以下的版本

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

但这给了我这个错误:

  

对于PhoneListActivity类型

,未定义getSupportLoaderManager()方法

我还看到不要在活动中使用“getLoaderManager:”。

那么我做错了什么?

  • 或者我如何从光标读取并创建列表而不会产生片段的复杂性?
  • 我只是错过了一个导入?

非常感谢, 克拉克

以下是我目前的情况:

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);

    }

}    

2 个答案:

答案 0 :(得分:0)

由于没有人回答,我重新完成了我的计划并使其正常运行。我真的需要使用“extends LauncherActivity”(用于WIMMOne)。由于我正在处理智能手表上的小型数据库 ,因此无论如何都不需要加载器,所以我只是放弃了加载器的使用并直接使用了光标。它实际上似乎更快。

答案 1 :(得分:0)

您可以让您的活动延长FragmentActivity并使用

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

希望这能帮到你!