【问题标题】:handling a click of Search suggestions (ContentProvider)处理点击搜索建议 (ContentProvider)
【发布时间】:2012-09-04 09:11:57
【问题描述】:

我正在使用内容提供商来提供搜索建议,但在处理建议点击时遇到了问题。我解释说搜索效果很好,但是当我单击建议时,我会转到搜索活动,但什么也没有。我的搜索代码:

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

如果可以的话请帮帮我

【问题讨论】:

  • 如果您单击返回一次,您应该会看到详细说明所选项目的屏幕。发生的事情是 searchactivity 位于活动显示堆栈的顶部。选择搜索建议时,您需要能够从代码中关闭搜索活动。

标签: android search android-contentprovider


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 1970-01-01
    • 2020-03-30
    相关资源
    最近更新 更多