Check selected item of ListView - android

I have a ListView and need to check, which item has been clicked.
The way I have it, it only shows my the selected item when I use the scrollwheel of my mouse (when testing it in the Eclipse emulator). When I test it on a real mobile device, it doesn't react at all.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
myFunction(listView.getSelectedItem());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Use setOnItemClickListener instead of setOnItemSelectedListener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//get value for listItems using position
String str =listItems[position];
myFunction(str);
}
});

I think better you need to use on onListItemClick listener
public void onListItemClick(ListView parent, View v, int position, long id)
{
parent.setItemChecked(position, parent.isItemChecked(position));
Toast.makeText(this, "You have selected" + items[position],30).show();
}

Related

about setOnItemClickListener(new OnItemClickListener()

My database data is stored in list view, when I click on one of the list view, how can I get each item according to parameter “position”?
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
});
You can do it by-:
messagesContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
break;
case 1:
break;
}
}
});
But this will be complex when we have to many items in listview.
Thanks
Try this...
this sample will help you...
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();
}
});
for more
If you set your database data in listview like below
ArrayAdapter<Object> mDatabaseData = new ArrayAdapter<Object>(this,android.R.layout.simple_expandable_list_item_2, array);
lv.setAdapter(mDatabaseData);
then you can get each item according to parameter “position” as below
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, get data for selected position
Object selectedItem=mDatabaseData.get(position);
}
});

error while trying to set onItemClickListener in ListView up

error screenshot
I am trying to make clickable items in ListView, and recivieng this kind of error.
ListView chHE = (ListView) findViewById(R.id.lvHE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.support_simple_spinner_dropdown_item, cities);
chHE.setAdapter(adapter);
chHE.getOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long l) {
if (position == 0) {
toast.show();
}
}
});
Error:(52, 13) error: method getOnItemClickListener in class AdapterView
cannot be applied to given types;
required: no arguments
found:
reason: actual and formal argument lists differ in length
where T is a type-variable:
T extends Adapter declared in class AdapterView
chHE.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if(position==0){
toast.show();
}
}
});
maybe you have write in a mastake?change get=>set
you should remplace getOnItemClickListener to setOnItemClickListener
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
Try doing this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//doSomeThing
}
});

How do I add Click Listener to ListView?

I have a List View which is populated with countries I want to be able to click a an item and have it go to another activity in which i play an audio.
my list View:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item,getResources()
.getStringArray(R.array.countries)));
}
}
How do I go about adding a onClick just like how you would make one for a button??
ListView listView = getListView(); // hear bind your listview
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item,getResources().getStringArray(R.array.countries)));
listView.setAdapter(ListAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
}
});
}
}
Either use getListView() to access built in list view, and operate on that:
getListView().setOnItemClickListener();
Or, override onListItemClicked() in ListActivity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//--do stuff--
}
override onItemClick() method
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
}
Since your activity extends ListActivity
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent launchActivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(launchActivity);
}
});
U can use context.getlistview() to get the listview. and add setOnItemClickListener to the listview like this.
context.getlistview().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do whatever you want here
}
});
To make it handy just implement OnItemClickListener to your class
Add this in onCreate
ListView listView = getListView();
listView.setOnItemClickListener(this);
then override onItemClick like this
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
// your code
}
You have to use the setOnItemClickListener method.
Here is the code
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// do your code here
}
});

Adding an event listener to listView

getSherlockActivity().findViewById(android.R.id.list);
new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}
};
I am trying to add an event listener to my activity, but the toast message is not shown at all. I am calling this piece of code from onCreateView method
It doesn't seem like you are actually setting the click listener on your list, you are just creating it, and not even keeping a reference to it.
try like this:
ListView lv = (ListView)(getSherlockActivity().findViewById(android.R.id.list));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}
});
getSherlockActivity().findViewById(android.R.id.list).setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}});
EDITED
((ListView)getSherlockActivity()).findViewById(android.R.id.list).setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}});
Add this line inside onCreate().
I do not see you adding anything. All you did was findViewById() for no purpose (as you did not used return value. Also you just created new OnItemClickListener for no reason as you do not use it either. And no, putting two lines of code next to each other won't make then automagically interact in any way...
Right approach is:
ListView v = (ListView)getSherlockActivity().findViewById(android.R.id.list);
v.setOnItemClickListener( new OnItemClickListener() {
...
...
Couple of weeks ago, I found same situation. I didn't use Sherlock but, it may help to set following line.
listView.setItemsCanFocus(false);
Also make sure to set clickable false to any Button in your row item.
Only add
ListView lv = (ListView)getSherlockActivity().findViewById(android.R.id.list);
lv.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
}
and Activity should implement OnItemClickListener
or
ListView lv = (ListView)getSherlockActivity().findViewById(android.R.id.list);
lv.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
}

How to get spinner control selected id

I've a spinner control that I connected it via a dataadapter and I get the data through it .
in the class, I want to get the selected spinner control's id .
How can I do that ?
I've used this code but when I run it , it says there's a problem and it closed .
Toast toast=Toast.makeText(MainActivity.this,sp.getSelectedItemId(),5000);
toast.setGravity(Gravity.CENTER,100, 0);
toast.show();
I tried getSelectedItemId and getSelectedItemPosition but non of them worked .
Here the answer for your question
ArrayAdapter<String> adpt = new ArrayAdapter<String>this,android.R.layout.simple_spinner_item, strType);
adpt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnType.setAdapter(adpt);
spnType.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int intItemType = spnType.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
This the line getting selected position of the spinner
int intItemType = spnType.getSelectedItemPosition();
Try this
//Spinner OnItemClick Event here
payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String spinnerSelectedValue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Please let me know your problem is resolved or not.

Categories

Resources