In have implemented the OnItemLongClickListener. When LongClicked the list item, I enable a button of the relevant listItem. After LongClicked the button enables successfully but onListItemClick doesn't get fired. If I onListItemClick without LongClicked, it fires successfully. Why does onListItemClick doesn't fire if LongClicked fired before that?
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, "Long Clicked : " + a.getAccountName(), Toast.LENGTH_LONG).show();
v.findViewById(R.id.btn_delete).setVisibility(View.VISIBLE);
return false;
}
};
getListView().setOnItemLongClickListener(listener);
public void onListItemClick(ListView l, View v, int position, long id) {
// Do something when a list item is clicked
Account a = null;
a = (Account) l.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, a.getAccountName(), Toast.LENGTH_SHORT).show();
}
Try to implement like below,
setOnItemLongClickListener
setOnItemClickListener
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, "Long Clicked : " + a.getAccountName(), Toast.LENGTH_LONG).show();
v.findViewById(R.id.btn_delete).setVisibility(View.VISIBLE);
return false;
}
});
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Account a = null;
a = (Account) l.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, a.getAccountName(), Toast.LENGTH_SHORT).show();
};
});
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, "Long Clicked : " + a.getAccountName(), Toast.LENGTH_LONG).show();
v.findViewById(R.id.btn_delete).setVisibility(View.VISIBLE);
return false;
}
};
getListView().setOnItemLongClickListener(listener);
I just found the cause for the problem. It's the line of code I used to enable the delete button when OnItemLongClick is clicked.
v.findViewById(R.id.btn_delete).setVisibility(View.VISIBLE);
When I remove that line the code works perfectly. But still I ain't got a solutions to the problem. I need to have that line of code to enable the delete button when the OnItemLongClick is clicked.
I just replaced the button with an ImageView. Then there was no issue. The two events are working smoothly. So I realized there is something wrong with the button. I think we have to set some of the properties in the button to function properly in this situation. You can simulate the issue.
1. User a Listview.
2. Customize the list view by adding a TextView and an ImageButton or a Button as a ListItem. Make the visibility of the button to invisible by default.
3. Implement the onItemLongClick event as above and make the button when ItemLongClicked.
4. Once you ItemLongClicked a ListItem and make the button in the ListItem Visible, onListItemClick event of the particular ListItem does not work.
Related
I have list view with two actions one is for new activity and other one is when I long press the list item delete but the item will not delete mean while when I long press the new activity will be opened automatically..how to solve this issue
You have to set setOnItemLongClickListener() and setOnItemClickListener() in the ListView:
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + pos);
return true;
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("Hello!", "Y u no see me?");
}
});
Make sure your onLongClick retrun true ,true means that the event is consumed. It is handled. No other click events will be notified.
#Override
public boolean onLongClick(View view) {
return true; // or false
}
or add in xml
<ListView android:longClickable="true">
or in java class
listView.setLongClickable(true)
You can also manage multiple touch my implementing your custom touch listener. Please refers this link for detail.
Elapsed time between first and second ACTION_DOWN
I have an app which use ListView,I have set onListItemClick event to view details about contact,and I want to implement onLongListItemClick to show a dialog and I don't know why it doesn't work,I mean nothing happens,I tried like this:
public void onListItemClick(ListView parent, View v, int position, long id)
{
//code
}
public void onLongListItemClick(ListView parent, View v, int position, long id)
{
showdialog();
}
Try like this :
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Long Clicked : ", Toast.LENGTH_LONG).show();
return true;
}
});
You need to add return true; here otherwise it will transfer control to single click event listener once you release your touch.
Hope it helps you.
Thanks.
I have an Android application. In one of my Activities which is derived from ListActivity, I've implemented the OnItemLongClickListener. I want to enable a delete button within the relevant list item where the ListItem has been LongClicked. How can I do this?
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
Toast.makeText(AccountActivity.this, "Long Clicked : " + a.getAccountName(), Toast.LENGTH_LONG).show();
//instead of the toast, I need to show/enable a button here...
}
};
getListView().setOnItemLongClickListener(listener);
.xml
<Button
android:id="#+id/imgdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
.java
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
view.findViewById(R.id.imgdelete).setVisibility(View.INVISIBLE);
return false;
}
});
in your get view method of adapter set unique id to your button
btn.setId(position);
then on your click listener
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Button btn = (Button) v.findViewById(position);
btn.setEnabled(true);
}
};`
Suppose you had a Button inside ListView's row layout then you can make it visible true`
OnItemLongClickListener listener = new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
Account a = null;
a = (Account) av.getItemAtPosition(position);
v.findViewById(R.id.btnid).setVisiBility(View.VISIBLE);
}
};`
You can add boolean flag isDeleteVisible to Account with default false value.
Then in OnItemLongClickListener set it to true and call adapter.notifyDataSetChanged()
In adapter's getView check isDeleteVisible and show or hide delete button.
How to disable list view on item click in particular row in android.if value is search i have to disable the onitem click event of particular row in list view in android can anybody tell how to do?
try this code
mListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long arg3)
{
if(your condition)
{
//your click event code
}
else
{
//nothing
}
}
});
you for comple row
onListItemClick(AdapterView<?> l, View v, int position, long id) {
if(Clickable condition ){
super.onListItemClick(l, v, position, id);
// your code .........
}else{
}
}
I'd like to have both type of clicks on a listView - onClick and LongClick.
I've implemented it like this:
this.listViewSub = (ListView) this.findViewById(R.id.listsub);
this.listViewSub.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(final AdapterView parent, final View view, final int position,
final long id) { ... } });
// listen to long click - to share texts
this.listViewSub.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) { ... } });
But it does't fire the Long Click.
Anyone has any idea why?
You have to enable the LongClickable
list.setLongClickable(true);
and
list.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
}
});
#Vadim, are your listview's adapter is extends from BaseAdapter? if yes, then also need to set convertView.setLongClickable(true); in the getView().
For me, I had to set android:longClickable="true" in the XML file that contains my ListView row layout (not ListView layout) for the item to be long-clickable.
onLongClick returns true if the callback consumed the long click, false otherwise. So if the event is handled by this method, return true.