在Android中搜索 [英] Search in Android

查看:177
本文介绍了在Android中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,希望在其中添加搜索功能,我试图按照 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){
    //调用点击条目的详细活动
    }
    $ b $ private void handleIntent(Intent intent){
    if(Intent.ACTION_SEARCH.equals(intent.getAction())){
    String query = intent.getStringExtra(SearchManager。查询);
    doSearch(query);
    }
    }
    private void doSearch(String queryStr){
    //获取一个Cursor,准备ListAdapter
    //并设置它
    }


  2. xml / searchable.xml

     <?xml version =1.0encoding =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> 
    <! - 其他行为 - >
    < activity android:label =@ string / app_nameandroid:launchMode =singleTop
    android:name =。SearchActivity>
    < intent-filter>
    < / intent-filter>
    < meta-data android:name =android.app.searchableandroid:resource =@ xml / searchable/>
    < / activity>
    < / application>


strong>



1。在StackOverflow中查看类似的问题及其解决方案

2。参阅 grokkingandroid edumobile ,他们没有帮助我问题。



3。花时间询问chat.stackoverflow人员

问题/问题



1. 好的我有 doSearch()方法,但是如何激发一个查询以及如何显示db的结果(如果可以的话,请用一个小的db来显示示例的精确程度,如果可以的话),以及如何填充结果。 b
$ b

2。当我点击searc时,搜索活动不会开始h在模拟器中。



3。在设计与开发人员不同的搜索时要牢记的是什么。

如何执行全文搜索,我读了 developer.android ,如何让我的数据库有好的FTS



PS:我是学习android的新手,所以如果出现新手的错误,请耐心等待,不要跳过步骤。

b $ b

解决方案

因此,搜索是决定应用程序实际可用性的关键因素,并提供必要的用户增强功能。

这是处理搜索最简单的方法之一

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



private void doSearch(String queryStr)
{
//获取一个Cursor / ArrayList,准备ListAdapter
/ /并将其设置为
//helper.getSearchRecords(queryStr);//helper是一个SQLiteOpenHelper对象
//将其设置为适配器或获取返回结果并按需要继续
//亲自我更喜欢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> 也需要在活动之外进行定义,如

 <应用> 
< activity android:label =@ string / app_nameandroid:launchMode =singleTop
android:name =。SearchActivity>
< intent-filter>
< / intent-filter>
< meta-data android:name =android.app.searchableandroid:resource =@ xml / searchable/>
< / activity>

<! - 在应用程序标记中定义活动外的元数据 - >
< meta-data android:name =android.app.default_searchableandroid:value =。SearchActivity/>

3. 要记住的关键之一是,为什么你的正在添加搜索功能到您的应用程序,它是一个联系人应用程序,点击检索结果将显示更多的细节特定的联系人,但对于餐厅应用程序,它会显示详细信息的餐厅的详细信息,你作为开发人员应该开发它,以满足用户的需求并增强用户体验。



4。对于全文搜索并不总是必要的,它完全取决于你希望如何查询结果到db,你会考虑哪些字段,它们与你的应用程序的主要目的有什么关系,

For more Reference




I have a app in which i wish to add search feature, i am trying to implement as told in developer.android but the activity does not start when i click on search in emulator, what is the problem?

  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(code below does not show other activities)

    <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>
    

Things already tried:

1. looks at similar question and their solution at StackOverflow

2. Look at tutorials at grokkingandroid and edumobile, they did not help my problem.

3. Spend time asking people at chat.stackoverflow

Problem/Questions

1. Ok i have doSearch() method, but how exactly fire a query and how to display results from db(take a small db to show example how exactly, if you can), how to populate results.

2. The search activity does not start when i click on search in the emulator.

3. What exactly to keep in mind, while designing search apart from the developer.android stuff, the other details which many times the site does not tell.

4. How to perform a full-text search, i read about it on developer.android , how to have my db be good to have a FTS

P.S: I am new to learning android, so if there is a rookie mistake, please bear with it, do not skip over steps.

解决方案

So the Search is crucial factor on how usable your application actually is and provide necessary user enhancement

1. This is one of the most easiest way to handle search

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. The reason for SearchActivity not starting is <meta-data> need to be define outside the activity also, like this

<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. One of the key things to keep in mind, why your are adding search feature to your app, it its a Contacts App, clicking on retrieve results would show more detail to particular contact, but for a restaurant app it will show detail for where restaurant details, you as developer should develop it to crater user needs and enhance user experience.

4. For having Full-Text Search, it is not always necessary, it solely depends on how you wish to query for results to db, which fields will you consider and how are they relevant to main purpose of your application,

For more Reference

这篇关于在Android中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