I am using fragments. I have a spinner in the fragment. I want to lauch a new activity when spinner item is selected. I am getting this error
Error
The constructor Intent(UserHomeActivity, Class) is undefined UserHomeActivity.java /SwipeyTabs/src/com/recscores/android line 28 Java Problem
public class UserHomeActivity extends SherlockFragment{
Spinner spinnerTeam;
Spinner spinnerLeague;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view =inflater.inflate(R.layout.user_home, container, false);
// Team Spinner
spinnerTeam = (Spinner)view.findViewById(R.id.spinner_team);
spinnerTeam.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
**Intent ii = new Intent(UserHomeActivity.this,TeamHomeActivity.class);
startActivity(ii); **
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
Start new Activity as :
Intent ii = new Intent(getActivity(),TeamHomeActivity.class);
startActivity(ii);
because Context is not super class of SherlockFragment class and you will need to use getActivity() which return Activity to current fragment is associated.
Related
I have just one fragment attached to an activity using replace. When the device locks the screen after a few seconds and then I unlock it, the onItemSelected method assigned to some spinner gets executed, even though the fragment views (EditTexts and so) and the spinner selected item remains the same.
public class MyFragment extends Fragment {
private Spinner mySpinner;
public static MyFragment newInstance() {
return new MyFragment();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.some_layout, container, false);
mySpinner = (Spinner)view.findViewById(R.id.my_spinner);
List<String> items = new ArrayList<>();
items.add("Anything");
items.add("Anything");
items.add("Anything");
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(
getContext(),
android.R.layout.simple_spinner_item,
items);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(), "Hello World", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
Every time I select an item I get the toast message "Hello World" (or any other method or action assigned to the listener), but when the device locks and then I unlock, I'm still getting the message as if the method onItemSelected was called.
I've been trying to mess around with booleans and Fragment's onSaveInstanceState and onActivityCreated methods, but I haven't figured out a way to stop onItemSelected method from executing when the device returns from a locked screen.
The problem is that onItemSelected is called after Spinner is initialised, solution is to put mySpinner.setSelection(position, false) before mySpinner.setOnItemSelectedListener
refer to this
I have a problem when try to add setOnItemClickListener into a Fragment.
This is my Fragment.
public class LocalesFragmento extends Fragment {
ListView locales_list;
private List localesList;
private Context context;
public LocalesFragmento() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View vista = inflater.inflate(R.layout.locales_list, container, false);
locales_list = (ListView) vista.findViewById(R.id.localesList);
new getLocalesAsyncTask().execute();
return vista;
}
private void setListAdapter() {
Log.e("Adapter","Adapter...");
locales_list.setAdapter(new LocalesAdapter(getActivity(), getActivity(), localesList));
locales_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//detailInstagram(list.get(position));
}
});
}
The setOnItemClickListener is never executed. Any ideas?
Do I need to implement some other method?
Thanks.
Best regards.
You start your AsyncTask on onActivityCreated method because here before attaching the layout you are starting an async and that may be the reason why the listener is setting up.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
locales_list.setAdapter(new LocalesAdapter(getActivity(), getActivity(), localesList));
locales_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//detailInstagram(list.get(position));
}
});
new getLocalesAsyncTask().execute();
}
Your LocalesAdapter might be the cause. You are passing the context (getActivity()) to the adapter twice.
locales_list.setAdapter(new LocalesAdapter(getActivity(), getActivity(), localesList));
Make your that the arguments passed to the LocalesAdapter are correct.
I have a main activity which consists of spinner + 3 fragments (tabs). I want to access spinner value from fragment. So I wrote this code into onActivityCreated inside my fragment:
final Spinner spinner = (Spinner) getView().findViewById(R.id.spinnerOblasti);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
/*TextView[] vystrahy = new TextView[1];
vystrahy[0] = (TextView) getView().findViewById(R.id.txtVystrahy);
(new htmlParser(vystrahy, 0) ).execute(new Integer[]{spinner.getSelectedItemPosition()});*/
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
but when I run my application spinner throws nullpointerexception. Any ideas on how to access it from fragment?
Here is my onCreateView which I believe is causing problem but I dont know how to fix it:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab1, container, false);
return rootView;
}
Thanks in forward
Doesn't work like that, what do you mean for spinner value? If you want to access to the spinner object you have to setup an interface which will be implemented by your activity and then in onActivityCreated you can call the method of that interface to do something. For example:
public class MyClass implements MyInterface {
public interface MyInterface {
void doSomethingWithSpinner();
}
public void doSomethingWithSpinner() {
mySpinner.setVisibility(View.VISIBLE);
}
}
And your onActivityCreated:
if(getActivity() instanceof MyInterface) {
((MyInterface)getActivity()).doSomethingWithSpinner();
}
I have this piece of code I want to show this dummy data in a static list but I get error in extends fragment and also in get activity, how can I solve them? Do I need to change any part of it?
public class AgendaFragment extends Fragment{
private ArrayAdapter<String> magendaAdapter;
public AgendaFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] data = {
"Monday going to restaurant at 14",
"Monday going to restaurant at 1",
"Monday going to restaurant at 1",
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
magendaAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_agenda, // The name of the layout ID.
R.id.list_item_agenda_textview, // The ID of the textview to populate.
weekForecast);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.listview_agenda);
listView.setAdapter(magendaAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String forecast = magendaAdapter.getItem(position);
Toast.makeText(getActivity(), forecast, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Add
import android.app.Fragment;
at the beginning of your code file.
(or android.support.v4.app.Fragment if you are using Android support library)
I can not get setOnItemClickListener of gridView in Fragment. What can be the problem?
Here is my code::
public class MainMenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main_menu_fragment, container, false);
itemsGridViewObj = (GridView) view.findViewById(R.id.itemsGridView);
itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d(TAG, "--> onItemClick listener..."); // Can not getting this method.
/*if(position == 1) {
FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
fragmentTransaction.commit();
}*/
}
});
return view;
}
}`
You may need to set the following in your ButtonView.
android:focusable="false"
android:focusableInTouchMode="false"
see adding CheckBox to list row loses my onItemClick events?
When using Fragments the initialisation of the view occurs over two stages.
The view is only inflated (and therefore accessible) after the onCreateView method. This method is only for inflating a view and returning it to the Fragment.
Therefore, any logic to do with finding views and setting up onClickListeners should be done in the onActivityCreated() function as this is the first point at which you can access the inflated view.
Have a look at the Google docs at http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
Below is you code adjusted to comply to what I have described above:
public class MainMenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.main_menu_fragment, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GridView itemsGridViewObj = (GridView) findViewById(R.id.itemsGridView);
itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d(TAG, "--> onItemClick listener..."); // You should see this now
/*if(position == 1) {
FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
fragmentTransaction.commit();
}*/
}});
}
}