मेरे पास एक साधारण गतिविधि है जिसमें दो टुकड़े स्विच करने के लिए एक टैबबार होता है। दो टुकड़े एक सूची हैं, सूचीकरण के भीतर खोज को संभव बनाने के लिए एक खोज दृश्य को कार्यान्वित करें और कार्यान्वित करें। सर्चव्यू हमेशा टैबबार के ऊपर एक्शनबार में दिखाया जाता है।टुकड़े के लिए अलग खोज देखें
मेरी समस्या यह है कि जब मैं टैब स्विच करता हूं (अन्य टुकड़े पर जाता हूं) तो खोज दृश्य का इनपुट रीसेट नहीं होता है। इसलिए, दूसरा खंड सर्चव्यू से इनपुट पढ़ता है और तदनुसार सूचीफ्रेम को फ़िल्टर करता है, जो वास्तव में मेरे द्वारा दर्ज किए गए इनपुट को पढ़ता है जब मैं अभी भी खंड में था।
मुझे जो चाहिए वह खोज दोनों टुकड़ों के लिए एक अलग खोजदृश्य होना है। क्या इसको हासिल करने के लिए कोई रास्ता है?
यहाँ मेरी कोड है:
गतिविधि
public class ActivityMainApp extends Activity implements ActionBar.TabListener {
private FragmentManager fragmentManager = getFragmentManager();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mainapp);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Add tabs
ActionBar.Tab relatieTab = actionBar.newTab().setText("Relaties");
ActionBar.Tab takenTab = actionBar.newTab().setText("Taken");
//ActionBar.Tab urenTab = actionBar.newTab().setText("Uren");
// Listeners
relatieTab.setTabListener(this);
takenTab.setTabListener(this);
// Tabs toevoegen aan actionbar
actionBar.addTab(relatieTab);
actionBar.addTab(takenTab);
// Create fragmentmanager to switch fragments
FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.fragment_content);
if(fragment == null){
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(R.id.fragment_content, new FragRelaties());
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
if(tab.getText().equals("Taken")){
FragTaken fragment = new FragTaken();
fragmentTransaction.replace(R.id.fragment_content, fragment);
fragmentTransaction.commit();
}
if(tab.getText().equals("Relaties")){
FragRelaties fragment = new FragRelaties();
fragmentTransaction.replace(R.id.fragment_content, fragment);
fragmentTransaction.commit();
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
टुकड़ा एक
public class FragRelaties extends ListFragment implements SearchView.OnQueryTextListener {
private LayoutInflater inflater;
private ModelRelatie modelRelatie;
private AdapterRelatie relatieAdapter;
public ArrayList<Relatie> relaties;
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
Activity activity = getActivity();
final Context context = activity.getApplicationContext();
setHasOptionsMenu(true);
new AsyncTask<Void, Void, ArrayList<Relatie>>(){
protected ArrayList<Relatie> doInBackground(Void... params) {
// Get all my contacts
modelRelatie = ModelRelatie.instantiate(context);
ArrayList<Relatie> relaties = modelRelatie.getRelaties();
return relaties;
}
protected void onPostExecute(ArrayList<Relatie> relaties) {
// Initial input of objects in the list
relatieAdapter = new AdapterRelatie(inflater.getContext(), R.layout.relatieslijstitem, relaties);
setListAdapter(relatieAdapter);
}
}.execute();
View view = inflater.inflate(R.layout.relaties, container, false);
return view;
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.relatie_menu, menu);
// Get the searchview
MenuItem zoekveld = menu.findItem(R.id.zoekveld_fragrelatie);
SearchView zoekview = (SearchView) zoekveld.getActionView();
// Execute this when searching
zoekview.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
public void onListItemClick(ListView l, View v, int position, long id) {
// Things that happen when i click on an item in the list
}
public boolean onQueryTextSubmit(String query) {
return true;
}
public boolean onQueryTextChange(String zoekterm) {
Activity activity = getActivity();
Context context = activity.getApplicationContext();
// We start searching for the name entered
ModelRelatie modelRelatie = ModelRelatie.instantiate(context);
ArrayList<Relatie> relaties = modelRelatie.zoekRelatie(zoekterm);
// The returned objects are placed in the list
this.relatieAdapter = new AdapterRelatie(inflater.getContext(), R.layout.relatieslijstitem, relaties);
setListAdapter(relatieAdapter);
return true;
}
टुकड़ा दो
public class FragTaken extends ListFragment implements SearchView.OnQueryTextListener {
private LayoutInflater inflater;
private AdapterTaak adapterTaak;
private ModelTaken modelTaken;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
Activity activity = getActivity();
final Context context = activity.getApplicationContext();
setHasOptionsMenu(true);
this.modelTaken = ModelTaken.instantiate(context);
ArrayList<Taak> taken = modelTaken.getTaken();
// Initial input for the list
adapterTaak = new AdapterTaak(inflater.getContext(), R.layout.takenlijstitem, taken);
setListAdapter(adapterTaak);
View view = inflater.inflate(R.layout.taken, container, false);
return view;
}
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.taken_menu, menu);
// get the searview
MenuItem zoekveld = menu.findItem(R.id.zoekveld_fragtaken);
SearchView zoekview = (SearchView) zoekveld.getActionView();
// Execute this when searching
zoekview.setOnQueryTextListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
public boolean onQueryTextSubmit(String query) {
return true;
}
public boolean onQueryTextChange(String zoekterm) {
Activity activity = getActivity();
Context context = activity.getApplicationContext();
// Search the task by the inputed value
ModelTaken modelTaken = ModelTaken.instantiate(context);
ArrayList<Taak> taken = modelTaken.zoekTaak(zoekterm);
// Return the found items to the list
this.adapterTaak = new AdapterTaak(inflater.getContext(), R.layout.takenlijstitem, taken);
setListAdapter(adapterTaak);
return true;
}
}
दोनों टुकड़े खोज भाग को छोड़कर लगभग समान हैं।
एक ही समस्या पर अटक गया। यह काफी समय है लेकिन यदि आप किसी भी उत्तर के साथ आए हैं, तो कृपया इसे पोस्ट करें। –