I'm trying to hide a listview, once I click on an item, and later show a button in it place, but inside of the setOnItemClickListener, the listview is not accesible. What can I do?
// Binding resources Array to ListAdapter
lv.setAdapter(new ArrayAdapter<String>(
MainActivity.this, R.layout.list_venues,
listItems));
// Click event for single list row
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter,
View view, int position, long arg) {
// TODO Auto-generated method stub
Button here = (Button) findViewById(R.id.iamhere);
lv.setVisibility(View.GONE);
here.setVisibility(View.VISIBLE);
Toast.makeText(
getApplicationContext(),
"Osea que andamos por "
+ listItems.get(position),
Toast.LENGTH_LONG).show();
}
});
Thank you very much.
That's because lv is out of scope for onItemClick(AdapterView<?> adapter, View view, int position, long arg).
However, AdapterView<?> parameter is actually a reference to containing listview, so you can simply change
lv.setVisibility(View.GONE);
to
adapter.setVisibility(View.GONE);
Related
I have a ListView and I am trying to handle the click event on list of items via the following code
listView.setOnClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
// Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
}
But I am getting compile time error which is as following
The method setOnClickListener(View.OnClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new AdapterView.OnItemClickListener(){})
How can this error be resolved ?
Use setOnItemClickListener() to set a listener for item clicks. setOnClickListener() would register a regular View.OnClickListener on the ListView widget itself.
Replace setOnClickListener by setOnItemClickListener :-)
use setOnItemClickListener()
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String item = (String)adapter.getItemAtPosition(position);
}
});
Use setOnItemClickListener instead of setOnClickListener
adding onClickListener to views in the getView() method is using when
you have 2 or more views that should have their own onClickListener,so
for your is better onItemClickListener
i.e. rewrite your code as
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
// Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
}
For more info see
Which: ListView.setOnItemClickListener or TextView.setOnClickListener?
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.
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
}
});
Hello all
I created a custom listview through my custom adapter..
CustomAdapter adapter = new CustomAdapter(MainPage.this, dbHelper, imgId, title);
adapter.setList(list1);
list1.setAdapter(adapter);
list1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.i("hello",arg2+"item clicked ....");
Toast.makeText(MainPage.this, "Kindly enter search parameter!!", Toast.LENGTH_SHORT).show();
}
});
--
Now i am not able to select any item. I dont know why.....
try setting the inflated view "Clickable" and "able to listen to click events" in your adapter class getView() method.
convertView = mInflater.inflate(R.layout.your_layout, null);
convertView.setClickable(true);
I have a list view in which I show data. When I click on a row of the list view, I want to display data for that row from web services. How do I do that?
What you do is set the ListView's OnItemClickListener with the setOnItemClickListener method. You then obtain the "item object" for the clicked item with the getItemAtPosition method:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentView, View itemView, int position, long id) {
ListView listView = (ListView) parentView;
Object o = listView.getItemAtPosition(position);
//...
}
});
The type of the "item object" (o in the example above) depends on the ListAdapter that the list view is using. One common choice is CursorAdapter, in which case the type of the item object is conveniently the Cursor to the record corresponding to the list item that was clicked.
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});