1

我正在为 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);

得到这个错误:

LoaderManager 类型中的方法 initLoader(int, Bundle, LoaderManager.LoaderCallbacks) 指的是缺少的类型 D

所以我尝试了这个:

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

并得到这个错误:

类型安全:LoaderManager 类型的通用方法 initLoader(int, Bundle, LoaderManager.LoaderCallbacks) 的未经检查的调用 initLoader(int, null, 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);

    }

}    
4

2 回答 2

0

由于没有人回答,我重新编写了我的程序并让它工作。我真的需要使用“扩展 LauncherActivity”(对于 WIMMOne)。由于我正在处理智能手表上的小型数据库,因此无论如何都不需要加载器,所以我只是放弃了加载器的使用并直接使用游标。它实际上似乎更快。

于 2013-05-29T00:42:50.973 回答
0

您可以让您的活动扩展FragmentActivity和使用

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

希望这对你有帮助!

于 2014-07-01T04:20:08.307 回答