Add onClickListener to listview item - java

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.

Related

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();

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

Access certain element of listview adapter

I have an Activity with a ListView. This ListView is set with the Class GetAllEntrysListViewAdapter:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ListCell cell;
if (convertView == null) {
convertView = inflater.inflate(R.layout.get_all_entry_list_view_cell, null);
cell = new ListCell();
cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
cell.likeButton = (ImageButton) convertView.findViewById(R.id.heartImage);
convertView.setTag(cell);
}
else {
cell = (ListCell)convertView.getTag();
}
public static class ListCell {
private TextView note;
private ImageView img;
public ImageButton likeButton;
}
In the Activity I now want to change the Image of likeButton on the certain list entry that is clicked, but how can I reach this certain element? Her is what I have until now:
GetAllEntrysListViewAdapter.ListCell listCell;
listCell = new GetAllEntrysListViewAdapter.ListCell();
getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Get the likeButton of this entry and change the Image
Try this code. Replace your
getALlEntrysListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Get the likeButton of this entry and change the Image
}
with
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//first get the elements by doing like
TextView note = (TextView)view.findViewById(R.id.listViewNote);
ImageView img = (ImageView) view.findViewById(R.id.listViewImg);
ImageButton likeButton = (ImageButton) convertView.findViewById(R.id.heartImage);
likeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
likeButton.setBackgroundResource(R.id.icon);
}
});
}
This will be helpful ...thanks
That part should be done in getView() method itself. It should work like this.
Set data in your Adapter
Display data in getView for each item
Once you Like any item, update data with new Like status
Now call notifyDatasetChanged() on your adapter, getView() will update item view accordingly.
You should read ListAdapter first, and see some examples how it works.

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.

How to get selected spinner value from list view in android?

I have a listview with a textview and spinner which has same values for every spinner in the list.both values for textview and spinner are separately in xml files. how can i get selected spinner value for each row in the spinner..according to the relative textview..i m totally beginner to android ..
In your WhateverAdapter add these methods:
public void setOnSpinnerItemSelectedListener(OnItemSelectedListener listener){
this.mSpinnerItemSelectedListener = listener;
}
In your getView() method:
public View getView(int position, View convertView, ViewGroup parent){
//...
viewHolder.spinner.setOnItemSelectedListener(mSpinnerItemClickListener);
//.....
return convertView;
}
Then in your Activity or wherever:
MyAdapter adapter = new MyAdapter(Context....);
adapter.setOnSpinnerItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
myListView.setAdapter(adapter)

Categories