处理点击搜索建议(ContentProvider)

时间:2012-09-04 09:11:58

标签: android search android-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;
}

如果可以,请帮助我

1 个答案:

答案 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);
            }