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.