将ListFragment与带有Holder Pattern的SimpleCursorAdapter一起使用

时间:2012-03-08 08:05:09

标签: android android-loadermanager

我正在ListFragment实施ViewPager并使用ListAdapter扩展SimpleCursorAdapter并尝试保留我在转换为使用之前使用的相同持有人模式LoaderManager使用自定义光标和CommonsWare.loaderex JAR。除了我LoaderManager放在ListFragment而不放在FragmentActivity托管ViewPager之外,所有内容似乎都很合适。主要是因为我不确定从FragmentActivity加载/刷新列表中的数据所需的回调,我想避免在内存中有一个巨大的光标,因为整个表单在所有的ViewPager中的碎片。让表单的每个部分在自己的Loader中管理自己的数据似乎没问题 当我构建类似于演示项目的Adapter Init设置时(参见下面的代码片段)

private void initList() {
    getHelper();        
    adapter=new PhoneNumberListAdapter(getActivity(), R.layout.phonenumber_row, null, null, null);
    setListAdapter(adapter);
    getSupportLoaderManager().initLoader(0, null, getActivity());   

}

getSupportLoaderManager()标记为错误,我无法弄清楚原因。这是片段的完整代码(我知道它是一个蹩脚的电话号码管理器)。对我做错的任何指导都会很棒。

package com.code.android.myproject

import java.util.ArrayList;
import java.util.List;

import com.commonsware.cwac.loaderex.acl.SQLiteCursorLoader;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.widget.SimpleCursorAdapter;
import android.content.Intent;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.ContextMenu;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.code.android.myproject.R;
import com.code.android.myproject.utilities.PhoneNumberListAdapter;
import com.code.android.myproject.data.AbstractDbAdapter;
import com.code.android.myproject.data.ActorDbAdapter;

public class ActorPhoneNumberListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
public final static String ID_EXTRA="com.code.android.myproject.actorid";
protected final static String TAG = "ActorPhoneNumberListFragment";
private SQLiteCursorLoader loader=null;
private PhoneNumberListAdapter adapter=null;
private ActorDbAdapter helper=null;
private String actorId = null;
//private SharedPreferences prefs=null;
OnActorPhoneNumberListListener listener=null;

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

@Override
public void onResume() {
    super.onResume();

    initList();
}

@Override
public void onPause() {
    helper.close();
    Log.w(TAG, "Closing database");

    super.onPause();
}

@Override
public void onListItemClick(ListView list, View view, int position, long id) {
    if (listener!=null) {
        int mId = Integer.parseInt(actorId);
        listener.onActorPhoneNumberListSelected(mId);
    }
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.contactlist_opton, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()==R.id.addselected) {
        startActivity(new Intent(getActivity(), PersonForm.class));
        return(true);
    } else if (item.getItemId()==R.id.help) {
        startActivity(new Intent(getActivity(), HelpPage.class));

        return(true);
    } else if (item.getItemId()==R.id.personRefresh) {
        startActivity(new Intent(getActivity(), PersonList.class));
        return(true);
} else 
    return(super.onOptionsItemSelected(item));
}

public void setOnActorPhoneNumberListListener(OnActorPhoneNumberListListener listener) {
    this.listener=listener;
}

public void loadPhoneNumbers(String actorId) {
    this.actorId=actorId;

    if (actorId!=null) {
        initList();
    }
}

private void initList() {
    getHelper();        
    adapter=new PhoneNumberListAdapter(getActivity(), R.layout.phonenumber_row, null, null, null);
    setListAdapter(adapter);
    getSupportLoaderManager().initLoader(0, null, getActivity());   

}

private void getHelper() {  
    if (helper==null) {
        helper=new ActorDbAdapter(getActivity());
        Log.w(TAG, "Creating a new instance of the database helper object");
    }
    helper.open();
    Log.w(TAG, "Opening database");
}

@Override
public void onDestroy() {
    super.onDestroy();
    helper.close();
}

public interface OnActorPhoneNumberListListener {
    void onActorPhoneNumberListSelected(long id);
}

@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    loader = new SQLiteCursorLoader(getActivity(), helper.mDb, AbstractDbAdapter.PHONE_VIEW, null);
    return (loader);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    adapter.changeCursor(cursor);

}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    adapter.changeCursor(null);

}
}

1 个答案:

答案 0 :(得分:9)

  

getSupportLoaderManager()标记为错误,我无法弄清楚原因。

那是因为Fragment上没有这样的方法。致电getActivity().getSupportLoaderManager()以获取LoaderManager