How to Handle Android ListView Subitems Click Events using SimpleAdapter? - android

I have been developing an android application and i am stuck in the middle.
The problem is i am using SimpleAdapter to do the adapter stuff and show items in the Listview and as far as i know i cannot override the getView() method of SimpleAdapter class to bound click listeners to the items.
There is a other way to handle click events of sub items like using XML, you can write in the XML like android:clickable="true"and android:onClick="clicklistenr", using this i can get the item but my problem is if i use this then i cannot get the position of the adapter which i need to get adapter item values and handle other tasks. So i am stuck here any help would be appreciable. thanks.
For example i have a ListView which contains one image, TextView, like Button, share Button in each of its items. And there is no way i can find that either its image or button clicked using setOnItemClickListener. So i need a way to handle click events of these sub items of a ListView, i am using SimpleAdapter.

Just call listView.setOnItemClickListener() with your implementation of the listener.
and use like
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
Where list=(ListView)findViewById(R.id.list); and list.setAdapter(your_adapter);
For More details Follow: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Hope It Will Help You.. :)

You can use like
myListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
String result= (String) obj.get("name");
Log.d("Yourtag", name);
}
});

First of all your problem is how to handle the items of a custom listview.. not sub. Anyways...
If you are using SimpleAdapter , you may use getView().But if you are using SimpleAdapter you don't need to use the getView() as the it handles the mapping of data to your layout via the resource. For inforamtion check the SimpleAdapter in developer site.
Another thing is there is not mandatory for using any particular adapter. You can create any adapter class which will extends the BaseAdapter which will implement the getView().
Inside getView() you can able to inflate your custom layout(which contains the image,button etc..) to your view or convertview.something like:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.yourcustomlayout, null);
//Initialize your custom views here
TextView someText= (TextView) vi.findViewById(R.id.tvSometext);
Button likeButton = (Button ) vi.findViewById(R.id.btnLike);
//put data or call onclicklistener or
//whatever you want to do with custom views
likeButton .setOnClickListener(...){
//on click
}
}
Ans simply call this through your constructor with some data and appropriate context.

Amir,
I am not sure you found the solution or not. You can do this way:
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
//for each subitem, set click listener & set tag having position as
// value.
ImageView imv = (ImageView)view.findViewById(R.id.live_view);
imv.setTag(i);
imv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("INFO","Live view clicked");
//This will give you position in listview.
int position = (int) v.getTag();
}
});
}
I hope it will help you and others looking to find relevant answer.
May be there is some other solution too, but it will surely work.

Related

Get access to particular item's layout in list view

In my android app my Main Activity extends List Activity. I store there elements called Items, their layout is defined by itemLayout xml file and I use custom adapter (called ItemAdapter) to transfer data to List View in Main Activity. In itemLayout there's an ImageView and my aim is to change its image when user clicks on the particular item in list view. In my opinion the easiest way to achieve that is to get access to particular item's (the one that was clicked) layout, there find the ImageView and call method setImageBitmap. How can I "find" this layout of clicked item? I tried many things in method:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
but nothing worked. Do I have to add anything to my ItemAdapter? Or is it possible to do in onListItemClick(…) but I can't find out how?
You're thinking about it in slightly the wrong way. Adapter's are a way to map data to views. So if you want to change how a particular view looks for a given position, you need to change it's correlating data so that the rendered View then changes. Attempting to modify a view directly kinda goes against how adapters are meant to be used.
So in your case, when a user clicks on an item. Find that item in your adapter via the position number. Then update the item and ensure notifydataset is called. Meanwhile, your adapter's getView() will then handle displaying the appropriate image.
As I understood you need to modify clicked item layout, right? Use argument from onListItemClicked:
v.findViewById(<your_img_view_id>)
For better performance use view holder.
onListItemClick is fired when you press on an element of the ListView. If you want to retrieve the element in the dataset, you can simply invoke
l.getItemAtPosition(position)
the returned value has to be casted to the specific object
Yes It is possible in your custom adapter class in the getView() mtheod u can change imageview bitmap by clicking on it
see this code
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
rowView = inflater.inflate(R.layout.playlist_item, null);
final ImageView im = (ImageView) rowView.findViewById(R.id.favorite);
im.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do your stuff here
}
});
return rowView;
}

