I found a similar question about scrolling listview and button click but it did not help me. My issue is:
I have a listview with custom rows. I have two different states of listview; the switch between the states is a button at the bottom of the screen. The second state has delete buttons in every row. When I click on delete button in a specific row, that row is removed from database and listview is updated. Everything works great except I need to click the delete button twice in order for it to work. Below is my code for handling the clicks. flag==1 is the second state of the listview.
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
View main = parent.getChildAt(position);
TextView delete = (TextView)main.findViewById(R.id.delete_button);
if(flag==0){
switchToItemsView(id);
}
if(flag==1){
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDbHelper.deleteList(id);
updateListView();
}});
}
}
I tried to set parent view's focusableInTouchMode attribute to false as suggested in another post but it did not help.
If you can help me solve this I will be grateful,
Thank you in advance.
Probably you have focus. Remove it.
android:focusableInTouchMode="true"
android:focusable="true"
You need to declare the onClickListener before the actual flag check; in your case the view changes and with that the flag, and the listener is set. The next click actually triggers the listener.
Not related, but you should change your second if to an elseif since there is no way it would be called if the first one is called.
After spending hours I figured out how to do it:
I moved my click listener from my main activity class to my custom ListAdapter class and modified it a little like below;
deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int rowid = Integer.parseInt(rowIds.get(position));
mDb.deleteList(rowid);
lists.remove(position);
notifyDataSetChanged();
}
});
Now it works great. When I click delete button it removes the list both from the ArrayList (the one used in ListAdapter) and from database.
Related
I have an Activity a with a ListView lv inside.
I have set the lv.setOnItemClickListener().
When the user clicks on a list item i want another list view (ListView lv_2) in the Activity a to be refreshed.
Problem is that i cannot access the Parent Activity a inside lv.setOnItemClickListener() am i right?
I studied some of custom event listeners but i don't understand how to use them in this particular case.
So how can i do this?
lv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
lv2Adapter.notifyDataSetChanged();
lv2.setAdapter(lv2Adapter);
lv2.notify();
}
});
I am using listviews instead of button in my application. I want to set OnClickListener() on listview instead of setOnItemClickListener().
here is my code:
listview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), BillingActivity1.class));
}
});
can someone suggest a method to do OnClickListener()?
thank you
Ok, I understood your problem.
Lets analyze cases:
Listview with items
If your ListView has some items, the good approach is to set anyway an onItemClickListener and, based on which item has been clicked, do something. You can also do the same thing for each item without considering which item has been pressed, but this is still the best approach.
ListView with no items
From Docs:
the list view will be hidden when there is no data to display.
ListView (usually) has the height set to wrap_content, so even setting the onClickListener on an empty list won't work since the list will result having height of 0, being not able to be clicked (you can't click a view with no height since it is not visible).
Perform actions on an empty ListView
If, as it looks like, you need to do some stuffs on your ListView even if it's empty, just add a Button or a FloatingActionButton to your Activity and then use those: you can both keep the button in any case (like an "Add item" Button) or you can make it visible only if the ListView is empty. something like:
xml
<Button
android:id="#+id/buttonEmptyListStuffs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List is empty, click me!"
android:visibility="gone"/>
activity
//init the button and do other stuffs
...
buttonEmptyListStuffs.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//go to activity 2
}
});
...
List<MyListViewItem> myListViewItems = //init your list of items for the listView
buttonEmptyListStuffs.setVisibility(myListViewItems.size() > 0 ? View.GONE : View.VISIBLE);
...
Note: I wrote this code by hand without compiler so it might not be perfect, just take the concept behind it
As Pskink said from comments
I forgot to mention that ListView has setEmptyView(View) which allows you to set a custom layout for the Listview if it is empty. Refer to his link for a good tutorial
USe this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
startActivity(new Intent(getApplicationContext(), BillingActivity1.class));
}
});
If you really want to handle click on any Point of your listView. You can put the list inside a FrameLayout or LinearLayout and then add your onClickListener on the layout.
findViewById(R.id.my_parent_list_layout).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO your stuff
}
});
If your list item count is empty the dynamaically change the visibility of the list view with a button(switching the visibility with list view and button) and give the click action to the button.
This is the situation: I have a Listview with some items. Inside each item there are two clickable views that are only shown after user clickes in their parent item.
It works like this:
productList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, final View view, final int position, long id) {
TextView txtAdd = (TextView) view.findViewById(R.id.txt_add_units);
txtAdd.setVisibility(View.VISIBLE);
txtAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
TextView txtRemove = (TextView) view.findViewById(R.id.txt_remove_units);
txtRemove.setVisibility(View.VISIBLE);
txtRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
basketProductsListAdapter.notifyDataSetChanged();
}
});
The first time a listview item is clicked, its txtAdd and txtRemove child views are shown and then if user clicks them, code inside their onclickListener is executed.
The problem appears ONLY IN THE FIRST listview item. First time it is clicked txtAdd and txtRemove are shown, BUT THE FIRST TIME user clicks over the child views, their parent's ItemClickListener is fired again, and then the child views (txtAdd and txtRemove) are not receiving the clickListener. Following times they are work perfectly.The weird thing is that it only happens first time on the first item in the listView.
Any ideas? thanks in advance!
Add this line to your listview xml android:descendantFocusability="blocksDescendants"
The Thing is our child view may be focusable so the first click will be for the focus and after that the itemClick will work. to avoide focusable add that line in the xml
Still if it does not work then try adding
android:focusable="false"
android:focusableInTouchMode="false"
to the child View of the listview
Further details:
If I do not set a OnClickListener on my View, Android seems to automatically handle the OnClick state of my View and changes the background of the view to show that it was clicked. Yet if I add an OnClickListener of my own, there is no visual change shown, is there something I need to call to show the state change?
This View is also found in a ListView (not sure if this helps though).
Here's how I set the OnClickListener on my View:
items.add(new EntryItem("Profile", R.drawable.ic_action_user, null,
new OnClickListener() {
#Override
public void onClick(View v) {
self.closeDrawer();
Intent userView = new Intent(self,
UserViewActivity.class);
userView.putExtra("username", "kortank");
self.startActivity(userView);
}
}));
This View is also found in a ListView (not sure if this helps though).
If you set a click listener for a list item, the listView's onClick listener for the list item(the one that changes the background and etc) is overriden. Try using the ListView.setOnItemClickListener instead.
I am using a custom list view by extending the ArrayAdapter class. Each row has some text as well as an image. What I want to do is delete the row when I click on the imagevView. So i set an onclick listener as well as a tag for each image view and then used the onclick listener to change the adapter. However it simply refuses to work. What exactly am I doing wrong?
holder.image.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view)
{
Toast.makeText(context, "ImageView clicked for the row = "+view.getTag().toString(), Toast.LENGTH_SHORT).show();
createEvent.list.remove(view.getTag());
createEvent.adapter.notifyDataSetChanged();
}
The toast prints correctly. However the item is not deleted. Any ideas on how I can achieve this?