how can i use both OnItemClickListener and OnItemLongClickListener or just disable longclick on a list view?
i've override OnItemLongClickListener and when i return true on onItemLongClick the longclick will disable but OnItemClickListener wont respond anymore.
DailyReportList.setOnItemLongClickListener(new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView parentView, View childView, int position, long id) {
return true;}});
registerForContextMenu(DailyReportList);
DailyReportList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
arg1.showContextMenu();
FillTxtWithListItems(arg1);
}});
If you want to disable onClick or onLongClick you just need to uncheck/check the feature in your layout xml properties. Find the image on how it can be disabled..
If you do not want long click listener nor context menu just do not set any and do not call registerForContextMenu(). Lists do not have these by themselves.
Related
I need to capture the event when the spinner don't change item. This is my code:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
}
This code works when I change item in spinner. But when I always click on the select Item how I can capture this event? Anyone can help me?
Its very simple.
Impl. the OnItemClickListener for the Spinner.
Docs: http://developer.android.com/reference/android/widget/Spinner.html#setOnItemClickListener(android.widget.AdapterView.OnItemClickListener)
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
...
}
});
I want to get Last textview from row that was being clicked by user
for ex,
I have clicked on first row then i will get "Single Cheese Topping"
So How Do i implement the onclicklistener any suggestion please.
For listview, you should use OnItemClickListener instead of OnClickListener. Here's you go.
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View viewGroup, int position,
long arg3) {
TextView lastTextView = (TextView)viewGroup.findViewById(R.id.last_textview_id);
String lastText = lastTextView.getText().toString();
}
});
Second parameter return your row layout ViewGroup. You just use findViewById to get the view you need to use from the row layout.
lv1.setOnItemClickListener(new OnItemClickListener (){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3)
here arg0 has the name of the item at index presses. you can get the name like this
name=arg0.getItemAtPosition(position).toString();
and you are done
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.
I know how can implement listview without using listactivity.But i dont know how get the clicked item from the listview.Please anyone tell me how listen the clicked item from the listview without using listactivity.
Please Follow this Link. Here you can find simple Listview example using Activity.
Using listview.setOnItemClickListener you can get Click on Listview.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
You can implements OnItemClickListener and can write the method like below or just simply use eclipse to implement unimplemented method
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
}
First you have to make instance of list view Clickable like
lv.setClickable(true);
then
use following
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
use ListView.
Then with the listview object invoke
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//do your stuff
}
}
for click on listview in activiyt try this code...
ListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//write you action here
}
});
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.