How do I enable clicking in ListView - android

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

Related

Hide a listview and show a button on a onItemClick

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

Can't get ListView item selected

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.

Android: trying to get response when person clicks on a item in list box

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.)

How to set onItemClickListener using SimpleCursorAdapter using android.R.id.list

Hi I am trying to implement OnItemClickListener using android.R.id.list which is inbuild list in android using ListActivity. Is there any other way so that i can use onItemclickLietener without placing ListView object in front of OnItemClickListener. Please suggest.
Also I wan to set delete and update method using longclick listener using Database "update and delete " methods. So please help me out.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.file_row, c, from, to);
setListAdapter(adapter);
getnotelist.close();
WhatToPutHere?.setOnItemClickListener(new OnItemClickListener()
{
#Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
How to use this using android.R.id.list or How to get ListView in ListActivity
if you are extending ListActivity then add setOnItemClickListener as:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view,int position, long id) {
// do your work here
}
});
You can get ListView as if extending ListActivity:
ListView listview = getListView();
or by using android.R.id.list as
ListView listview = (ListView)findViewById(android.R.id.list);

Single Choice Lists and onClick events

I have a CHOICE_MODE_SINGLE list with 3 rows. How do I place an onClick event that changes the value of an arbitrary variable, so that if Row 1 is clicked, say, the value of X = 1, and when Row 2 is clicked, the value of X = 2, etc.?
public class List extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, GENRES));
final ListView listView = getListView();
listView.setItemsCanFocus(false);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
private static final String[] GENRES = new String[] {"Barre", "Buffumville","Hodges"};
}
Its quite simple..rather than extending ListActivity extend it by Activity and just declare listview in your XML file as below:
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
initialize listview in your activity:
ListView listView=(ListView) findViewById(R.id.list);
And after setting adapter as you did,set the item click listener of the listview as below in your code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Toast.makeText(getBaseContext(), GENRES[position], Toast.LENGTH_SHORT).show();
}
});
By doing this you will get the desired value of that position of listview.
You will surely get the result you want.
You want to set the onItemClickListener on the list view.
listView.setOnItemClickListener(
new OnItemClickListener(
#Override
onItemClick(AdapterView<?> listView, View cell, int position, long id) {
// Code to change variables
}
});
You can implement protected void onListItemClick (ListView l, View v, int position, long id) in your ListActivity, and the position variable lets you know which item was clicked.

Categories

Resources