2012-03-14 27 views
7

Xin chào tôi đang sử dụng tiện ích tìm kiếm android 3.0 (không phải chế độ tìm kiếm), vấn đề là tôi cần phải chuyển một số tham số cùng với từ tìm kiếm.gói truyền android với tìm kiếm

Android không gọi bất kỳ của các

searchManager.setOnDismissListener(new OnDismissListener() { 

      @Override 
      public void onDismiss() { 
       // TODO Auto-generated method stub 
       Log.v(MyApp.TAG, "on dismiss search"); 

      } 
     }); 

     searchManager.setOnCancelListener(new OnCancelListener() { 

      @Override 
      public void onCancel() { 
       // TODO Auto-generated method stub 
       Log.v(MyApp.TAG, "on dismiss cancel"); 

      } 
     }); 

@Override 
    public boolean onSearchRequested() { 
     Bundle appData = new Bundle(); 
     appData.putLong("listino_id", this.listino_id); 
     this.startSearch(null, true, appData, false); 
     return true; 
     // return super.onSearchRequested(); 
    } 

tôi cần hoạt động của tôi để KHÔNG được singleOnTop, đây là AndroidManifest.xml của tôi

<activity android:name=".activity.ListinoProdottiActivity" android:windowSoftInputMode="stateHidden"> 


      <!-- Receives the search request. --> 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
       <!-- No category needed, because the Intent will specify this class component--> 
      </intent-filter> 

        <!-- enable the base activity to send searches to itself --> 
      <meta-data android:name="android.app.default_searchable" 
       android:value=".activity.ListinoProdottiActivity" /> 


      <!-- Points to searchable meta data. --> 
      <meta-data android:name="android.app.searchable" 
         android:resource="@xml/searchable" /> 

     </activity> 

Dưới đây là searchable.xml của tôi

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

Khi bạn không vượt qua các tham số thêm không nó hoạt động? –

+0

Tôi không thể chuyển thông số phụ, vì tôi không thể sử dụng bất kỳ "móc" nào để thêm thông số vào Intent, không có chức năng nào trong số 3 chức năng đó được gọi là – max4ever

+0

Vui lòng dán nội dung của @ xml/searchable. –

Trả lời

0

Kiểm tra xem yo u có các chuỗi được mã hóa cứng cho android:hint hoặc android:label trong res/xml/searchable.xml. Chúng phải là các id tài nguyên chuỗi.

+0

chúng là các tài nguyên chuỗi – max4ever

6

dữ liệu qua với bó trong Search Widget:

layout/main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:text="@string/hello" /> 
    <Button android:id="@+id/button" android:text="test" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" /> 
</LinearLayout> 

bố trí/searchable.xml:

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:label="@string/search_label" 
    android:hint="@string/search_hint" 
    android:searchMode="showSearchLabelAsBadge" 
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" 
    android:voiceLanguageModel="free_form" 
    android:voicePromptText="@string/search_invoke" 
    android:searchSuggestSelection=" ? " 
/> 

giá trị/chuỗi .xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="hello">Hello World, SearchTest!</string> 
<string name="app_name">Searchtest</string> 
<string name="search_label">Search Test</string> 
<string name="search_hint">1234</string> 
<string name="search_invoke">234</string> 
<string name="search_query_results">544545</string> 
</resources> 

AndroidManifest.xml:

<application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:label="@string/app_name" 
      android:name=".SearchWidgetExampleTest" > 
      <intent-filter > 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <meta-data android:name="android.app.default_searchable" 
         android:value=".ResultActivty" /> 
     </activity> 

     <activity android:name=".ResultActivty" 
        android:label="@string/search_query_results"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.SAMPLE_CODE" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.SEARCH" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
      <meta-data android:name="android.app.searchable" 
         android:resource="@layout/searchable" /> 
     </activity> 
    </application> 

ResultActivty.java:

