在SearchActivity中调用完成无效

时间:2016-05-12 13:17:13

标签: android listview android-cursoradapter android-search activity-finish

我有一个我根据http://developer.android.com/guide/topics/search/search-dialog.html构建的SearchActivity。从本地内容提供程序获取数据后,我使用基本的CursorAdapter在ListView中显示它。到目前为止一切正常。然后在listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {});我设置中将结果返回给调用活动,然后调用finish()

但是发生的事情是finish()不断清除适配器而不是完成活动。我尝试了不同的完成和onBackPressed但没有区别:它清除ListView但仍保留在活动中。谁能看到我做错了什么?

public class SearchableActivity extends AppCompatActivity {
    private static final String TAG = SearchableActivity.class.getSimpleName();
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
        SearchView searchView = (SearchView) toolbar.findViewById(R.id.search_view);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

        searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSearchRequested();
            }
        });
        toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        listView = (ListView) findViewById(R.id.search_result_list);
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            PLog.d(TAG, "ACTION_SEARCH intent");
            String query = intent.getStringExtra(SearchManager.QUERY);
            searchContentProvider(query);
        }
    }

    private void searchContentProvider(String query) {
        PLog.d(TAG, "start searchContentProvider");
        Uri uri = PContract.SearchableEntry.buildUri(query);
        Cursor cursor = getContentResolver().query(uri, DataProjection.Searchable.PROJ, null, null, null);
        if (null == cursor || 0 == cursor.getCount()) {
            PLog.d(TAG, "end searchContentProvider without result");
            return;
        }
        cursor.moveToFirst();
        searchableResultsList(cursor);
        PLog.d(TAG, "end searchContentProvider");
    }

    private void searchableResultsList(Cursor data) {
        PLog.d(TAG, "end searchableResultsList");
        CursorAdapter cursorAdapter = (CursorAdapter) listView.getAdapter();
        if (null == cursorAdapter) {
            cursorAdapter = new CursorAdapter(this, data, true) {
                @Override
                public View newView(Context context, Cursor cursor, ViewGroup parent) {
                    return getLayoutInflater().inflate(R.layout.searchable_row,parent,false);
                }

                @Override
                public void bindView(View view, Context context, Cursor cursor) {
                    TextView pName = (TextView) view.findViewById(R.id.p_name);
                    TextView catlang = (TextView) view.findViewById(R.id.catlang);
                    pName.setText(cursor.getString(DataProjection.Searchable.COL_NAME));
                    String lang = cursor.getString(DataProjection.Searchable.COL_LANG);
                    String cat = cursor.getString(DataProjection.Searchable.COL_CAT);
                    catlang.setText(cat + ", " + lang);

                }
            };
            listView.setAdapter(cursorAdapter);
        } else {
            cursorAdapter.swapCursor(data);
        }
        PLog.d(TAG, "searchable: cursor count is?  " + data.getCount());

        final CursorAdapter cursorAdapterFinal = cursorAdapter;
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Cursor cursor = cursorAdapterFinal.getCursor();
                cursor.moveToPosition(position);

                Intent data = new Intent();
                data.putExtra(Constants.ARGS_P_DB_ID, cursor.getString(DataProjection.Searchable.COL_P_ID));
                if (getParent() == null) {
                    setResult(RESULT_OK, data);
                } else {
                    getParent().setResult(RESULT_OK, data);
                }
                PLog.d(TAG, "onItemClick POSITION %s", position);
                finish();//I try different things here
            }
        });
        PLog.d(TAG, "end searchableResultsList");
    }
}

3 个答案:

答案 0 :(得分:0)

如果此代码在对话框内,则完成活动调用:

getActivity().finish();

答案 1 :(得分:0)

我面临同样的问题。 写return();完成后(); 解决了我的问题。

但为什么?: 根据{{​​3}}回答

  

调用finish()的方法将运行完成。在将控制权返回给Android之前,finish()操作甚至不会开始。

答案 2 :(得分:-1)

试     SearchActivity.this.finish();