立即搜索ListFrament

时间:2015-06-12 15:41:13

标签: java android xml

我有一个标签式活动,其中ListFragment作为标签之一。列表项仅是具有电话号码的联系人。我希望能够搜索它并在用户搜索时更新联系人。现在,搜索时列表不会改变,我无法弄清楚如何过滤结果。一个奇怪的事情是我必须以编程方式设置查询提示,因为xml不会更改它。这就是我所拥有的:

的AndroidManifest.xml

<activity
    android:name=".LaunchActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <action android:name="android.intent.action.SEARCH" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>

searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_label"
    android:hint="@string/search_hint"
    android:inputType="textCapWords">
</searchable>

menu_launch.xml

<item android:id="@+id/action_search"
    android:title="@string/action_search"
    android:icon="@drawable/ic_action_search"
    appcompat:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom"/>

Contacts.java

public class ContactsTab extends ListFragment implements LoaderCallbacks<Cursor> {

private CursorAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.contacts_list_item, null, FROM, TO, 0);
}

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

    setListAdapter(mAdapter);
    getLoaderManager().initLoader(0, null, this);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setIconifiedByDefault(true);
    searchView.setQueryHint(getResources().getString(R.string.search_hint));
    searchView.setOnQueryTextListener(queryListener);
    super.onCreateOptionsMenu(menu, inflater);
}

private String currentQuery = null;

final private SearchView.OnQueryTextListener queryListener = new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String newText) {
        if (TextUtils.isEmpty(newText)) {
            currentQuery = null;
        } else {
            currentQuery = newText;
        }
        getLoaderManager().restartLoader(0, null, ContactsTab.this);
        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {
        mAdapter.notifyDataSetChanged();
        return false;
    }
};


private static final String[] PROJECTION = {
        Contacts._ID,
        Contacts.DISPLAY_NAME_PRIMARY,
        Contacts.HAS_PHONE_NUMBER
};

private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { R.id.contact_text };
private static final String SELECTION = "(" + Contacts.IN_VISIBLE_GROUP +
        " = '1' AND (" + Contacts.HAS_PHONE_NUMBER + " != 0 ))";

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    Uri contentUri = Contacts.CONTENT_URI;

    return new CursorLoader(getActivity(),
            contentUri,
            PROJECTION,
            SELECTION,
            null,
            Contacts.DISPLAY_NAME_PRIMARY + " ASC");
}

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

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

我真的很感激任何帮助。

编辑:我相信在加载光标时我必须使用String[] selectionArgs,但我还没有完全弄明白

0 个答案:

没有答案