package org.imranandroid.TestSearchexmp; 
import android.app.Activity; 
import android.app.SearchManager; 
import android.os.Bundle; 
import android.widget.Toast; 

public class ResultActivty extends Activity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Bundle bundled= getIntent().getBundleExtra(SearchManager.APP_DATA); 
     Long ttdata=bundled.getLong("listino_id"); 
     Toast.makeText(this, ttdata.toString(), Toast.LENGTH_SHORT).show(); 
    } 
} 

SearchWidgetExampleTest.java:

package org.imranandroid.TestSearchexmp; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class SearchWidgetExampleTest extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button button1 = (Button) findViewById(R.id.button); 
     button1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       onSearchRequested(); 
      } 
     }); 
    } 
    @Override 
    public boolean onSearchRequested() { 
     Bundle appDataBundle = new Bundle(); 
     appDataBundle.putLong("listino_id", 4434724724L); 
     startSearch("imran", false, appDataBundle, false); 
     return true; 
    } 
} 

Xảy ra sự cố!

+1

cảm ơn, nó hoạt động như tìm kiếm thông thường, nhưng nó không sử dụng WIDGET tìm kiếm, như trong http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget hoặc là trong xem ảnh chụp màn hình ở đây http://stackoverflow.com/questions/9588810/how-to-hide-unhide-the-actionbar-list-on-android-3 – max4ever

0

Nếu cuộc gọi của bạn được đồng bộ hóa (bạn thực hiện một yêu cầu tìm kiếm và chờ phản hồi và không thực hiện bất kỳ yêu cầu tìm kiếm nào cho đến khi bạn nhận được phản hồi) thì thật dễ dàng.

Chỉ cần tạo một lớp trợ giúp tĩnh để làm điều đó cho bạn.

Một số điều như thế này:

public static Bundle myStaticClass; 
. 
. 
. 
myStaticClass.putString(key, value); 
callSearch(); 
. 
. 
. 
afterSearchResponse(); 
String value = myStaticClass.get(key); 
+0

và điều này giúp tôi như thế nào? – max4ever

+0

Bạn hỏi về truyền dữ liệu, vì vậy đây là cách bạn có thể làm điều đó, nếu tôi hiểu lầm bạn hãy chỉnh sửa câu hỏi của bạn để dễ hiểu hơn. –

1

khi truy vấn bởi SearchView (tìm kiếm || SearchView widget) bạn có thể vượt qua tham số để searchActivity (searchResultActivity) bởi xác định OnQueryTextListener riêng của bạn như thế này.

searchView.setOnQueryTextListener(new OnQueryTextListener() { 

     @Override 
     public boolean onQueryTextChange(String arg0) { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     @Override 
     public boolean onQueryTextSubmit(String query) { 
      Intent searchIntent = new Intent(getApplicationContext(), SearchResultsActivity.class); 
      searchIntent.putExtra(SearchManager.QUERY, query); 
      searchIntent.setAction(Intent.ACTION_SEARCH); 
      startActivity(searchIntent); 
      return true; 
     } 
    }); 
0

bạn có thể sử dụng theo

trong Menu

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_al_ayats, menu); 

    SearchManager searchManager = 
      (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = 
      (SearchView) menu.findItem(R.id.action_search).getActionView(); 
    Bundle appData = new Bundle(); 
    appData.putString("hello", "world"); 

    searchView.setAppSearchData(appData); 

    searchView.setSearchableInfo(
      searchManager.getSearchableInfo(getComponentName())); 

    return true; 
} 

và trong hoạt động kết quả tìm kiếm của bạn đặt dưới

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_activity_search_result); 

    handleIntent(getIntent()); 
} 

    private void handleIntent(Intent intent) { 

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String extrastring=intent.getBundleExtra(SearchManager.APP_DATA).getString("hello"); 
     //use the query to search your data somehow 
     getSupportActionBar().setTitle(extrastring); 
    } 
} 
Các vấn đề liên quan