Implement search for SelectPublicKeyFragment

This commit is contained in:
Miroojin Bakshi 2014-03-06 03:37:04 +05:30
parent 53f6189301
commit 7932dd8a81
2 changed files with 39 additions and 5 deletions

View File

@ -38,17 +38,22 @@ import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;
public class SelectPublicKeyFragment extends ListFragmentWorkaround implements
public class SelectPublicKeyFragment extends ListFragmentWorkaround implements TextWatcher,
LoaderManager.LoaderCallbacks<Cursor> {
public static final String ARG_PRESELECTED_KEY_IDS = "preselected_key_ids";
private Activity mActivity;
private SelectKeyCursorAdapter mAdapter;
private ListView mListView;
private EditText mSearchView;
private long mSelectedMasterKeyIds[];
private String mCurQuery;
/**
* Creates new instance of this fragment
@ -67,7 +72,8 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSearchView = (EditText)getActivity().findViewById(R.id.select_public_key_search);
mSearchView.addTextChangedListener(this);
mSelectedMasterKeyIds = getArguments().getLongArray(ARG_PRESELECTED_KEY_IDS);
}
@ -82,7 +88,6 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements
mListView = getListView();
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// Give some text to display if there is no data. In a real
// application this would come from a resource.
setEmptyText(getString(R.string.list_empty));
@ -220,10 +225,14 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements
// sort by selected master keys
orderBy = inMasterKeyList + " DESC, " + orderBy;
}
String where = null;
if(mCurQuery != null)
where = UserIds.USER_ID + " LIKE \"" + mCurQuery + "%\"";
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(getActivity(), baseUri, projection, null, null, orderBy);
return new CursorLoader(getActivity(), baseUri, projection, where, null, orderBy);
}
@Override
@ -250,4 +259,21 @@ public class SelectPublicKeyFragment extends ListFragmentWorkaround implements
// longer using it.
mAdapter.swapCursor(null);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
String newQuery = !TextUtils.isEmpty(editable.toString()) ? editable.toString() : null;
mCurQuery = newQuery;
getLoaderManager().restartLoader(0, null, this);
}
}

View File

@ -4,8 +4,16 @@
android:layout_height="match_parent"
android:layout_centerHorizontal="true" >
<EditText
android:id="@+id/select_public_key_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/menu_search"
android:drawableLeft="@drawable/ic_action_search"/>
<FrameLayout
android:id="@+id/select_public_key_fragment_container"
android:layout_below="@id/select_public_key_search"
android:layout_width="match_parent"
android:layout_height="match_parent" />