Selecting Spinner Item doesen't trigger onItemSelected - method - java

I have a settings fragment with 2 spinners in it. I'm not able to execute code written into the onItemSelected-method. There are no errors or exceptions.
I searched many similiar questions but none of the solutions worked for me. I have no idea what to try next.
public class SettingsFragment extends Fragment implements AdapterView.OnItemSelectedListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Getting the instance of Spinner and applying OnItemSelectedListener on it
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Spinner spinnerBetrag = (Spinner) view.findViewById(R.id.spinnerBetrag);
Spinner spinnerDetails = (Spinner) view.findViewById(R.id.spinnerDetails);
spinnerBetrag.setOnItemSelectedListener(this);
spinnerDetails.setOnItemSelectedListener(this);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_settings, container, false);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//>>Problem: Code within this method is never beeing executed<<
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}

Change your onCreateView() method to return the view
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Getting the instance of Spinner and applying OnItemSelectedListener on it
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Spinner spinnerBetrag = (Spinner) view.findViewById(R.id.spinnerBetrag);
Spinner spinnerDetails = (Spinner) view.findViewById(R.id.spinnerDetails);
spinnerBetrag.setOnItemSelectedListener(this);
spinnerDetails.setOnItemSelectedListener(this);
return view; // add this instead
}
Also for your onItemSelected() add
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinnerBetrag)
{
Log.d("Spinner 1","selected");
}
else if(spinner.getId() == R.id.spinnerDetails)
{
Log.d("Spinner 2","selected");
}
}

Change your return statement in onCreateView to
return view;
Your current return statement is inflating a whole new view

Related

appcompat and fragment in one activity

I'm adding fragments to my menu.....now one of the menufragments is a listview and i need to use both appcompatactivity and fragment class which isn't possible to extends both
The error are in the Screenshots
public class friendjava extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.friends, container, false);
final ListView list = (ListView) findViewById(R.id.listview1);
final ArrayList<String> mylist = new ArrayList<>();
mylist.add("wisdom");
mylist.add("favour");
mylist.add("faith");
mylist.add("chisom");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mylist);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),mylist.get(position),Toast.LENGTH_SHORT).show();
}
});
}
}
the error are in screenshot this is error screenshot
That is a fragment. A fragment is separate from an activity. You have to bind it to an activity. The findViewById() method needs to be tied to a view of an activity.
Get the view reference to a view object first.
public class friendjava extends Fragment {
private View view;
private ListView list;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.friends, container, false);
list = view.findViewById(R.id.listview1);
final ArrayList<String> mylist = new ArrayList<>();
mylist.add("wisdom");
mylist.add("favour");
mylist.add("faith");
mylist.add("chisom");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mylist);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),mylist.get(position),Toast.LENGTH_SHORT).show();
}
});
}
return view;
}
Take care of how view is used and returned in the end.
Try changing
final ListView list = (ListView) findViewById(R.id.listview1);
into
final ListView list = findViewById(R.id.listview1);
and after that run the program and check logcat

How to set onItemClickListener for ListView in a fragment?

I try do it with below code but it not working! Please help me... I am confused ...
In below code all things is good but onItemClickListener is useless and it is in onCreateView method is wrong?
My items layout is not problem..
public class favFrg extends Fragment {
View v;
ListView listView;
List<HashMap<String,Object>> listFav;
DatabaseManager dbManager;
public favFrg() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v= inflater.inflate(R.layout.fragment_fav_frg, container, false);
listView= v.findViewById(R.id.listViewFav);
dbManager=new DatabaseManager(getContext());
dbManager.openDB();
listFav=dbManager.getStoryFavorite();
dbManager.closeDB();
String[] keys={"id","txtName"};
int[] resources={R.id.favTxtViewNumberStory,R.id.favTxtViewTitleStory};
SimpleAdapter adapter=new SimpleAdapter(getContext(),listFav,R.layout.fav_item_layout,keys,resources);
listView.setAdapter(adapter);
Log.i("ListView","Adapter Set");
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ListView","click item");
Toast.makeText(getActivity(),"Clicked",Toast.LENGTH_SHORT).show();
}
});
Log.i("ListView","Return View");
return v;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
Just Only clickable and focusable attr in my item layout should be false... and works for me
https://code.i-harness.com/en/q/1561bd1
instead of getContext() use container.getContext() in creating adapter for listview.
Try making the SimpleAdapter as a public variable and put it outside the class onCreateView

onItemClicked() in Fragment

