Handling button event in each row of Listview issue - android

I am new to android and am trying to develop a new android app. But I am struggling to siolve one of the problems in my project.
I am using a listview extended from baseadapter and need to add a button in each row of thelistview. When I click on the button in any row of the listview, I want that it should be removed. However when I do so, some of the other buttons also get removed in the listview.
How can I solve this problem? Thank you..

You have an adapter, activity and some sort of data source
In your adapter you attach some data to buttons to be able to tell one from another:
public class ExpAdapter extends ListAdapter {
#Override
public View getView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
/* SOME CODE HERE*/
convertViewButton.setTag(buttonId);
return convertView;
}
/* SOME CODE HERE*/
}
in your activity you mark button id as the one to be hidden:
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
storageOfHiddenButtonsIds.add((Long)arg1.getTag());
}};
and then ListAdapter changes like this:
#Override
public View getView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
/* SOME CODE HERE*/
convertViewButton.setTag(buttonId);
if(storageOfHiddenButtonsIds.contains(buttonId))
{
convertViewButton.setVisiblity(View.GONE);
}
return convertView;
}
and when you want your adatper to change you, don't forget to call
this.expAdapterAllTaks.notifyDataSetChanged();
Sorry for any errors in my code, but i just wanted to give you an idea.

I faced same type of problem. ListView's setOnItemClickListener not works if you add item like a button on every listView item. Solution is use onClick in the list Item layout(which you use in custom adapter file) as
<ImageButton
android:id="#+id/my_delete"
android:onClick="onDeleteButtonClickListener"
... and so on />
where onDeleteButtonClickListener is a method in the activity where you set the adapter in listview.
public void onDeleteButtonClickListener(View v) {
// your code
}
here listItem means the individual row item of a ListView
Helpful Link: Button in ListView item

Related

How to Handle Android ListView Subitems Click Events using SimpleAdapter?

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.

android OnItemLongClickListener listview to display listview checkboxes

I would like to create an android app which displays a list in a listview and then allow the user to delete an item when on onItemlongclick and then displays checkboxes to select which items to delete.
I know it have to call the OnItemLongClickListener but do not know how to implement that. Any ideas to do this?
lv.setOnItemLongClickListener(this);
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int pos, long id) {
// TODO Auto-generated method stub
return false;
}
I'm so lost in ideas how to do this. I googled but unfortunately didn't find any relevant tutorials.
Help is much appreciated. Thanks.
I did this just recently, but it was an ad hoc fix, so this might not be the best way.
In the layout for my ListView items (rows), I included a CheckBox whose visibility will be toggled, but is initially not visible, i.e., View.GONE.
Then, in my Adapter, I included a member boolean variable, selectable, and a public method to set it and refresh:
private boolean selectable = false;
public void setSelectable(boolean selectable)
{
this.selectable = selectable;
notifyDataSetChanged();
}
In the getView() method of the Adapter, selectable is checked and the CheckBox's visibility is set accordingly.
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
...
cbx.setVisibility(selectable ? View.VISIBLE : View.GONE);
...
}
I used a ToggleButton to change the selection mode, but in your case, you'll need to do something a little different. I would add an additional method to the Adapter:
public boolean isSelectable()
{
return selectable;
}
Then, you can toggle the selectable state on long clicks:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v, int pos, long id)
{
...
yourAdapter.setSelectable(!yourAdapter.isSelectable);
...
}
To respond to clicks on list items, you need to put this in onCreate():
ListView lv = (ListView) findViewById (R.id.list);
lv.setOnItemLongClickListener (this);
lv.setLongClickable (true);
Then, onItemLongClickListener will be called whenever the user long-touches a list item. Put a breakpoint there and be satisfied you understand this much.

How can I eliminate the dependency from of a ListView's onItemClick/getView and its row types?

