I am trying to learn how to receive a notification on a click on a radio button in a ListView but nothing comes back when I click on a radio button next to a ListView item. Below is the code where I set up the listener. I am doing this in an asynchronous task under the onPostExecute() method where I populate my ListView from the server and my main activity extends MapActivity. Does anyone know what I am doing wrong?
protected void onPostExecute(ArrayList<String> result) {
// ... some code
mapView.postInvalidate();
final ArrayAdapter<String> arrayAdpt = new ArrayAdapter<String>(
getApplicationContext(), android.R.layout.simple_list_item_single_choice,
viewline);
ListView restaurant_list = (ListView) findViewById(R.id.list);
restaurant_list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
restaurant_list.setAdapter(arrayAdpt);
restaurant_list.setScrollContainer(true);
restaurant_list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.e("listargs", (String.valueOf(arg1)) + " " + String.valueOf(arg3));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Does anyone know what I am doing wrong?
I recommend to you use OnItemClickListener instead of OnItemSelectedListener
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View parent,
int position, long id) {
// do your stuff
}
});
OnItemClickListener is usually used when you want to catch click events. OnItemSelectedListener is on the other side usually used with Spinner.
It sounds like you might have focus problem. Click listener only works if no other view can take focus.
Alter your checkbox xml with the following:
focusable="false"
Or set it to false if you are generating your layouts in code.
OnItemSelectedListener is for trackpad/trackball events
OnItemClickListener is for click events
Personaly I always implement both for safety.
Related
I have a requirement wherein I want to disable items in the list view.
Say e.g., I have 5 items in the listview out of which I want to only enable 1 item.
Note: disabling means greying out the item.
Following is my code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,android.R.layout.simple_list_item_1, movies);
I don't want to go with the custom adapter wherein we get the getView().
Is there any other way to implement this functionality?
In the adapter there is a method name isEnabled which you can override. This is called for each row like getview. The onclicklistener will only fire if this function returns true. So try doing that in your custom adapter.
#Override
public boolean isEnabled(int position) {
if(YOUR CONDTITION){
return false;
}
return true;
}
Without adapter:
Then you need to disable the item by getting view at specific position.
Please implement the listener for this method setOnItemSelectedListener . So you can disable any item that you want.
You can also disable item using:
final Set<Integer> disabledPositions = new HashSet<Integer>();
disabledPositions.add(positionYouWantToDisable);
disabledPositions.add(positionYouWantToDisable);
disabledPositions.add(positionYouWantToDisable);
ListView listView = new ListView(this);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
if(!disabledPositions.contains(position) {
// do what you want
}
}
});
try this:
final List<Integer> disabledItems = new ArrayList<Integer>();
disabledItems.add(0);
disabledItems.add(2);
lvMovies.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (disabledItems.contains(arg2)) {
Toast.makeText(getApplicationContext(), "DISABLED", Toast.LENGTH_SHORT).show();
arg1.setEnabled(false);
} else {
Toast.makeText(getApplicationContext(), "NOT DISABLED", Toast.LENGTH_SHORT).show();
arg1.setEnabled(true);
}
}
});
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'm just learning how to use ListViews. I got it working, but wont to be able to respond when some one clicks a item.
I'm trying to use the setOnItemClickListener method to take a call back for when a item is clicked on. But my code will not compile due to errors in method setOnItemClickListener
r
Right now i get a error that says
setOnItemClickListener is not applicable for arguments OnItemClickListener();
void SetUpList()
{
listView = (ListView) findViewById(R.id.mylist);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile"};
EventsAdapter adapter = new EventsAdapter(this, cGlobals.eventsTitle);
// Assign adapter to ListView
listView.setAdapter(adapter);
// this is whare I get the error listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
}
});
}
}
First make sure you have imported this class:
import android.widget.AdapterView.OnItemSelectedListener;
Next you need to call setOnItemClickListener() like so:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override // "#Override" is required for Java 1.6, but forbidden in 1.5
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do Something
}
});
Or if your activity implements OnItemClickListener: You need to add the onItemClick() method outside your onCreate() method:
#Override
public void onCreate(Bundle savedInstanceState) {
// Do Something
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Do something else
}
(Of course, if you are extending a ListActivity or ListFragment you should override onListItemClick() instead of onItemClick() like the second approach.)
I want to get the text element that makes up my ListView items.
myList = (ListView) findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, partialNames);
myList.setAdapter(adapter);
I am not extending ListActivity so I can't call a simple onListItemClick function.
I have tried using the following:
myList.setOnItemClickListener(adapter.?)
, but I don't know what parameters I need for the constructor
How can I capture the string value in a particular ListView item when the user clicks on the row?
Make your Activity implement OnItemClickListener, and then:
myList.setOnItemClickListener(this);
or if you dont want to implements OnItemClickListener, you can do:
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
// TODO Auto-generated method stub
}
});
I create a CursorAdapter to provide data for my ListView.
I implement the bindView() method to show data in a row of my Listview.
But at the end of my bindView, I add an clickListener to it. But when I run it on emulator, I don't see any print statement.
Can you please tell me how to add event handling in a row in ListView?
view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println (" getting an onclick event....");
}
});
You could attach OnItemClickListener to the ListView - not the rows individually.
listView.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
Log.d("TAG", "Clicked");
}
});