場景:您有兩個片段,填充ListAdapter的Fragment A,包含具有ListAdapter的SearchView的片段B。從片段B中的ListAdapter更新SearchView列表中的片段A
問:Fragment A如何更新Fragment B的SearchView的ListAdapter?
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
SectionsPagerAdapter mSectionsPagerAdapter =
new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager viewPager =
(ViewPager)findViewById(R.id.main_activity_container);
viewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout =
(TabLayout) findViewById(R.id.main_activity_tabs);
tabLayout.setupWithViewPager(viewPager);
} //MainActivity class
private class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
Fragment_1 fr1 = new fr1();
return fr1;
case 1:
Fragment_2 fr2 = new fr2();
return fr2;
default:
return null;
}
}
} //sectionPageAdapter class
public class Fragment_1 extends Fragment {
private View rootView;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView = inflater.inflate(R.layout.fr1, container, false);
/* toggle button will cause data to be read and updated */
/* from fr1 to fr2 */
handleToggleButton();
return rootView;
}
private void handleToggleButton(){
ToggleButton tglBroadCaseBtn = (ToggleButton) this.rootView.findViewById(R.id.broadcast_btn);
tglBroadCaseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* update content in searchview */
updateSearchView();
}
}
}
public void updateSearchView(){
new Handler().post(new Runnable() {
@Override
public void run() {
/* get data from database */
final Cursor cursor = dblocate.rawQuery(DB_SEL + DB_TABLE, null);
/* populate ListAdapter */
final ListAdapter la = new ListAdapter(getContext(), cursor, false);
/* update list adapter in fr_2 */
la.notifyDataSetChanged(); /* <--- THIS IS HARD */
cursor.close();
}
});
}
}
} //Fragment_1 class
public class Fragment_2 extends Fragment {
private View rootView;
private SearchManager searchManager;
private android.support.v7.widget.SearchView searchView;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView = inflater.inflate(R.layout.fr2, container, false);
searchManager = (SearchManager) rootView.getContext().getSystemService(Context.SEARCH_SERVICE);
searchView = createSearchView();
new Handler().post(new Runnable() {
@Override
public void run() {
final Cursor cursor = db.rawQuery(DB_SEL + DB_TABLE, null);
listAdapter = new ListAdapter(getContext(), cursor, false);
searchView.setSuggestionsAdapter(listAdapter);
searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
return true;
}
@Override
public boolean onSuggestionClick(int position) {
Cursor cursor1 = (Cursor) searchView.getSuggestionsAdapter().getItem(position);
String suggestion = cursor1.getString(cursor1.getColumnIndex(cursor1.getColumnName(position)));
ListSelection selection = new ListSelection();
selection.setSelection(suggestion);
return true;
}
});
cursor.close();
}
});
return rootView;
}
private SearchView createSearchView(){
SearchView temp;
temp = (android.support.v7.widget.SearchView) nestedView.findViewById(R.id.search_view);
temp.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
temp.setQueryHint(SEARCH_VIEW_QUERY_HINT);
temp.onActionViewExpanded();
temp.setActivated(true);
temp.setIconified(false);
return temp;
}
} //Fragment_2 class
public class ListAdapter extends CursorAdapter {
private static final int DB_FIELD = 2;
public ListAdapter(Context context, Cursor cursor, boolean autoQuery) {
super(context, cursor, autoQuery);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.fr2_sv_single_row_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tvItem = (TextView) view.findViewById(R.id.tv_some_name);
tvItem.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(DB_FIELD))));
}
} //ListAdapter
使用界面https://developer.android.com/training/basics/fragments/communicating.html –