Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 装载机<;光标>;相对于片段生命周期调用onCreateLoader(int,Bundle)?ListView未填充_Android_Android Fragments_Android Studio_Android Contentprovider_Loader - Fatal编程技术网

Android 装载机<;光标>;相对于片段生命周期调用onCreateLoader(int,Bundle)?ListView未填充

Android 装载机<;光标>;相对于片段生命周期调用onCreateLoader(int,Bundle)?ListView未填充,android,android-fragments,android-studio,android-contentprovider,loader,Android,Android Fragments,Android Studio,Android Contentprovider,Loader,正如标题中所述,与片段生命周期相关的Loader-onCreateLoader(int,Bundle)何时被调用?我试图实现一个由内容提供者支持的游标加载程序,以在片段内部填充ListView。创建片段时,列表视图不会填充,但当我退出活动(通过启动另一个意图)并使用游标加载程序返回片段(导致再次查询内容提供程序)时,列表视图会按预期填充 这使我怀疑我的初始化和/或查询顺序不正确。在我的内容提供者查询方法中,我调用cursor.setNotificationUri(contentResolver,

正如标题中所述,与片段生命周期相关的
Loader-onCreateLoader(int,Bundle)
何时被调用?我试图实现一个由内容提供者支持的游标加载程序,以在片段内部填充ListView。创建片段时,列表视图不会填充,但当我退出活动(通过启动另一个意图)并使用游标加载程序返回片段(导致再次查询内容提供程序)时,列表视图会按预期填充

这使我怀疑我的初始化和/或查询顺序不正确。在我的内容提供者查询方法中,我调用
cursor.setNotificationUri(contentResolver,uri)
,并在insert语句中调用
getContext().getContentResolver().notifyChange(uri,null)

任何帮助都将不胜感激

内容提供者

public class VenueProvider extends ContentProvider {
public static final String TAG = "VenueProvider";

DatabaseHelper databaseHelper;

public static final String AUTHORITY = "com.androidtitan.hotspots.Provider.VenueProvider";
public static final String BASE_PATH = DatabaseHelper.TABLE_VENUES;

public static String base_CONTENT_URI = "content://" + AUTHORITY
        + "/" + BASE_PATH + "/";



//MIME Types for getting a single item or a list of them

public static final String VENUES_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
        + "/vnd.com.androidtitan.Data.venues";
public static final String VENUE_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
        + "/vnd.com.androidtitan.Data.venues";

//Column names
public static class InterfaceConstants {
    public static final String id = "_id";
    public static final String venue_name = "venue_name";
    public static final String venue_city = "venue_city";
    public static final String venue_category = "venue_category";
    public static final String venue_id_string = "venue_string";
    public static final String venue_rating = "venue_rating";
}

//URI matcher variable
private static final int GET_ALL = 0;
private static final int GET_ONE = 1;
private static final int GET_SELECT = 2;

static UriMatcher uriMatcher = BuildUriMatcher();

static UriMatcher BuildUriMatcher() {

    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    //Uri to match and the code to return when matched

    matcher.addURI(AUTHORITY, BASE_PATH, GET_ALL);
    matcher.addURI(AUTHORITY, BASE_PATH + "/#", GET_ONE);
    matcher.addURI(AUTHORITY, BASE_PATH + "/*", GET_SELECT);

    return matcher;
}

@Override
public boolean onCreate() {

    databaseHelper = DatabaseHelper.getInstance(getContext());
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    Cursor queryCursor;

    switch(uriMatcher.match(uri)) {
        case GET_ALL:

            queryCursor = databaseHelper.getReadableDatabase().rawQuery("SELECT * FROM "
                    + DatabaseHelper.TABLE_VENUES, null);
            break;

        case GET_ONE:

            queryCursor = databaseHelper.getReadableDatabase().rawQuery("SELECT * FROM "
                    + DatabaseHelper.TABLE_VENUES
                    + " WHERE " + DatabaseHelper.KEY_ID + " = " + uri.getLastPathSegment(), null);
            break;

        case GET_SELECT:

            String selectionQuery = "SELECT * FROM " + DatabaseHelper.TABLE_VENUES + " td, "
                    + DatabaseHelper.TABLE_COORDINATES + " tg, " + DatabaseHelper.TABLE_COORDINATES_VENUES
                    + " tt WHERE tg." + DatabaseHelper.KEY_LOCAL + " = ? AND tg." + DatabaseHelper.KEY_ID
                    + " = " + "tt." + DatabaseHelper.KEY_COORDS_ID + " AND td." + DatabaseHelper.KEY_ID
                    + " = " + "tt." + DatabaseHelper.KEY_VENUES_ID;

            Log.e(TAG, "selectionQuery: " + selectionQuery);
            queryCursor = databaseHelper.getReadableDatabase().rawQuery(selectionQuery,
                    new String[] { String.valueOf(uri.getLastPathSegment()) });
            break;

        default:
            throw new IllegalArgumentException("Unknown Uri:" + uri);
    }

    // Tell the cursor what uri to watch, so it knows when its source data changes
    queryCursor.setNotificationUri(getContext().getContentResolver(), uri);
    return queryCursor;

}

@Override
public String getType(Uri uri) {
    switch(uriMatcher.match(uri)) {

        case GET_ALL:
            return VENUES_MIME_TYPE;
        case GET_ONE:
            return VENUE_MIME_TYPE;
        case GET_SELECT:
            return VENUES_MIME_TYPE;
        default:
            throw new IllegalArgumentException("Unknown uri: " + uri);
    }
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    int uriType = uriMatcher.match(uri);
    SQLiteDatabase database = databaseHelper.getWritableDatabase();

    long insertId = 0;
    switch(uriType) {
        case GET_ONE:

            insertId = database.insert(DatabaseHelper.TABLE_VENUES, null, values);

            break;

        case GET_ALL:
            break;

        case GET_SELECT:
            break;

        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    Log.e(TAG,"KEY_ID " + uri.getLastPathSegment()
            + " KEY_LOCATION_ID " + values.getAsLong(DatabaseHelper.KEY_VENUE_LOCATION_ID));

    databaseHelper.assignVenueToLocation(Long.valueOf(uri.getLastPathSegment()),
            values.getAsLong(DatabaseHelper.KEY_VENUE_LOCATION_ID));


    getContext().getContentResolver().notifyChange(uri, null);
    return uri;
    //return Uri.parse(DatabaseHelper.TABLE_VENUES + "/" + insertId);

}

//todo:::
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {

    return 0;
}

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    int uriType = uriMatcher.match(uri);
    SQLiteDatabase database = databaseHelper.getWritableDatabase();

    int updateId = 0;

    switch(uriType) {
        case GET_ONE:

            updateId = database.update(DatabaseHelper.TABLE_VENUES, values, null, null);

            break;

        case GET_ALL:
            break;

        case GET_SELECT:
            break;

        default:
            throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    getContext().getContentResolver().notifyChange(uri, null);
    return updateId;
}

}
初始化加载程序的片段

public class VenueResultsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = "VenueResultsFragment";

DatabaseHelper databaseHelper;

LocationBundle focusLocation;
//    String locationName;
int locationIndex;

private static final String[] PROJECTION = new String[]{
        VenueProvider.InterfaceConstants.id,
        VenueProvider.InterfaceConstants.venue_name,
        VenueProvider.InterfaceConstants.venue_city,
        VenueProvider.InterfaceConstants.venue_category,
        VenueProvider.InterfaceConstants.venue_id_string,
        VenueProvider.InterfaceConstants.venue_rating};

private static final int LOADER_ID = 1;

private LoaderManager.LoaderCallbacks<Cursor> callBacks;
private SimpleCursorAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    databaseHelper = DatabaseHelper.getInstance(getActivity());

    locationIndex = getArguments().getInt(MapsActivity.venueFragmentLocIndex);
    focusLocation = databaseHelper.getAllLocations().get(locationIndex);

    if (databaseHelper.getAllVenuesFromLocation(focusLocation).size() == 0) {
        new FoursquareHandler(getActivity(), focusLocation.getLatlng().latitude,
                focusLocation.getLatlng().longitude, locationIndex);
    }


    String[] dataColumns = {VenueProvider.InterfaceConstants.venue_name};
    int[] viewItems = {R.id.nameTextView};

    adapter = new SimpleCursorAdapter(getActivity(), R.layout.listview_venue_item, null,
            dataColumns, viewItems, 0);

    setListAdapter(adapter);

    callBacks = this;

    LoaderManager lm = getLoaderManager();
    lm.initLoader(LOADER_ID, null, callBacks);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_venue_results, container, false);


    return v;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        //mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement Interface");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    //mListener = null;
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    Log.e(TAG, "pre-cursorLoader locationName: " + focusLocation.getLocalName());

    return new CursorLoader(getActivity(), Uri.parse(VenueProvider.base_CONTENT_URI + focusLocation.getLocalName()),
            PROJECTION, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    // switch (loader.getId()) {
    //case LOADER_ID:
    // break;
    // }
    adapter.swapCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {

    adapter.swapCursor(null);
}

}
公共类VenureResultsFragment扩展ListFragment实现LoaderManager.LoaderCallbacks{
私有静态最终字符串TAG=“VenueResultsFragment”;
数据库助手数据库助手;
定位束病灶定位;
//字符串位置名称;
国际定位指数;
私有静态最终字符串[]投影=新字符串[]{
VenueProvider.InterfaceConstants.id,
VenueProvider.InterfaceConstants.venue\u名称,
VenueProvider.InterfaceConstants.venue_city,
VenueProvider.InterfaceConstants.venue\u类别,
VenueProvider.InterfaceConstants.venue\u id\u字符串,
VenueProvider.InterfaceConstants.venue_rating};
私有静态最终整数加载器_ID=1;
私有LoaderManager.LoaderCallbacks回调;
专用SimpleCursorAdapter适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
databaseHelper=databaseHelper.getInstance(getActivity());
locationIndex=getArguments().getInt(MapsActivity.VenueFragmentLocalIndex);
focusLocation=databaseHelper.getAllLocations().get(locationIndex);
if(databaseHelper.getAllVenuesFromLocation(focusLocation).size()=0){
新的FoursquareHandler(getActivity(),focusLocation.getLatlng().latitude,
focusLocation.getLatlng().longitude,locationIndex);
}
String[]dataColumns={VenueProvider.InterfaceConstants.venue_name};
int[]viewItems={R.id.nameTextView};
adapter=new-SimpleCursorAdapter(getActivity(),R.layout.listview\u-venture\u-item,null,
数据列,视图项,0);
setListAdapter(适配器);
回调=这个;
LoaderManager lm=getLoaderManager();
lm.initLoader(LOADER_ID,null,回调);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图v=充气机。充气(R.layout.fragment\u venue\u结果,容器,错误);
返回v;
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
试一试{
//mListener=(OnFragmentInteractionListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现接口”);
}
}
@凌驾
公共无效连接(){
super.onDetach();
//mListener=null;
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
Log.e(标记“预游标加载程序位置名称:”+focusLocation.getLocalName());
返回新的游标装入器(getActivity(),Uri.parse(VenueProvider.base\u CONTENT\u Uri+focusLocation.getLocalName()),
投影,空,空,空);
}
@凌驾
public void onLoadFinished(加载器,光标){
//开关(loader.getId()){
//案例加载器\u ID:
//中断;
// }
适配器.swapCursor(游标);
}
@凌驾
公共void onLoaderReset(加载器){
适配器.swapCursor(空);
}
}

所以我原来的问题是我的查询很差。现在我把它修好了,一切正常

我读了这篇文章,它为我澄清了问题:

为了回答我的问题,内容提供程序一经调用就被创建,即
getLoaderManager.initLoader()
onLoaderfinished()
然后填充加载程序,然后加载程序将信息传递给适配器


如果我错了,请有人纠正我或添加一些内容吗?

因此,我遇到的问题是我的查询不好。现在我把它修好了,一切正常

我读了这篇文章,它为我澄清了问题:

为了回答我的问题,内容提供程序一经调用就被创建,即
getLoaderManager.initLoader()
onLoaderfinished()
然后填充加载程序,然后加载程序将信息传递给适配器

如果我错了,有人能纠正我或补充一下吗