OnItemClickListener() - Android listview - android

I was developing a sample app in Android and came across below code, I am just wondering what part of Java,OOPS concept is used when this line is used , can any one please explain in detail this kind of declaration ? why its being declaraed like this
mainlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
} });

This is called an Anonymous class, in this example used as part of an Observer Pattern.

OnItemClickListener is an interface.
The ListView will save the interface implementation you gave (your new OnItemClick...).
And when the ListView detects a user click on item it will call the callback you gave (your implementation) via mOnItemClickCallback.onItemClick(params...)

In addition to what "metter" has stated,
this
mainlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
} });
can also be written as
mainlistview.setOnItemClickListener(this);
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/// do the stuff for item click
}

Have a look at observer and command pattern. These pattern help you to understand the concept.

Related

OnClick Listner of images in Grid View Android

I have a Grid View that have 4 images .So i want to identify which image has been clicked so that corresponding to that i can start a new activity .
So please help me how can i get this
I have tried this
dataView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
getView can be used for this purpose.for this in getView find View and apply onClicklistner their.to make all views clickable you need to setFocusable(false) on all focusable views.
I have got the solution
dataView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
links[arg2], Toast.LENGTH_SHORT).show();
}
});
int arg2 in the OnItemClick method specifies the position.Using that you can get the item clicked.

Android: can not set onItemListener

I am creating a ListView with an adapter in an intent with a dialogtheme:
adapter = new SimpleAdapter(this, test_list, R.layout.list_layout2, from, to);
lv=(ListView)findViewById(R.id.listView1);
Then I am trying to add some listeners, but they aren't triggered in the running app.
onclick =new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.d("item",arg2 + "");
}
};
onlongclick = new OnItemLongClickListener(){
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.d("longitem",arg2 + "");
return false;
}
};
lv.setOnItemClickListener(onclick);
lv.setOnItemLongClickListener(onlongclick);
Does anybody have an suggestion to get things running correctly?
It seems to be correct, but what i dont understand is why you make variables of your listeners?
You can just set them.
Check out the link here:
http://www.ezzylearning.com/tutorial.aspx?tid=1351248
It uses ArrayAdapter instead of SimpleAdapter and one of the parameters is the items.
It might help you out.
Okay, I found the answer.
In my item layout "R.layout.list_layout2" was a TextView with
android:focusable="true"
android:focusableInTouchMode="true"
Deleting this got my OnItemClickListener work stable.
I've found tutorial that might help you with your problem
Check out here
http://www.androidbegin.com/tutorial/android-simple-listview-tutorial/

Click to list item in CalendarView

I'm trying to click current day in my CalendarView like code below
((ListView) calendar.findViewById(android.R.id.list)).setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
System.err.println("click");
}
});
, but this way doesn't help me and I don't know why. How can I bind click listener to CalendarView?
CalendarView has a nested listener interface CalendarView.OnDateChangeListener with the method onSelectedDayChange(...). You can implement it with custom logic and set using Calendar.setOnDateChangeListener(...) method.

create Listview without using listactivity.only use activty

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
}
});

Calling methods on objects in a ListView

I have a listview that is populated by a by objects from an "Offer" class I've written. Obviously, the toString() method is called and that's the text that's displayed in each list item. My question is, how do I implement an onItemClickListener that will call one of my getter or setter methods on the particular object whose toString method had been used to populate that item?
For example, I'd like to raise a toast or something when an item is clicked that retrieves a string my getClaimCode() method and displays in the toast (or whatever other dialog or even a new activity).
can I just call the methods by doing something like item.getClaimCode()...?
You don't have to implement AdapterView.OnItemClickListener, but I do, and in onCreate() I call myListView.setOnItemClickListener(this).
Then for the AdapterView.OnItemClickListener interface, I have the method:
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
//Code
}
For you, you might do something like:
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
//Arg2 is the position in the list so...
OfferObject offer = (OfferObject) arg0.getItemAtPosition(arg2);
//Go to town...
}
Of course your Activity doesn't have to implement the AdapterView.OnItemClickListener and you could create the listener on the fly.
yourList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
OfferObject offer = (OfferObject) arg0.getItemAtPosition(arg2);
//Same idea here...
}
});

Categories

Resources