2

検索機能を追加したいアプリがあります。説明どおりに実装しようとしてdeveloper.androidいますが、エミュレータで検索をクリックしてもアクティビティが開始されません。問題は何ですか?

  1. SearchActivity.java

    public class SearchActivity extends ListActivity 
    {
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
       handleIntent(getIntent());
    }
    
     @Override
     public void onNewIntent(Intent intent) {
       super.onNewIntent(intent);
       setIntent(intent);
       handleIntent(intent);
     }
    
     public void onListItemClick(ListView l,
       View v, int position, long id) {
        // call detail activity for clicked entry
     }
    
     private void handleIntent(Intent intent) {
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
      String query =intent.getStringExtra(SearchManager.QUERY);
       doSearch(query);
     }
    }
      private void doSearch(String queryStr) {
        // get a Cursor, prepare the ListAdapter
        // and set it
     }
    }
    
  2. xml/searchable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
        android:label="@string/app_name"
        android:hint="@string/srchHint"
        android:inputType="textCapCharacters">
    </searchable>
    
  3. AndroidManifest.xml(以下のコードは他のアクティビティを示していません)

    <application>
         <!-- other actvities -->
          <activity android:label="@string/app_name" android:launchMode="singleTop" 
                 android:name=".SearchActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
          </intent-filter>
            <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
         </activity>
    </application>
    

すでに試したこと:

1.StackOverflowで同様の質問とその解決策を確認します

2. grokkingandroidedumobileのチュートリアルを見てください、それらは私の問題を助けませんでした。

3.chat.stackoverflowで人々に尋ねる時間を費やします

問題/質問

1.わかりました。doSearch()メソッドはありますが、クエリを正確に実行する方法と、dbからの結果を表示する方法(可能であれば、例を示すために小さなdbを取得します)、結果を入力する方法。

2.エミュレータで検索をクリックしても、検索アクティビティが開始されません。

3.何を正確に覚えておくべきか、ものとは別に検索を設計するときdeveloper.android、サイトが何度も教えていない他の詳細。

4.全文検索を実行する方法、私はそれについて読んだ、developer.android私のデータベースがFTSを持つのに良い方法

PS:私はAndroidを学ぶのは初めてなので、新人の間違いがある場合は、それを我慢してください。手順をスキップしないでください。

4

1 に答える 1

0

したがって、検索は、アプリケーションが実際にどれだけ使用可能であるかを決定する重要な要素であり、必要なユーザー拡張機能を提供します。

1.これは検索を処理する最も簡単な方法の1つです

private void handleIntent(Intent intent) { 
      if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
         String query = 
               intent.getStringExtra(SearchManager.QUERY); 
         doSearch(query); 
      } 
   }    

   private void doSearch(String queryStr) 
       { 
       // get a Cursor/ArrayList, prepare the ListAdapter
       // and set it
       //helper.getSearchRecords(queryStr);//helper is a SQLiteOpenHelper Object
       //set it to adapter or get return result and proceed as you wish
       //personally i prefer ArrayAdapter
       ListView lstResult=(ListView)findViewById(R.id.lstVwResult);
       lstResult.setAdapter(null);
    SearchAdapter adapter=new SearchAdapter(getApplicationContext(), R.id.txvPersonId, helper.getSearchRecords(queryStr));
        lstResult.setAdapter(adapter);
   } 

2. SearchActivityが開始されない理由は<meta-data>、このようにアクティビティの外部でも定義する必要があるためです。

<application>
<activity android:label="@string/app_name" android:launchMode="singleTop" 
                 android:name=".SearchActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"  android:resource="@xml/searchable" />
</activity>

<!--define metadata outside activity in application tag-->
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />

3.覚えておくべき重要なことの1つ、アプリに検索機能を追加する理由、それは連絡先アプリです。検索結果をクリックすると、特定の連絡先の詳細が表示されますが、レストランアプリの場合はレストランの詳細については、開発者として、ユーザーのニーズを高め、ユーザーエクスペリエンスを向上させるために開発する必要があります。

4.を持っFull-Text Searchている場合、それは必ずしも必要ではありません。それは、dbへの結果のクエリ方法、検討するフィールド、およびアプリケーションの主な目的にどのように関連しているかにのみ依存します。

詳細については

于 2013-02-19T02:16:06.520 に答える