How to detect ListView row click using SlideMenu - java

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.

Related

Selecting Spinner Item doesen't trigger onItemSelected - method

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

Clickable ListView items in Fragment

So I created ListView in Fragment. Here's the code:
public class HomeFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
//INFLATE THE FRAGMENT LAYOUT FILE
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_home, container, false);
//CREATE DATASOURCE
String[] datasource={"Potisak s klupe",
"Potisak s klupe bučicama",
"Potisak s kose klupe",
"Potisak s kose klupe bučicama",
"Potisak s kontrakose klupe",
"Potisak s kontrakose klupe bučicama",
"Potisak na spravi",
"Razvlačenja na ravnoj klupi",
"Razvlačenja na kosoj klupi",
"Razvlačenja na kontrakosoj klupi",
"Razvlačenja kablovima na klupi",
"Propadanja",
"Sklekovi",
"Sklekovi s nogama na povišenom",
"Crossover kablovima",
"Razvlačenja na leptir mašini",
};
//CREATE ADAPTER
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.rowlayout_prsa, R.id.labelrowlayout_prsa_tekst, datasource);
// BIND ADAPTER TO THE LISTFRAGMENT
setListAdapter(adapter);
//RETAIN LISTFRAGMENT INSTANCE ACROSS CONFIGURATION CHANGES
setRetainInstance(true);
return rootView;
}
// HANDLING ITEM CLICK
public void onListItemClick(ListView l, View view, int position, long id){
ViewGroup viewGroup = (ViewGroup)view;
TextView txt = (TextView)viewGroup.findViewById(R.id.labelrowlayout_prsa_tekst);
l.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent (getActivity(), PotisakSKlupe.class);
startActivity(intent);
}
});
}
}
But I can't get to another activity when first list item is clicked with intents. Also tryed with several other methods found on Internet, but no luck. There's no much about fragment list views..
What am I missing here?
try this i think u had not called this method in your code.please call and then try.
public void onListItemClick(ListView l, View view, int position, long id){
ViewGroup viewGroup = (ViewGroup)view;
TextView txt = (TextView)viewGroup.findViewById(R.id.labelrowlayout_prsa_tekst);
l.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent (getActivity(), PotisakSKlupe.class);
startActivity(intent);
}
});
}
Hope it helps.
first override this method.
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
after the onCreateView, onActivityCreated method is called. so put your onListItemClick() inside onActivityCreated method. Now set the list item to be clickable.
listView.setClickable(true);
and then,
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startActivity(new Intent(getActivity(), AddActivity.class));
}
});
This might help you.
(Check the variable name that I used and yours.)

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;
}

Listview item click applying to other items

I have this on my main activity:
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
TextView wrapper = (TextView) v.findViewById(R.id.title);
VideoModel o = (VideoModel) videoAdapter.getItem(position);
wrapper.setText(o.title);
}
});
This code (as you would guess) changes the textview of a list view row (which is a layout).
The problem is that for some reason when I click on the first row, the text changes, but it also changes on the third row (with the same text).
Any toughts?
If you want to see the whole code, refer to How to call method from activity to change layout?
Don't change Views in setOnItemClickListener instead change your data and notify adapter so that adapter will change your UI.
Example:
Model
class VideoModel {
public String title;
public String videoUrl;
public String imageUrl;
public boolean isTitleVisible;
}
Adapter
public class VideoAdapter extends BaseAdapter{
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
VideoLayout view;
if (convertView == null) {
view = (VideoLayout) LayoutInflater.from(context).inflate(R.layout.video, parent, false);
}else{
view = (VideoLayout) convertView;
}
VideoModel item = (VideoModel) getItem(position);
if (item != null) {
view.prepare(item);
if(item.isTitleVisible){
view.changeTitle("");
}
}
return view;
}
}
On click listener:
#Override
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
videoItems.get(position).isTitleVisible=! videoItems.get(position).isTitleVisible;
videoAdapter.notifyDatasetChange();
}
This is just an example in which you change your view model and just notify adapter to change the view in onITemClick

android.R.layout.simple_list_item_checked not toggling in ListView

Here is my custom adapter:
...
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(android.R.layout.simple_list_item_checked, parent, false);
return v;
}
...
Now this visually appears to be exactly what I needed. The problem is that I can't get toggle the checked state when I click on the listview item. Any solutions?
I quickly found a sloppy way of doing this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView textview = (CheckedTextView)v;
textview.setChecked(!textview.isChecked());
}
Mohit,
Why don't you try this:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
CheckedTextView textview = (CheckedTextView)v;
textview.toggle();
}

Categories