À l'aide d'un EditText pour filtrer un SimpleCursorAdapter adossés à des ListView
J'ai une ListView avec un élément personnalisé de mise en page, soutenu par un SimpleCursorAdapter. J'aimerais que l'utilisateur soit capable de taper du texte dans un EditText boîte et d'avoir la liste de filtrer automatiquement en cours de saisie. Il semble que ce est très simple à faire avec un ArrayAdapter, comme décrit ici.
Malheureusement, je ne suis pas du tout avoir de la chance pour la faire fonctionner avec un SimpleCursorAdapter. J'ai essayé d'ajouter des méthodes supplémentaires, comme décrit ici, mais cet exemple est pour un AutoCompleteTextView au lieu d'une ListView. Je n'ai pas été en mesure d'obtenir quelque chose de travail pour ma ListView.
Quelqu'un peut-il me fournir un exemple de filtrage d'un SimpleCursorAdapter adossés à des ListView avec un EditText boîte?
public class DirectoryListActivity extends DirectoryActivity {
private static SimpleCursorAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.directory_list);
fillPeopleListView();
EditText etext=(EditText)findViewById(R.id.search_box);
etext.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
adapter.getFilter().filter(s.toString());
}
});
}
private void fillPeopleListView() {
//Populate the ListView
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(
directoryPeople.PEOPLE_TABLE
);
String asColumnsToReturn[] = {
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople.LAST_NAME + "," +
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople.FIRST_NAME + "," +
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople.MIDDLE_NAME + "," +
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople.JOB_TITLE + "," +
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople._ID
};
mCursor = queryBuilder.query(mDB, asColumnsToReturn, null, null,
null, null, directoryPeople.DEFAULT_SORT_ORDER);
startManagingCursor(mCursor);
adapter = new SimpleCursorAdapter(this,
R.layout.directory_people_item, mCursor,
new String[]{
directoryPeople.LAST_NAME,
directoryPeople.FIRST_NAME,
directoryPeople.MIDDLE_NAME,
directoryPeople.JOB_TITLE},
new int[]{
R.id.txtLastName,
R.id.txtFirstName,
R.id.txtMiddle,
R.id.txtTitle}
);
ListView av = (ListView)findViewById(R.id.listPeople);
av.setAdapter(adapter);
av.setFastScrollEnabled(true);
adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
String partialValue = constraint.toString();
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
String asColumnsToReturn[] = {
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople.LAST_NAME + "," +
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople.FIRST_NAME + "," +
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople.MIDDLE_NAME + "," +
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople.JOB_TITLE + "," +
directoryPeople.PEOPLE_TABLE + "."
+ directoryPeople._ID
};
Cursor c = queryBuilder.query(mDB, asColumnsToReturn, directoryPeople.LAST_NAME + " LIKE " + partialValue, null,
null, null, directoryPeople.DEFAULT_SORT_ORDER);
return c;
}
});
}
Voici ma mise en page de code, comme l'a demandé. Page principale:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white">
<include android:id="@+id/lytHeadBar" layout="@layout/headbar" />
<include android:id="@+id/lytTitleBar" layout="@layout/titlebar" />
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type a name..."
android:maxLines="1" android:inputType="textFilter|textCapWords" android:id="@+id/search_box"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listPeople"
android:divider="@android:drawable/divider_horizontal_dark"
android:cacheColorHint="@android:color/white" />
</LinearLayout>
Élément de la liste mise en page:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lytItem"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/item_line_bottom">
<LinearLayout android:id="@+id/nameLine"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/txtLastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:text="lastname" />
<TextView
android:id="@+id/nameSpace1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text=", " />
<TextView
android:id="@+id/txtFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text="firstname" />
<TextView
android:id="@+id/nameSpace2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text=" " />
<TextView
android:id="@+id/txtMiddle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="@android:color/black"
android:text="middle" />
</LinearLayout>
<LinearLayout android:id="@+id/titleLine"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@android:color/black"
android:text="title" android:ellipsize="marquee"/>
</LinearLayout>
</LinearLayout>
Vous devez vous connecter pour publier un commentaire.
eh bien, il est beaucoup plus comme arrayAdapter
voici un exemple de code
}
et deux fichiers xml sont énumérés ci-dessous
main.xml est
et list_item.xml est
Espère que cela va vous aider à
getAllSuggestedValues()
méthode dans ma base de données à l'aide. Comment puis-je la mettre?getAllValues
non plus d'ailleurs. Je suis juste mise en œuvre de mes requêtes directement dans cette classe. J'ai essayé de l'adapter à votre exemple, mais mon ListView toujours affiche toutes les entrées peu importe ce que je tape. Je vais mettre à jour ma question avec mon code.Finalement réussi à résoudre le problème! La réponse peut être trouvée ici: ListView, SimpleCursorAdapter, un EditText filtre -- pourquoi ne veut-il pas faire quelque chose?