I created a ListView within a fragment and populated it with data from a class called Workout within the onStart() method. I set an Adapter and the list shows up fine. I would like to now be able to get the value of the specific row that the user clicks but am unable to implement an onItemClicked() method. Does anyone know how I can get this value? Thank you.
public class WorkoutListFragment extends Fragment {
static interface WorkoutListListener{
void itemClicked(long id);
}
private WorkoutListListener listener;
private ListView workoutList;
private LayoutInflater inflate;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflate = inflater;
return inflater.inflate(R.layout.fragment_workout_list, container, false);
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.listener = (WorkoutListListener)activity;
}
#Override//ISSUE IS IN HERE
public void onStart() {
super.onStart();
View view = getView();
String[]names = new String[Workout.workouts.length];
for (int i = 0; i < names.length; i++){
names[i] = Workout.workouts[i].getName();
}
if (view!=null){
workoutList = (ListView)view.findViewById(R.id.listView);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(inflate.getContext(),
android.R.layout.simple_list_item_1,names);
workoutList.setAdapter(adapter);
}
//HOW CAN I NOW GET THE VALUE OF THE ITEM CLICKED?
// I TRIED THIS BUT IT'S NOT RECOGNIZED
workoutList.setOnClickListener(new onItemClickListener);
}
Use this:
workoutList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// the 'position' argument is the position you are looking for
}
});
EDIT:
I noticed that you are not initializing the workoutList listView. You should do that in the OnCreateView method:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_workout_list, container, false);
workoutList = (ListView) view.findViewById(R.id.workoutListView); // assuming that your listView's id is "#+id/workoutListView"
return view;
}

Restore spinner position in Fragment, after screen rotation

As I wrote in title the title, I want restore selected position in spinner, after screen rotation. Tried do this by saving position in bundle, and get position from savedInstanceState after rotation in
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { }
or
onViewCreated(View view, Bundle savedInstanceState)
or
onActivityCreated(Bundle savedInstanceState)
but no one works :-/
AddRecipeFragment
public class AddRecipeFragment extends Fragment implements ObservableScrollViewCallbacks {
private static final String SELECTED_CATEGORY = "SELECTED_CATEGORY";
private Spinner categoriesSpinner;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_add_recipe, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
categoriesSpinner = (Spinner) view.findViewById(R.id.categories_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.categories, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categoriesSpinner.setAdapter(adapter);
if (savedInstanceState != null) {
categoriesSpinner.setSelection(savedInstanceState.getInt(SELECTED_CATEGORY, 0), true);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(SELECTED_CATEGORY, categoriesSpinner.getSelectedItemPosition());
}
}
I have also tried setSelection(position, false) with false.
If all that failed, why don't you use a static variable?!
private static int selectedPosition = 0;
and when spinner item selected
categoriesSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and inside onViewCreated() method
categoriesSpinner.setSelection(selectedPosition);

How to detect ListView row click using SlideMenu

I'm using SlideMenu, I've implemented the Fragment and the ListView, what I need now is detect which row the user has clicked from the Slide Menu. How can I possibly do it?
This is the code of the listView:
public class listFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.menu, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
adapter.add(new SampleItem("menu left A", android.R.drawable.ic_menu_search));
adapter.add(new SampleItem("menu left B", android.R.drawable.ic_menu_search));
adapter.add(new SampleItem("menu left C", android.R.drawable.ic_menu_search));
adapter.add(new SampleItem("menu left D", android.R.drawable.ic_menu_search));
adapter.add(new SampleItem("menu left A", android.R.drawable.ic_menu_search));
setListAdapter(adapter);
}
private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
}
ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView.findViewById(R.id.row_title);
title.setText(getItem(position).tag);
return convertView;
}
}
}
As I understand, method
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.menu, null);
will return the ListView object. So you can call setOnItemClickListener() method. Something like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ListView list = (ListView) inflater.inflate(R.layout.menu, null);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// write your click handler here
}
});
return list;
ListFragment have its own ListView by default, but it seems you are overriding default implementation with your custom function onCreateView. You need to be sure that this layout (R.layout.menu) has ListView inside and its id is set to android.R.id.list. Then you should be able to get a reference to that list in onCreateView by finding view by id or later on in any places inside your fragment by calling ListFragment.getListView().
You can then:
override public void onListItemClick (ListView l, View v, int position, long id) which should be called automatically for you
get reference to ListView and set setOnItemClickListener(AdapterView.OnItemClickListener listener) on it manually (just be sure to set it after whole Layout is inflated and available for you)
If that won't work for you, you should put here also R.layout.menu content to help others to help you :)
best regards,
Darek
I used the following method without having to change my code and it worked:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "Row clicked",
Toast.LENGTH_LONG).show();
}
Override the onListItemClick() for your listfragment.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//here "position" is clicked item position
}
Hope this will help for you.

Categories