Get the position of Button in List View

I have one problem.
I want to get the position of Button in List View.
In below image when I clicked in Edit or Delete button i just want its position in List View.
How it possible?
This is my Office Management Screen. All the data getting in ListView run-time from my web-service.
I get the data in List View by using this code-
ListAdapter adapter = new SimpleAdapter(Office_Mgmt.this, officeList, R.layout.office_mgmt_list_model, new String[] { "office_name" }, new int[] { R.id.label});
setListAdapter(adapter);
You can create your custom adapter, passing the context of your app as argument to set the click:
listView.setAdapter(new PesquisaAdapter(this, anunciantescidades, this);
Then, in the constructor of your adapter you will have a OnClickListener to recieve the argument this you passed:
public PesquisaAdapter(Context context, List<Anunciante> anunciantes, OnClickListener onClick1)
In the getView method of the adapter, you can set onClickListener of the button:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.your_custom_layout, null);
Button btn = (Button) v.findViewById(R.id.yourbutton);
btn.setOnClickListener(onClick1);
}
In your java, you can implements onClickListener, and then do what you want with your button:
#Override
public void onClick(View v) {
if(yourbutton.getId() == v.getId()){
final int position = listView.getPositionForView((LinearLayout)v.getParent());
}
}
So, you will have the position in the list that you clicked, and you can manage the click of the edit and delete separately.
Hope it helps!
implement onItemClick(AdapterView<?> parent, View view, int position, long id) for the listview.
Position and view can be identified from the handler. You can set additional info using setTag while creating the view inside the Adapter's getView method and retrieve in the handler and then verify in the handler if required.

Android Listview simpleadapter onClick, how to find which view was clickon?

I have a list view that is built using a simpleAdapter, each row is defined by a Layout containing a TableRow and a few TextView.
What should I do to know on which TextView (in the layout) was clicked on?
I am able to get the listview row (position) if I use a OnItemClickListener on the Listview, however it dosen't tell me on which TextView on that row was clicked on.
I would like to take different action depending on which TextView a user click on in a listview
listview
-----------------------
TextView1 : TextView2 | <--Row1
-----------------------
TextView1 : TextView2 | <--Row2
-----------------------
TextView1 | TextView2 | <--Row3
-----------------------
I would like to be able to tell, that row 2's TextView2 was click on.
Hopefully I am explaining myself well enough.
Thanks you
you can use below code in your List Activity
setListAdapter(new ArrayAdapter<String>(this, R.layout.your name, String Name));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
If the click is being handled by onItemClick, then you cannot know from this which View WITHIN that View was clicked. An OnItemClickListener just handles clicking a list item. What you might try is using a custom adapter in which you grab hold of all the Views in the row in getView() and set onClickListeners on them there. Or you can define the onClickListeners directly in your View class if possible.
you can handle the click of different view's in Adapter.The Adapter must be customized
I needed to do something similar to set a tag on elements within the list view, so I could find which button was clicked
myAdapter = new SimpleAdapter(this, myArrayList, R.layout.list_item,
new String[] {"ItemID", "ItemText", "ItemImage"},
new int[] {R.id.item_id, R.id.item_text, R.id.item_image})
{
#Override
public View getView (int row, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
view.findViewById(R.id.item_id).setTag(row);
view.findViewById(R.id.item_text).setTag(row);
view.findViewById(R.id.item_image).setTag(row);
return view;
}
};
I added an onClick event for each item in the layout
<TextView
...
android:clickable="true"
android:onClick="myClickEvent"
...
/>
And then in my onClick events I had something like
public void myClickEvent(View v) {
Object o = v.getTag();
if (o instanceof Integer) {
int row = (Integer) o;
// do something
}
You can either use one onClick event per item in the view to determine which item was clicked, or you can have one onClick event for all and modify the tag to contain both the item and the row.

ListView and hidden Id. How it is possible?

I'm developing an application for android and now I have implemented a ListView that shows a list of courses, connected to a database.
I would like to know how to include, with the name, an hidden id (that come from the db) so that once the user click on the elements the app goes to the relative view of the selected courses.
And how can I maintain the id during the navigation inside the course-view?
At the moment my code just load the name of the courses from the db and set in the list view:
ArrayAdapter<String> result = new ArrayAdapter<String>(CourseActivity.this, android.R.layout.simple_list_item_1);
for (CourseRecord c : get())
result.add(c.getFullname());
lv.setAdapter(result);
obviously I'm able to do also c.getid() but I don't where to put the id.
Thank you very much.
P.S.: Maybe does someone have also a really nice graphics of list view?
change your array adapter like this.
private ArrayAdapter<String> result = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
v.setTag(getMyIdForPosition(position));
return convertView;
}
};
and have an item click handler to recieve the selected ids
private OnItemClickListener itemClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
String myId = (String)v.getTag();
doYourStuff(myId);
}
};
assign the listener to the list
myList= (ListView)findViewById(R.id.history);
myList.setOnItemClickListener(itemClickedHandler);
You can store the id in a hidden TextView. In the list item XML, add 'android:visibility="gone"' to the TextView. Likewise in the click handler you can read the id from the textview.
You can also store id using setTag(Object object) method of a View. Use getTag() method to extract that id from that view.

