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.
Related
I'm using a spinner in order to select a certain value, and in the activity that contains the spinnr, I've implemented OnItemSelectedListener.
in the OnCreate() method, I've defined:
spinner1.setOnItemSelectedListener(this);
but it doesn't get to the method onItemSelected(), my guess is that I'm not sending the right variable ("this").
what should I send to setOnItemSelectedListener in order for this to get to the method?
Thank You!
activity that contains the spinnr, I've implemented
OnItemSelectedListener
If OnItemSelectedListener listener is implemented in Activity then override onItemSelected in Activity :
#Override
public void onItemSelected(AdapterView<?> parent,
View selectedItemView, int position, long id) {
// your code here
}
Try this.
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Do something
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Also you can look at this. http://developer.android.com/guide/topics/ui/controls/spinner.html
I have added an onItemClick() listener to a listview. The listener is not working.
Here is the code I am working with:`
MyBaseAdapter ma= new MyBaseAdapter(context, myList);
lvDetail.setAdapter(ma);
lvDetail.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
ListData ld= myList.get(arg2);
String des=ld.getDescription();
Toast.makeText(context,"description"+des, Toast.LENGTH_LONG).show();
}
});
Since you are using custom adapter, please write listener inside getView of adapter on item of list and perform operation.
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.
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.
I have a ListView with 3 items and an OnItemClickListener.
How can I get a View of the second item?
I need to change the text in the second item when I click on the first item.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
switch (arg2) {
case 0:
((TextView)/*here i need a view of second item*/).setText();
});
One way would be ,
You can use view.getParent(), in your case arg1.getParent() and then use getChild() which will return an array of child views and use it accordingly.
you can use arg0.getChildAt(position) , this is better than previous solution, if it works for you.
If you can make the listView final (shouldn't be a problem), you can reference it from inside the onItemClick method and use its getChildAt method to get the TextView you need.
final ListView listView = ...
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
switch (arg2) {
case 0:
((TextView) lv.getChildAt(1)).setText();
});