Clickable ListView items in Fragment - java

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.)

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

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

Yet another adding onclicklistener to listview

I am trying to add a onclicklistener and uncomment this line. Here is my modified code stolen from here
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_deviceitem_list, container, false);
ToggleButton scan = (ToggleButton) view.findViewById(R.id.scan);
// Set the adapter
mListView = (AbsListView) view.findViewById(android.R.id.list);
((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
// Set OnItemClickListener so we can be notified on item clicks
//mListView.setOnItemClickListener(this);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = mListView.getItemAtPosition(position);
Toast.makeText(getActivity(), "item is clicked",Toast.LENGTH_SHORT).show();
}
});
return view;
}
However, this I need to show the text of device name and the address instead of "item is clicked". Any help?
Toast toast = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);
toast.setText(device.getName()+":"+device.getAddress());
toast.show();

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.

Add onClickListener to listview item

I have a list view of a thumbnail and text next to each other. I am trying to figure out how to add a onClicklistner to each list item so when the user selects the text or thumbnails the full image will pop up. Below is my list object and adapter and the lazyAdapter code.
MainActivity:
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings, mImages);
list.setAdapter(adapter);
LazyAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);;
ImageView image=(ImageView)vi.findViewById(R.id.image);
text.setText(image_name[position]);
imageLoader.DisplayImage(data[position], activity, image);
return vi;
}
EDIT
THis is what I ended up using.
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
Toast.makeText(MainActivity.this, "Show Full Image", Toast.LENGTH_LONG).show();
}
});
You can use mStrings and mImages in your OnItemClickListener. Assumed from your LazyAdapter that they are arrays. Maybe you can try something like this.
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
String text = mStrings[position];
YourImageClass img = mImages[position];
Intent i = new Intent(MainActivity.this, ShowFullImageActivity.class);
i.putExtra("TEXT", text);
i.putExtra("IMAGE", img); // <-- Assumed you image is Parcelable
startActivity(i);
}
}
What may help you is implementing an AdapterView.OnItemClickListener.
More info can be found here http://developer.android.com/guide/topics/ui/binding.html.
Note this is used for each row in the ListView and not the individual TextView or ImageView in each row.

Categories