gpt4 book ai didi

android - 我怎样才能得到 getLoaderManager().restartLoader(0, null, this) 得到正确的这个?

转载 作者:行者123 更新时间:2023-11-30 01:12:42 30 4
gpt4 key购买 nike

当我检索我的联系人时,我试图在我的 listfragment 的 actionBar 中实现一个搜索 View 。

当我尝试使用时我的问题来了

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

在 onQueryTextChange 方法中:

我进入“this”,下一个错误:

Found: 'android.support.v7.widget.SearchView.OnQueryTextListener', required: 'android.support.v4.app.LoaderManager.LoaderCallbacks<java.lang.Object>

我不明白为什么在 onActivityCreated 方法中我可以使用相同的代码并工作:

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

提前致谢!

这是我的全部代码:

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.ResourceCursorAdapter;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.InputStream;


public class contacts extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

private static final String[] PROJECTION = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.CONTACT_STATUS,
ContactsContract.Contacts.CONTACT_PRESENCE,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
};

private MyCursorAdapter mCursorAdapter;
private Uri baseUri;

String cursorFilter;

public contacts() {
}

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


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

setEmptyText(getResources().getString(R.string.empty_numbers));
mCursorAdapter = new MyCursorAdapter(getActivity(), R.layout.list_item, null, 0 );
setListAdapter(mCursorAdapter);
getLoaderManager().initLoader(0, null, this);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (cursorFilter != null) {
baseUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(cursorFilter));
} else {
baseUri = ContactsContract.Contacts.CONTENT_URI;
}

String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND (" + ContactsContract.Contacts.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri, PROJECTION, select, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mCursorAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = (Cursor) mCursorAdapter.getItem(position);
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactId);
intent.setData(uri);
getActivity().startActivity(intent);
}

@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.action_search);
final SearchView sv = new SearchView(((MainActivity) getActivity()).getSupportActionBar().getThemedContext());
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
MenuItemCompat.setActionView(item, sv);

sv.setQueryHint(getString(R.string.type_search));
sv.setIconifiedByDefault(false);
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
Toast.makeText(getActivity(), "Submitted: "+query, Toast.LENGTH_SHORT).show();
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
Toast.makeText(getActivity(), "tap: "+newText, Toast.LENGTH_SHORT).show();
cursorFilter = !TextUtils.isEmpty(newText) ? newText : null;
getLoaderManager().restartLoader(0, null, this);
return false;
}
});

MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(sv.getWindowToken(), 0);
return true;
}

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
sv.requestFocus();

InputMethodManager mgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

return true;
}
});

super.onCreateOptionsMenu(menu,inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:

break;
}
return true;

}
}

最佳答案

使用contacts.this

顺便说一下,让你的类名大写。所以它应该是 Contacts.this

关于android - 我怎样才能得到 getLoaderManager().restartLoader(0, null, this) 得到正确的这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38265849/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com