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.
Related
Here i have using following code. Now my grid showing the download button on long press.If i long press another item it shows the button.But the problem is that previous button is there.I want to hide the old .(Hide the old download button on new Long press on another grid item).
How can i do ? Please check it and give me a idea to implement this part ?
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
}}
Thanx in advance.
You can remember previous long-clicked item:
private View previousItem;
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
if (previousItem != null) {
downloadImg=(ImageView) previousItem.findViewById(R.id.download);
shareImg=(ImageView) previousItem.findViewById(R.id.share);
downloadImg.setVisibility(View.GONE);
shareImg.setVisibility(View.GONE);
}
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
previousItem = view;
}
}
This may help for same activity.
grid1.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg1=(ImageView) view.findViewById(R.id.download1);
shareImg1=(ImageView) view.findViewById(R.id.share1);
downloadImg1.setVisibility(View.VISIBLE);
shareImg1.setVisibility(View.VISIBLE);
downloadImg2=(ImageView) view.findViewById(R.id.download2);
shareImg2=(ImageView) view.findViewById(R.id.share2);
downloadImg2.setVisibility(View.GONE);
shareImg2.setVisibility(View.GONE);
}}
grid2.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
downloadImg1=(ImageView) view.findViewById(R.id.download1);
shareImg1=(ImageView) view.findViewById(R.id.share1);
downloadImg1.setVisibility(View.GONE);
shareImg1.setVisibility(View.GONE);
downloadImg2=(ImageView) view.findViewById(R.id.download2);
shareImg2=(ImageView) view.findViewById(R.id.share2);
downloadImg2.setVisibility(View.VISIBLE);
shareImg2.setVisibility(View.VISIBLE);
}}
If you using custom adapter. You can create in your CustomAdapter method setButtonVisible(int position). And in the getView check this setted position and show or hide Button in view. Your method onClick kwill be something like this:
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
adapter.setButtonVisible(position);
adapter.notifyDataSetChanged();
}
You can:
Make a field or final variable with last button. in onItemLongClick check if it's not null and then make it invisible. On the end of onItemLongClick fill in the field variable with current button.
In your case (with provided code I suggest that downloading and shareImg are final variables common for all views that could be clicked):
bookGrid.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
if (downloading != null) {
downloadImg.setVisibility(View.INVISIBLE);
}
if (shareImg != null) {
shareImg.setVisibility(View.INVISIBLE);
}
downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
}}
Make a button disappear after some time. But in this case the variable should be the method scope. E.g. :
final ImageView downloadImg=(ImageView) view.findViewById(R.id.download);
shareImg=(ImageView) view.findViewById(R.id.share);
downloadImg.setVisibility(View.VISIBLE);
shareImg.setVisibility(View.VISIBLE);
downloadImg.postDelayed(new Runnable(){
#Override
public void run() {
downloadImg.setVisibility(View.INVISIBLE);
}
}, 1000);
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.
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.
For example when i press longclick on one item on listview , its actual button will turn to delete button. I try like next_sign.setBackgroundResource(R.drawable.delete); but, it force close.
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
//next_sign.setBackgroundResource(R.drawable.delete);
return true;
}
});
12-28 13:44:34.251: E/AndroidRuntime(9108): FATAL EXCEPTION: main
12-28 13:44:34.251: E/AndroidRuntime(9108): java.lang.NullPointerException
12-28 13:44:34.251: E/AndroidRuntime(9108): at com.mycos.cycleborg.PreviousActivity$3.onItemLongClick(MenuListActivity.java:129)
you have to declare two buttons of your first item and delete button at same position in your layout
make visibility of your original button to
android:visibility="visible"
and make visibility of delete button to gone in layout using
android:visibility="gone"
in programming
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Original_Button.setVisibility(View.Gone);
delete_Button.setVisibility(View.Visible);
return true;
}
});
public boolean onItemLongClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
Button mButton = (Button)view.findViewById(R.id.button);
mButton.setVisibility(View.Visible);
return true;
}
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.