2012-09-04 29 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); 
      }