Création d'un moteur de recherche dans une action bar

F

FuruyaRei

Membre
Inscrit
30 Octobre 2017
Messages
1
Points
0
#1
Bonjour. J'essaye depuis plusieurs jours de mettre en place un moteur de recherches trouver des destinations dans l'action bar d'un projet d'appli.
J'ai trouvé ce code sur Internet et l'ai essayé, mais à chaque fois, l'appli se ferme à cause du onQueryTextListener.
Pourtant les packages semblent bon.

PHP:
<span class="syntaxdefault"><br />package&nbsp;com</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">example</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">asus</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">actionbarsearchview</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">import&nbsp;android</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">os</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">Bundle</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">import&nbsp;android</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">support</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">v4</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">MenuItemCompat</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">import&nbsp;android</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">support</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">v7</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">app</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">AppCompatActivity</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">import&nbsp;android</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">support</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">v7</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">widget</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">SearchView</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">import&nbsp;android</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">Menu</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">import&nbsp;android</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">view</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">MenuItem</span><span class="syntaxkeyword">;<br /><br />public&nbsp;class&nbsp;</span><span class="syntaxdefault">MainActivity&nbsp;</span><span class="syntaxkeyword">extends&nbsp;</span><span class="syntaxdefault">AppCompatActivity&nbsp;</span><span class="syntaxkeyword">{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;@</span><span class="syntaxdefault">Override<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">protected&nbsp;</span><span class="syntaxdefault">void&nbsp;onCreate</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Bundle&nbsp;savedInstanceState</span><span class="syntaxkeyword">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">super</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">onCreate</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">savedInstanceState</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">setContentView</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">R</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layout</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">activity_main</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;@</span><span class="syntaxdefault">Override<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">public&nbsp;</span><span class="syntaxdefault">boolean&nbsp;onCreateOptionsMenu</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Menu&nbsp;menu</span><span class="syntaxkeyword">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxcomment">//&nbsp;Inflate&nbsp;the&nbsp;menu;&nbsp;this&nbsp;adds&nbsp;items&nbsp;to&nbsp;the&nbsp;action&nbsp;bar&nbsp;if&nbsp;it&nbsp;is&nbsp;present.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">getMenuInflater</span><span class="syntaxkeyword">().</span><span class="syntaxdefault">inflate</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">R</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">menu_search</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">MenuItem&nbsp;searchItem&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxdefault">menu</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">findItem</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">R</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">id</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">action_search</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">SearchView&nbsp;searchView&nbsp;</span><span class="syntaxkeyword">=&nbsp;(</span><span class="syntaxdefault">SearchView</span><span class="syntaxkeyword">)&nbsp;</span><span class="syntaxdefault">MenuItemCompat</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">getActionView</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">searchItem</span><span class="syntaxkeyword">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">searchView</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">setOnQueryTextListener</span><span class="syntaxkeyword">(new&nbsp;</span><span class="syntaxdefault">SearchView</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">OnQueryTextListener</span><span class="syntaxkeyword">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@</span><span class="syntaxdefault">Override<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">public&nbsp;</span><span class="syntaxdefault">boolean&nbsp;onQueryTextSubmit</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">String&nbsp;query</span><span class="syntaxkeyword">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxcomment">//votre&nbsp;code&nbsp;ici<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">return&nbsp;</span><span class="syntaxdefault">false</span><span class="syntaxkeyword">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@</span><span class="syntaxdefault">Override<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">public&nbsp;</span><span class="syntaxdefault">boolean&nbsp;onQueryTextChange</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">String&nbsp;s</span><span class="syntaxkeyword">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span class="syntaxdefault">false</span><span class="syntaxkeyword">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span class="syntaxdefault">true</span><span class="syntaxkeyword">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />}&nbsp;</span><span class="syntaxdefault"></span>
Mon menu :
PHP:
<span class="syntaxhtml"><br /><?xml&nbsp;version="1.0"&nbsp;encoding="utf-8"?><br /><menu<br />&nbsp;&nbsp;&nbsp;&nbsp;xmlns:android="http://schemas.android.com/apk/res/android"<br />&nbsp;&nbsp;&nbsp;&nbsp;xmlns:app="http://schemas.android.com/apk/res-auto"><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<item<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:id="@+id/action_search"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:title="Search"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:icon="@android:drawable/ic_menu_search"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:actionViewClass="android.widget.SearchView"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app:showAsAction="always"><br />&nbsp;&nbsp;&nbsp;&nbsp;</item><br /><br /></menu><br /></span>
Mon layout :