As a simplified example, consider a ListView that can contain both sub-categories and book titles. If a book title is clicked, a new activity should start that shows the cover image. If a sub-category is clicked, a new list of books and categories is displayed.
A Row interface is defined as follows.
interface Row {
void onClick();
void draw(View v);
}
I would like to know how to prevent a dependency from the ListView's ArrayAdapter as well as from the implementer of onItemClickListener on the Row implementers (e.g.,Book and Category).
One of the forces driving this requirement is the "don't repeat yourself" (DRY) principle: the ArrayAdapter implementation does not need to change when new row types are introduced.
Forgive my chicken-scratched "UML" below, but here's how I do it.
class ListAdapter extends ArrayAdapter<Row<?>> {
...
public View getView(int position, View convertView, ViewGroup parent) {
View view = listViewRowViewRecycler.getView(convertView);
rowProvider.get().getRow(position).draw(view);
return view;
}
}
The implementer of OnItemClickListener has a method like this:
void onItemClick(AdapterView<?> adapterView, View view, int rowIndex, long rowId)
{
rowProvider.onItemClick(rowIndex);
}
Then RowProvider has a method like this:
void onItemClick(int rowIndex) {
rowList.get(rowIndex).onClick();
}
Then the Row interface has a method with signature void onClick() and there are Category and Book implementations of Row that provide the necessary behavior.
Another possibility is to use the setTag/getTag methods on each item's View in the list - this allows you to attach the 'Book' or 'Category' to its appropriate row.
For this to work each of the 'listed' items should implement a common interface that has an appropriate method to be called when it is clicked.
For instance:
interface Selectable {
void onSelected(); // Add parameters if necessary...
}
In your list adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View itemView; // Logic to get the appropriate view for this row.
Selectable theObjectBeingRepresented;
//
// Logic to assign a Book/Category, etc to theObjectBeingRepresented.
// In this example assume there is a magic Book being represented by
// this row.
//
Book aMagicBook = new Book();
theObjectBeingRepresented = aMagicBook;
// If all objects that will be contained in your list implement the
// 'Selectable' interface then you can simply call setTag(...) with
// the object you are working with (otherwise you may need some conditional logic)
//
itemView.setTag( SELECTABLE_ITEM_KEY, theObjectBeingRepresented );
}
Then when an item is clicked:
void onItemClick(AdapterView<?> adapterView, View view, int rowIndex, long rowId)
{
Selectable item = (Selectable)view.getTag( SELECTABLE_ITEM_KEY );
// Depending on how you are populating the list, you may need to check for null
// before operating on the item..
if( null != item ) {
item.onClick(); // Appropriate implementation called...
}
}
A bit more detail on the setTag method:
setTag documentation
Hope this helps...

ExpandableListView with button inside childs

I'm writing an ExpandableListActivity with a SimpleCursorTreeAdapter that queries a DB. I added a button inside very child, and now i'm having bad time getting info on the item from the list that its button is clicked. I think that my problem is that i don't well understand how to handle data within the adapter. Most of the examples are about listadapter like this one.
My adapter got getChildView implementation and what i cannot achieve is to retrieve the _id of the clicked group from the original cursor. Because i didn't know how, i tried in the following code to get the name from the group textview but in every child that i click i get the name of the first item in the list.
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
v.inflate(getApplicationContext(),R.layout.productsexpchilditem, null);
Button button = (Button)v.findViewById(R.id.button1);
String name=((TextView)parent.findViewById(R.id.txt_exp_products_name_item)).getText().toString();
button.setTag(name);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ProductsActivityExp.this, "TOAST "+v.getTag(), Toast.LENGTH_SHORT).show();
}
});
return v;
}
BTY I didn't implemented the getGroupView method, cause when I did it I lost all the displayed text in the groups (but not in the child). I really think the work with cursorTreeAdapter got specific implementation different then listadapter.
I didn't used ExpandableListView, but it seems strange that you get name value via parent. Its not surprising that you get same name for all buttons.
Can you explain in details, what you want to do?

How to keep the ExpandableListView Opened?

I am working on the ExpandableListView I have completed the work, now only one thing that I want to do is I don't want the ListView to be DropDown on click of the Expandable List View rather I want to show it opened with all the Items displayed inside without performing any click on them.
Can anyone tell me how can i do that particularly.
Do this for every one of the groups to expand them:
listView1.expandGroup(int groupPosition);
If you want to prevent group collapse, then do this:
listView1.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()
{
public boolean onGroupClick(ExpandableListView arg0, View itemView, int itemPosition, long itemId)
{
listView1.expandGroup(itemPosition);
return true;
}
});
Solution:- To Keep The Expandable list in Expanded mode all the times
It is actually very simple you dont need to do anything but just put this one line in your getGroupView of your adapter and your expandablelistview always be in opened/expanded state:--
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
your_expandableListview.expandGroup(groupPosition);
return v;
}
This works for sure... Enjoy..!
Open ExpandableListView Group:
listView.expandGroup(itemPosition);
Collapse ExpandableListView Group:
listView.collapseGroup(itemPosition);

Categories

Resources