Help regarding onClick() event on an item of ListView custom row layout

I have a ListView whose rows are formatted by me. Each row has a mix of ImageView and TextView.
I have also implemented my own adapter and am able to draw each row through it.
Now, I would want something like this-
User clicks on an ImageView (not anywhere else on the row, but only this ImageView should respond to clicks)
I get to know the position of the row whose ImageView was clicked.
I have tried many things for this and have wanted my code to be as efficient as possible (in terms of overkill).
Currently i can capture the click event on that particular ImageView only, but I can't know which row was clicked.
I have provided an attribute in the Row XML like this-
<ImageView android:id="#+id/user_image"
android:padding="5dip"
android:layout_height="60dip"
android:layout_width="60dip"
android:clickable="true"
android:onClick="uImgClickHandler"/>
And in my code, I have a method like this:
public void uImgClickHandler(View v){
Log.d("IMG CLICKED", ""+v.getId());
LinearLayout parentRow = (LinearLayout)v.getParent();
}
I can get the parent row (perhaps) but am not sure how to go further from here.
Can someone please help?
Please refer this,
Me just writing the code to give you idea, Not in correct format
class youaddaper extends BaseAdapter{
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflate = LayoutInflater.from(context);
View v = inflate.inflate(id, parent, false);
ImageView imageview = (ImageView) v.findViewById(R.id.imageView);
imageview.setOnClickListener(new imageViewClickListener(position));
//you can pass what ever to this class you want,
//i mean, you can use array(postion) as per the logic you need to implement
}
class imageViewClickListener implements OnClickListener {
int position;
public imageViewClickListener( int pos)
{
this.position = pos;
}
public void onClick(View v) {
{// you can write the code what happens for the that click and
// you will get the selected row index in position
}
}
}
Hope it helped you
Another option is to use the methods setTag() and getTag() of the view. You set it in your getView like this:
imageView.setTag(new Integer(position));
Then in the onClick() you can find the tag by:
Integer tag = v.getTag();
This will then be used to correlate the image view to the position of the listview item.
Note that this approach will give problems if the listview can lose items from the middle, so that the item positions change during the lifetime of the listview.
you can simply do like this:
in the getview method of our adapter
Button btn1 = (Button) convertView.findViewById(R.id.btn1);
btn1.setOnClickListener(mActivity);
further you can handle the onclick event in your activity,,
for the context of the activity here mActivity just pass the this in the constructer of the adapter and cast it here into the activity like
MyActivity mActivity=(MyActivity)context;
in the adapter.
thanx
This appears to work in a ListActivity whose item layout contains an ImageView with android:onClick="editImage":
public void editImage(View v) {
int[] loc = new int[2];
v.getLocationInWindow(loc);
int pos = getListView().pointToPosition(loc[0], loc[1]);
Cursor c = (Cursor) adapter.getItem(pos);
// c now points at the data row corresponding to the clicked row
}

Categories

Resources