2012-09-04 67 views
1

我正在使用內容提供商的搜索建議,但我有處理建議點擊問題。我解釋了搜索工作的好處,但是當我點擊建議時,我去了搜索活動,但沒有任何東西。我對搜索代碼:處理點擊搜索建議(ContentProvider)

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.simple_list_item_2); 
    DataBaseUtil.startDataBase(getApplicationContext()); 

    // Get the intent, verify the action and get the query 
    Intent intent = getIntent(); 
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 

     Cursor cursorAdapter = GetListResult(query); 

     if (cursorAdapter != null) { 
      cursorAdapter.moveToFirst(); 
     } 
     startManagingCursor(cursorAdapter); 
     String[] from = new String[] { SearchManager.SUGGEST_COLUMN_ICON_1, 
       BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1, 
       "project_id" }; 
     int[] displayViews = new int[] { R.id.row_image, R.id.name_entry, 
       R.id.name_id, R.id.name_descrption }; 

     final SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(
       this, R.layout.layout_row, cursorAdapter, from, 
       displayViews); 

     final ListView list = (ListView) findViewById(android.R.id.list); 
     list.setAdapter(listAdapter); 

     list.setOnItemClickListener(new ListView.OnItemClickListener() { 
      public void onItemClick(AdapterView<?> l, View v, int position, 
        long id) { 
       Cursor cursor = (Cursor) listAdapter.getItem(position); 
       String i = String.valueOf(cursor.getInt(cursor 
         .getColumnIndex("project_id"))); 

       Intent intent = new Intent(ItemIntent.this, 
         AcceuilProjet.class); 
       intent.putExtra("folderId", "0"); 

       intent.putExtra("projectName", " "); 
       intent.putExtra("projectId", Long.valueOf(i)); 

       startActivity(intent); 
      } 
     }); 
    } 

} 

,這是我的供應商

@Override 
public Cursor query(Uri uri, String[] projection, String selection, 
     String[] selectionArgs, String sortOrder) { 

    String[] columns = new String[] { 
       BaseColumns._ID, 
      ProjectPersistence.KEY_WORD, 
      ProjectPersistence.KEY_DEFINITION, 
      /* SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, 
          (only if you want to refresh shortcuts) */ 
       SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID}; 

    Cursor rtnCursor = null; 
    Cursor rtnCursorProject = null; 

// Cursor rtnCursorMessage = null; 
    String word = uri.getLastPathSegment(); 

    Log.d(TAG, " Search Provider query, word: " + word); 

    if (!word.equals(SEARCH_QUERY)) { 
     try { 
      rtnCursorProject = ProjectPersistence.fetchProjectCursor(word); 

     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } catch (InstantiationException e) { 
      e.printStackTrace(); 
     } 


     rtnCursor = getMatrixCursor(rtnCursorProject); 


    } else if (word.equals(SEARCH_QUERY_VIEW)) { 
     // Handle a suggestions click (because the suggestions all use 
     // ACTION_VIEW) 
     try { 
      rtnCursorProject = ProjectPersistence.fetchProjectCursor(word); 
     } catch (IllegalAccessException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (InstantiationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Toast.makeText(getContext(), "anything", Toast.LENGTH_SHORT).show(); 
     Log.d("Provider", "this is a item click"); 
    } 

    return rtnCursor; 
} 

請幫助我,如果你能

+0

如果單擊回來一次,你應該看到詳細顯示所選項目的屏幕。發生的事情是,搜索活動在活動顯示堆棧的頂部。您需要能夠在選擇搜索建議時從代碼中關閉搜索活動。 –

回答

0

試試這個:

if (Intent.ACTION_VIEW.equals(intent.getAction())) { 
       // handles a click on a search suggestion; launches activity to show word 
       Intent intentShowLocal = new Intent(this, DetailsLocalActivity.class); 
       intentShowLocal.setData(intent.getData()); 
       startActivity(intentShowLocal); 
      }