I am currently designing a store for a game and am facing a problem with the list of items to buy:
I have a ListView containing RelativeLayouts as items - in each RelativeLayout are two inner RelativeLayouts, with the items in them.
I want to find out if the user clicks on BUY in item1 or item2 - but I found no way to get this in an onItemClickListener.
The problem is, if I use onClickListener instead of onItemClickListener, I have the item (one or two), but not the row - so I always have either the row or the column.
Basically, I need to get the ID of the actually clicked Item from within the onItemClickedListener - is there any way I can do this?
Add a Tag to your Button (button.setTag(position)) in the AdapterĀ“s getView() and then in onClickListener call view.getTag() to retrive an index
You should add onClickListner to your Child RelativeLayouts.Do it from your adapter getView method.
you set onClickListener to your buy Button. To the buy button you can attach tag of the position and retreive it in its onClick method.
Inside getView()
btnBuy.setOnClickListener(this);
btnBuy.setTag(position);
in onClick method
int position=Integer.parseInt(view.getTag().toString());
Related
My app have an button(B1) custom ListView that item view include : checkbox,textview
Goal is I press button B1 then all checkbox in ListView will be setVisible and I had done it BUT THIS IS WORK FOR WHAT IS SHOWING ON SCREEN. When I scroll Listview in case multi-rows that the others not be visibled
Let's see my explanation:
- Example that there is 10 items in list data -> Listview on current screen just show 5 items ( 10 items is still visible when scroll)
- When press button B1 that just CHECKBOX of 5 items is visible, the others is not working
I mean I want to change layout of view item right on Activity
Because you are only updating the views of layout. You need to also update your data set (array of your adapter of listview) but I know you don't have a boolean variable in your data set to decide whether checkboxs are visible or not. First you need to add variables. After button clicked, don't update views of layout. Only change the datas (boolean variables) and call notifyDataSetChanged() method of your custom adapter. notifyDataSetChanged() method will recall your getView() method of your adapter for all views visible in your screen. Values will be updated correctly if you check visibility of views in your getView() method. Other items after scrolling will be correct because your getView() method is correct.
If you don't understand the answer add your OnClickListener of button and your custom adapter class. I will update them.
Edit: You have focused on the wrong problem. Your solution isn't related to updating the layout. You have problem about logic of how listView should works.
I have a scrollable listview that has 2 textViews, 3 imageButtons and 1 checkBox in each row.
In the Header of the listView, I have a delete button just above the column containing all the checkboxes of the listView.
I have to perform the delete operation of objects in the listView when 2 or more checkBoxes are checked randomly by scrolling the listView and thereafter the delete button at the top is clicked.
But the problem is that i am not getting the correct poition of the checkbox that was selected. Moreover, sometimes i get the correct position but still the object to be deleted passed is wrong. Hence the entire functionality is affected maybe due to the scrolling nature of the list.
Should i take the position in the holder of the adapter class and also bind the state of the checked or unchecked checkbox with my object.
And should I use checkBox.setOnCheckedChangeListener() or deleteButton.setOnClickListener().
If i use the latter one, then how to get all the corresponding objects of the list whose checkboxes were checked before pressing the delete button?
And where should all the related code be placed..in the listAdapter class or in the activity?
Please help me find a solution to this problem..
first of all in your getView() method you should set a specific Tag to each checkBox. For example: check1.setTag(position) then you should implement both OnCheckBoxChangeListener for your checkBoxes and OnClickListener for your delete button. As you know setting of onCheckBoxChangeListner have to be in getView() method. Then you add positions of list that their checkBox is checked to the a ArrayList with the help of getTag() method of chechBoxes in onCheckedChanged() method.
Try getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {}
I have a list view where items have a text view and a button. I have managed to make list view onclick and button on click work together. The problem is that when I click the button I don't really know what list view item index it belongs too. Is there any way to know that? I need this to pass it to a "CRUD" for editing, etc...
You can use setTag and getTag here to get the position of the Button Clicked in the ListView,
Something like,
button.setTag(position); // in your getView() method
and then,
int cur_pos = (Integer)v.getTag(); // inside onClick of Button in getView() method
Some options:
You can have separate OnClickListener instances for each button.
You can call setTag() on your button to store arbitrary data (e.g. index or identifier) and retrieve it later with getTag()
An other solution would be to reproduce the button click on its parent, when the user tap on the button:
http://developer.android.com/reference/android/view/View.html#performClick()
I am trying to create a listview that includes a "remove" button on every list item. The item will be removed from the list when it is clicked. Can any one guide me to how can I actually detect the position of the list item if I am using a button onclicklistener? and should the onclicklistener be placed in? (should it be in the list adapter, custom extended listview class or in the activity which holds the listview?
Your implementation should be in the ListView's adapter. When you implement the getView() method in your list adapter, add the remove feature for the button. The getView() method will pass you the position in the array of that item. Here's some what of a guide to custom ListViews with an ArrayAdapter. Your button's onClickListener should simply call adatper.remove(item) to remove the item.
I have a listView using a custom adapter. Each row contains a button and some other Views. I want to be able to click either on the button, or on the row itself (to edit the item of the list that is clicked).
Setting an onItemClickListener in the activity won't work because of this problem
I think I have to set an onClickListener in the getView() method of my adapter for it to work properly.
I would like to use my activity's onClickListener, in order to use a startActivityForResult() when the row is clicked, in order to have something returned to my activity when the item edition activity is over.
How can I do that?
Thanks!
You'll need to add an onclick listener to every button you add to every row. The best way to do this is probably to make your own custom layout in code, and every time you create a new view in your adapter, set the onclick listener in the layout code.