Asmaa Rashad Asmaa Rashad - 2 years ago 94
Android Question

Android Transform the current Fragment Class to Activity Class

I have a fragment that do somethings related to searchview it works well,

but I want to Create an activity to perform tasks typically as the fragment.

There is no problem with XMLs the main problem is in Java Code.Some methods in the fragment , i don't know the opposite of them in activity.

Fragment Code:

public class AirlineSearchFragment extends Fragment implements SearchView.OnQueryTextListener{
private RecyclerView recyclerview;
private List<AirlineData> airlinesDataModel;
private AirlinesSearchViewAdapter adapter;

public AirlineSearchFragment()
{
setHasOptionsMenu(true);

}
@Override
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);

View view = inflater.inflate(R.layout.airline_search_fragment, container, false);

recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(layoutManager);
return view;
}


@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

setHasOptionsMenu(true);
String[] locales = Locale.getISOCountries();
airlinesDataModel = new ArrayList<>();
airlinesDataModel.addAll(AirlinesCore.airlinesList);
adapter = new AirlinesSearchViewAdapter(airlinesDataModel);
recyclerview.setAdapter(adapter);
}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.auto_complete, menu);

final MenuItem item = menu.findItem(R.id.action_search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);

MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
adapter.setFilter(airlinesDataModel);
return true; // Return true to collapse action view
}

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
return true; // Return true to expand action view
}
});
}

@Override
public boolean onQueryTextChange(String newText) {
final List<AirlineData> filteredModelList = filter(airlinesDataModel, newText);
adapter.setFilter(filteredModelList);
return true;
}

@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

private List<AirlineData> filter(List<AirlineData> models, String query) {
query = query.toLowerCase();

final List<AirlineData> filteredModelList = new ArrayList<>();
for (AirlineData model : models) {
if(model.arabicName==null)
{
model.arabicName="";
}
if(model.shortName==null)
{
model.shortName="";
}
if(model.name==null)
{
model.name="";
}
final String text = model.name.toLowerCase()+model.shortName.toLowerCase()
+model.arabicName.toLowerCase()+model.code.toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}


}


My Activity with some methods copied from fragment(What should I add to it?):

public class AirlinesSearchActivity extends AppCompatActivity implements SearchView.OnQueryTextListener{

private RecyclerView recyclerview;
private List<AirlineData> airlinesDataModel;
private AirlinesSearchViewAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_airlines_search);

}

@Override
public boolean onQueryTextChange(String newText) {
final List<AirlineData> filteredModelList = filter(airlinesDataModel, newText);
adapter.setFilter(filteredModelList);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
private List<AirlineData> filter(List<AirlineData> models, String query) {
query = query.toLowerCase();

final List<AirlineData> filteredModelList = new ArrayList<>();
for (AirlineData model : models) {
if(model.arabicName==null)
{
model.arabicName="";
}
if(model.shortName==null)
{
model.shortName="";
}
if(model.name==null)
{
model.name="";
}
final String text = model.name.toLowerCase()+model.shortName.toLowerCase()
+model.arabicName.toLowerCase()+model.code.toLowerCase();
if (text.contains(query)) {
filteredModelList.add(model);
}
}
return filteredModelList;
}
}

Answer Source

Try moving your logic in onCreateView and onViewCreated to Activity.onCreate().

Then override onCreateOptionsMenu in the Activity and copy the logic that you had in the Fragment

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download