2016-11-15 5 views
0

URL(JSONデータ)からフェッチされた花の名前を表示するListviewがあります。 JSONデータにあるすべての花の情報を保存した型クラス "Flower"のArrayListを作成しました。 アプリにSearchViewを追加しました。そのアプリケーションで適切に設定されていますが、その中にテキストを入力してモバイルのキーボードの検索ボタンをクリックすると、それ以降は何もしません。検索ビューがアンドロイドで機能していません

これは私のSearchResultActivity.javaファイルです。

私はとのAndroidManifest.xmlファイルにこの活動を設定している
package com.example.hsports.flowers; 

import android.app.SearchManager; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class SearchResultsActivity extends AppCompatActivity { 

    FrontPage frontPage=new FrontPage(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_search_results); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     handleIntent(getIntent()); 


     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 


    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 

     handleIntent(getIntent()); 

    } 


    private void handleIntent(Intent intent) { 

     TextView name=(TextView)findViewById(R.id.displayName); 
     TextView url=(TextView)findViewById(R.id.displayUrl); 


     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      String query = intent.getStringExtra(SearchManager.QUERY); 

      for(Flower f:frontPage.flowersList) 
      { 
       if(f.name.equalsIgnoreCase(query)) 
       { 

        name.setText(f.name); 
        url.setText(f.url); 

       } 
      } 



     } 
    } 
} 

私はsearchviewのアイコンをクリックすると、いくつかのテキストを入力し、押し、キーボードから入力し、そのは私にいずれかを与えていないです
<activity 
      android:name=".SearchResultsActivity" 
      android:label="@string/title_activity_search_results" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
      </intent-filter> 


     </activity> 

結果。 もう1つの疑問があります。searchViewにクエリを入力した後、どこにフローが入りますか?

答えて

0

あなたのマニフェスト

EDITの活動レベルのandroid:launchMode="singleTop"不足している:あなたはまた、次のコード

<?xml version="1.0" encoding="utf-8"?> 
 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
 
      android:label="@string/app_name" 
 
      android:hint="Search" />

とxmlディレクトリにsearchable.xmlファイルを作成する必要があり

あなたのマニフェストに同じアクティビティがある場合は、次のメタデータを追加してください

<activity android:name=".SearchResultsActivity" 
 
      android:label="@string/title_activity_search_results" 
 
      android:theme="@style/AppTheme.NoActionBar" 
 
      android:launchMode="singleTop"> 
 
      <intent-filter> 
 
       <action android:name="android.intent.action.SEARCH" /> 
 
      </intent-filter> 
 
      <meta-data android:name="android.app.searchable" 
 
         android:resource="@xml/searchable" /> 
 
     </activity>

+0

私はsearchViewにクエリを入力するとき、あなたは私の流れを教えてくださいすることができます。この後はどこに行くの? –

+0

私はあなたが上で示唆したものすべてを加えましたが、うまくいきません。 –

+0

@ shikher.mishraそれがすべて動作すると仮定します。 'handleIntent'の' query'から文字列クエリを取得することができます。また、onNewIntentに 'setIntent(intent)'を追加する必要があります。 –

関連する問題