PHP:
<span class="syntaxhtml"><br /><?xml&nbsp;version="1.0"&nbsp;encoding="utf-8"?><br /><RelativeLayout<br />&nbsp;&nbsp;&nbsp;&nbsp;xmlns:android="http://schemas.android.com/apk/res/android"<br />&nbsp;&nbsp;&nbsp;&nbsp;xmlns:app="http://schemas.android.com/apk/res-auto"<br />&nbsp;&nbsp;&nbsp;&nbsp;xmlns:tools="http://schemas.android.com/tools"<br />&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="match_parent"<br />&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="match_parent"<br />&nbsp;&nbsp;&nbsp;&nbsp;tools:context="com.example.asus.actionbarsearchview.MainActivity"><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<ListView<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:id="@+id/listViewCountry"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="wrap_content"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="wrap_content"&nbsp;/><br /><br /></RelativeLayout><br /></span>
et mon gradle :

PHP:
<span class="syntaxdefault"><br />apply&nbsp;plugin</span><span class="syntaxkeyword">:&nbsp;</span><span class="syntaxstring">'com.android.application'<br /><br /></span><span class="syntaxdefault">android&nbsp;</span><span class="syntaxkeyword">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">compileSdkVersion&nbsp;25<br />&nbsp;&nbsp;&nbsp;&nbsp;buildToolsVersion&nbsp;</span><span class="syntaxstring">"26.0.1"<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">defaultConfig&nbsp;</span><span class="syntaxkeyword">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">applicationId&nbsp;</span><span class="syntaxstring">"com.example.asus.actionbarsearchview"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">minSdkVersion&nbsp;15<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;targetSdkVersion&nbsp;25<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;versionCode&nbsp;1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;versionName&nbsp;</span><span class="syntaxstring">"1.0"<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">testInstrumentationRunner&nbsp;</span><span class="syntaxstring">"android.support.test.runner.AndroidJUnitRunner"<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">buildTypes&nbsp;</span><span class="syntaxkeyword">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">release&nbsp;</span><span class="syntaxkeyword">{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">minifyEnabled&nbsp;false<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proguardFiles&nbsp;getDefaultProguardFile</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'proguard-android.txt'</span><span class="syntaxkeyword">),&nbsp;</span><span class="syntaxstring">'proguard-rules.pro'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span class="syntaxdefault">dependencies&nbsp;</span><span class="syntaxkeyword">{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">compile&nbsp;fileTree</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">dir</span><span class="syntaxkeyword">:&nbsp;</span><span class="syntaxstring">'libs'</span><span class="syntaxkeyword">,&nbsp;include:&nbsp;[</span><span class="syntaxstring">'*.jar'</span><span class="syntaxkeyword">])<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">androidTestCompile</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'com.android.support.test.espresso:espresso-core:2.2.2'</span><span class="syntaxkeyword">,&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">exclude&nbsp;group</span><span class="syntaxkeyword">:&nbsp;</span><span class="syntaxstring">'com.android.support'</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">module</span><span class="syntaxkeyword">:&nbsp;</span><span class="syntaxstring">'support-annotations'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxkeyword">})<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">compile&nbsp;</span><span class="syntaxstring">'com.android.support:appcompat-v7:25.3.1'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">compile&nbsp;</span><span class="syntaxstring">'com.android.support.constraint:constraint-layout:1.0.2'<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="syntaxdefault">testCompile&nbsp;</span><span class="syntaxstring">'junit:junit:4.12'<br /></span><span class="syntaxkeyword">}<br />&nbsp;</span><span class="syntaxdefault"></span>
 
yoanf26

yoanf26

Admin suppléant
Inscrit
7 Octobre 2011
Messages
45 840
Points
48
#2
Hello

Pour moi, cette ligne contient une erreur au niveau des parenthèses. Je te mets des couleurs en rouge pour les ouvertures et bleu pour les fermetures. Il manque rai une fermeture de parenthèse.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

Après, je ne suis pas expert en développement d'application Android.
 
Valentin

Valentin

Moderateur Global
Inscrit
20 Février 2013
Messages
10 884
Points
38
#3
Salut,

peux t-on avoir les erreurs générées ?

@yoan
oui c'est normal à cette ligne, la fermeture de la première parenthèse rouge se trouve à la fin :wink:
 
yoanf26

yoanf26

Admin suppléant
Inscrit
7 Octobre 2011
Messages
45 840
Points
48
#4
Hello.

Et la deuxième ?

Edit : oups, je ne l'avais pas vu à côté de l'accolade....
 
Haut Bas