There is an android application. The point is simple - 1 Activity , 2 Fragment , ActionBar with SearchView and one more button (to call 2 fragments).

The bottom line is that when the search results are displayed, the user can click on one of the options offered under SearchView , which will result in an update (in fact re-creation) 1 Fragment with already new data.

The problem is, it seemed to me that this mechanism of the chain is not very correct and rather complicated, how can it be simplified?

In short. ViewHolder implements OnClickListener which uses a CursorAdapter (which uses SearchView ) has a link to the MainActivity , which when clicked on a particular position, calls the method in the MainActivity to MainActivity the fragment with already new data.

The code itself (missed moments)

 public class MainActivity extends BaseActivity { private SearchView mSearchView; private GeneralSearchListener listener; private SQLAdapter mSQLAdapter; private Cursor mCursor; private CityNameCursorAdapter mCursorAdapter; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); changeFragment(GeneralFragment.newInstance("Moscow")).commit(); getSupportFragmentManager().executePendingTransactions(); init(); } private void init() { mSQLAdapter = new SQLAdapter(getApplicationContext()); mCursor = mSQLAdapter.getCityNamesList(); //пСрвая Π½Π° ΠΌΠΎΠΉ взгляд слоТная инициализация ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° с Π³Ρ€ΠΎΠΌΠΎΠ·Π΄ΠΊΠΈΠΌ конструктором mCursorAdapter = new CityNameCursorAdapter(getApplicationContext(), mCursor, 0, this); //вторая Π΅Ρ‰Π΅ Π±ΠΎΠ»Π΅Π΅ слоТная, Π½ΠΎ Π±Π΅Π· MainActivity listener = new GeneralSearchListener(getApplicationContext(), mCursor, mSQLAdapter, mCursorAdapter); } @Override public boolean onCreateOptionsMenu(final Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); mSearchView = (SearchView) menu.findItem(R.id.menu_search).getActionView(); mSearchView.setOnQueryTextListener(listener); mSearchView.setSearchableInfo(manager.getSearchableInfo(getComponentName())); //todo go next step... mSearchView.setSuggestionsAdapter(mCursorAdapter); return true; } @Override public void loadNewCity(String cityName) { //...create new fragment and set }} 

And the adapter itself:

 public class CityNameCursorAdapter extends CursorAdapter { private LayoutInflater mInflater; private static BaseActivity mMainActivity; //Π‘ΠΎΠ»ΡŒΡˆΠΎΠΉ нСкрасивый конструктор public CityNameCursorAdapter(Context context, final Cursor c, final int flags, BaseActivity mainActivity) { super(context, c, flags); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMainActivity = mainActivity; } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = mInflater.inflate(R.layout.list_item_search_view, parent, false); ViewHolder holder = new ViewHolder(view); holder.mTextViewCityName = (TextView) view.findViewById(R.id.text_view_search_view_item); view.setTag(holder); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { //...lot of code ViewHolder holder = (ViewHolder) view.getTag(); holder.mTextViewCityName.setText(cursor.getString(cursor.getColumnIndex(CityName.KEY_name))); } static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView mTextViewCityName; public ViewHolder(View itemView) { super(itemView); itemView.setOnClickListener(this); } @Override public void onClick(View v) { //ΠΈ всС Ρ€Π°Π΄ΠΈ Ρ‚ΠΎΠ³ΠΎ Ρ‡Ρ‚ΠΎ Π±Ρ‹ ΠΎΡ‚Π»ΠΎΠ²ΠΈΡ‚ΡŒ ΠΊΠ»ΠΈΠΊ ΠΏΠΎ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½ΠΎΠΌΡƒ элСмСнту с ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½Π½Ρ‹ΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ mMainActivity.loadNewCity(mTextViewCityName.getText().toString()); } } } 

There is an idea to add in the xml file responsible for the display of the element, but as far as I know, this is absolutely bad practice.

    0