Obtaining a certain view in the ListView - android

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

Related

In Listview onItemClick() listener is not working?

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.

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.

OnItemClickListener() - Android listview

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.

get which spinner is clicked

I took multiple spinners in single view. I know how to get when item is selected.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id)
{
parent.getItemAtPosition(pos);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
But how can i know that which spinner user has clicked on in a single listener?
Any suggestion will be appreciated.
Thanks In advance.
Try the following way.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
switch(parent.getId()){
case R.id.spinner1:
//your code goes here
break;
case R.id.spinner2:
//your code goes here
break;
}
}
Refer here and here too.
parent in onItemClick refers to the Spinner that was clicked.
AdapterView.OnItemSelected
parent The AdapterView where the selection happened
Spinner is an AdapterView. Spinner

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.

Categories

Resources