I have a custom list view with simple text and button, this button open a dialog that allow user to delete or save a file, but i want to get the view of the row when i push this button to perform a delete animation.
How can i get the view of this specific row, inside on a button click listener?
I use a custom array Adapter with Base Adapter!
extends BaseAdapter implements View.OnClickListener{
Thanks in advance, i don't know if it's necessary to put here the code, it's so long.
you should use the custom adapter for your list view and add tag to your button that represent the position of your row and after that in your clicklistener you should get the tag and find out that which row has been clicked .
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Button yourbtn= (Button) v.findViewById(R.id.yourbtnid);
yourbtn.setTag(position);
yourbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Button btn = (Button)arg0;
int position = Integer.parseInt(btn .getTag().toString());
}
}
You need to do this from the ListView by using ListView.setOnItemSelectedListener(). The OnItemSelectedListener returns the View.
If you're implementing your click action inside the Adapter code itself (which I think is not the best way to do it), you can get an instance of the whole strip by calling getParentView() as many times as needed, depending on how deep into your parent layout the button is.
Related
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;
}
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.
I have a ListView items/cell that has a few layers, relative, linearLayout etc.
I tried doing v.getParent() inside the onClick of the button but it just went one layer up. How can I get a reference to the root of the current element inside onClick event handler?
Here is my getView()
Button v = (Button)findViewById(R.id.myButton);
v.setOnClickListener(new OnClickListener(){
onClick(View v) {
//Right here I want to find another view inside the cell.
View otherView = ???.findViewById(R.id.myOtherViewInListItem);
otherView.setBackgroundColor(...);
.........................................
}
});
I'm imagining that this button is inside the ListView element. Based on this assumption:
create only one OnClickListener. The way you're doing you're creating adapter.getCount() OnClick listener and waste memory like crazy.
Maybe you want to completely remove the button and use the OnItemClick to track clicks on your whole ListView element, this will pass to you the reference to the whole View.
If you really must have the button, inside your getView you store a reference to the parent, something like that:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// do whatever you're doing to get the view, let's call it v
Button b = (Button)v.findViewById(/* your button id */ )
b.setTag(v)
}
then on your click listener you can:
onClic(View v){
View parent = (View)b.getTag();
}
and if that answer works for you (I know it does) accept it as correct like Daniel Martinus pointed out.
Inside onClick(View v)
just call v.getViewParent(); then you will have a view from which you can call the find for the others.
in my app i use list view with image button called buy video. i populate video list from urban air ship. if i press buy button the video is downloaded.
how to hide image button at a particular position in a list view. i did but the right position and some other position image button also hide. please help me.
i check whether installed(downloaded) or not by the code:
if(statusOfProduct.equals("INSTALLED")){
....
buy.setVisibility(View.INVISIBLE);
}
main code :
public class InventoryListActivity extends ListActivity {
...........
public class InventoryAdapter extends BaseAdapter implements Observer,OnClickListener
{
..........
public View getView(int position, View convertView, ViewGroup parent) {
Product product = (Product) getItem(position);
View view;
if(statusOfProduct.equals("INSTALLED")){
Log.e("vocab","if-status");
Log.e("vocab",product.getIdentifier());
buy.setVisibility(View.INVISIBLE); // hide right position and some other position image button also.
}
}
please help me. i do not the reason.
Though it is not clear from your code, this may solve your problem :
In place of
buy.setVisibility(View.INVISIBLE);
Use
((Button) pConvertView.findViewById(R.id.buyButtonID)).setVisibility(View.INVISIBLE);
I wasn't totally sure what you were asking, but there is a difference between VIEW.INVISIBLE and VIEW.GONE. Make sure you're using the right one for your needs.
try this
yourBtn = (ImageButton) view.getRootView().findViewById(R.id.YOUR_BUTTON_ID);
and then try this
yourBtn.setVisibility(View.GONE);
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
}