Is there onListItemTouch method in listview - android

I know onListItemClick method to handle click listener as below.
protected void onListItemClick(ListView l, View v, int position, long id){
}
But if I want to handle touch listener.
While item be touched, I want to get the position.
But not do in onListItemClick.
How can I do in ListView?
Is there any method such above?

you need to invoke onInterceptTouchEvent() method
Please check out my answer here.

Use AbsListView.onTouchEvent()
You have to subclass the ListView to use it though.
http://developer.android.com/reference/android/widget/AbsListView.html#onTouchEvent(android.view.MotionEvent)

Related

Disable onItemClickListener for one list?

I've two list one with custom adapter and other with array adapter. So i want onItemClickListener to work with custom adapter listview and it will automatically disable when I start using same listview for array adapter. To initiate array adapter list I'm using button.
I've already try using ListView.setClickable(false)
. but this doesn't work.
If setClickable(boolean) not working then u can use flag to work with as per your behavior.It'll work as u want.
Inside the onitem click event put in an if condition, that is, if its the button to enable the click then perform operations, otherwise do not.
In the onClick of button1 set a integer flag to 1 & of button2 set flag to 2
Then define onItemClickListener like this
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
if(flag ==1){
Toast.makeText(getApplicationContext(),"You selected "+ arr.get(position)+"", 1).show();
Intent ints= new Intent(getApplicationContext(),Activity2.class);
ints.putExtra("pos", position);
startActivity(ints);
}
}
NOTE: if the flag is not 1 then dont implement the required code thus by providing a condition you manipulate the onClick of listview.

Why setOnClickListener() did not work for GridView (AdapterView)

When I try to set click listener for GridView (AdapterView), the program breakdown and Logcat : "Don't call setOnClickListener for an AdapterView".
Add: Why the mechanism(Adapterview could not call on click) work like this ?
Try to use onItemClick
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Added in API level 1
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
You should try to setOnItemClickListener function.

Change image of listview item on click

I am trying to change image of a list item on click.I have tried using view but on using it I am getting multiple items with changed image even though I just click on a single one.I am using a simplecursor adapter and code is same as given in this question:-changing image on listview at runtime in android.
And also I don't want to use a custom adapter.It will be really helpful if someone helps me in solving this problem just through simple adapter.
Current code
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
/*
imageView=(ImageView)l.getChildAt(position).findViewById(R.id.PlayPause);
imageView.setImageResource(R.drawable.pause);
*/
}
1- Do not use get child at when u are directly getting the child view as "View v" in the event.
2- you just need to call invalidate() method on the view once u set the new background resource.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
imageView=((ImageView)v)
imageView.setImageResource(R.drawable.pause);
imageView.invalidate();
}
While you should make your list using a simple cursor adapter with an imageview ---- :-
http://thinkandroid.wordpress.com/2010/01/09/simplecursoradapters-and-listviews/
refer this link for that.
Why not use
ListView.OnItemClick(OnItemClickListener{})
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

How to get access to the fields of custom adapter in ListView in Android?

In my project I use a custom adapter to define the items of a ListView. So I want to define special behavior of item in ListView, which will be depend on the value of the field in adapter. Like this :
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
So, here, in this method I want to get access to the element of adapter to get the value from it, number of which is defined by the int position. So, what is code to do this?
you can simply cast it if you're confident enough it is always your adapter that is used:
protected void onListItemClick(ListView l, View v, int position, long id) {
((MyAdapter) l.getAdapter()).myMethod(position);
}
If you are defining an custom adapter you can save a reference to the adapter in the activity that registers the onClick behaviour. You now can call getItem on the Adapter, if you have implemented the getItem method properly.
You can get selected item(object) from list. Lets say your custom adapter containing Restaurent object per row, and your listView name foodJntListView, then
Restaurent rest= (Restaurent) foodJntListView.getSelectedItem();
will give you the selected item(object).

Identify the item clicked on ListActivity method onListItemClick

I'm developing an Android application.
I have several objects loaded on a ListActivity. I want to know the item clicked on event onListItemClick.
I see method onListItemClick has a parameter called id. How can I setup this id?
My objects are identified by an ID, how can I add this ID to listItem?
Thanks.
if SectionObj is your object that you want to access later, set that in the adapter when you set the source.
ArrayList<SectionObj> li
ArrayAdapter<SectionObj> adapter=new ArrayAdapter<SectionObj>(getApplicationContext(),android.R.layout.simple_list_item_1, li);
setListAdapter(adapter);
then in ur listener method..
protected void onListItemClick(ListView l, View v, int position, long id) {
SectionObj o=(SectionObj)getListView().getItemAtPosition(position);
}
What is the source of your list data? If you are using a cursor - then the id passed in onListItemClick(ListView l, View v, int position, long id) will automatically be the id of the cursor row.
Use the following;
listView.getItemAtPosition(position);
Where listView is the name of your list view.
You can set the id in your ArrayAdapter view.
Checkout the following pages then you should find the solution.
http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView,%20android.view.View,%20int,%20long)
http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications

Categories

Resources