Using a ListFragment with a SimpleCursorAdapter with the Holder Pattern

∥☆過路亽.° 提交于 2019-12-24 01:19:32

问题


I am implementing a ListFragment from a ViewPager and using a ListAdapter extending SimpleCursorAdapter and trying to keep the same Holder Pattern I was using before the conversion to using a LoaderManager with custom cursor and the CommonsWare.loaderex JAR. Everything seems to be fitting together well except I placed the LoaderManager on the ListFragment and not in the FragmentActivity hosting the ViewPager. Mostly because I was not sure about the required call backs to load / refresh the data in the list from the FragmentActivity and I wanted to avoid having a giant cursor out in memory since the overall form has many fields across all of the Fragments in the ViewPager. It seemed okay to have each section of the form manage its own data in its own Loader.
When I build the Adapter Init setup similar to the demo project (see code snippet below)

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

}

The getSupportLoaderManager() flags as an error and I cannot figure out why. Here is the full code for the Fragment (I know it’s a lame Phone Number Manager). Any guidance on what I did wrong would be great.

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:


The getSupportLoaderManager() flags as an error and I cannot figure out why.

That is because there is no such method on Fragment. Call getActivity().getSupportLoaderManager() to get your LoaderManager.



来源:https://stackoverflow.com/questions/9614530/using-a-listfragment-with-a-simplecursoradapter-with-the-holder-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!