listener on listview in android - android

ListView lv=(ListView) findViewById(android.R.id.list);
lv.setOnItemLongClickListener(new OnItemLongClickListener()//error:change to <listeners>
{
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {
//here is where i got the classCastException.
String[] tmp = (String[]) arg0.getItemAtPosition(row);
//tmp[0] ist the Text of the first TextView displayed by the clicked ListItem
Toast.makeText(ReminderListActivity.this, tmp[0].toString(), Toast.LENGTH_SHORT).show();
return true;
}
});

change to lv.setOnItemClickListener

You need to fix your imports.
you probably import the wrong class and now eclipse thinks you want something else.

You should use below listener:
m_listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> p_arg0, View p_arg1, int p_arg2, long p_arg3)
{
// TODO Auto-generated method stub
}
});

Related

OnItemSelectedListener of ListView is not getting envoked

I have a list view and using two listeners - OnItemClicked and OnItemSelected. The clicked listener is working properly but onItemSelected Listener is not getting invoked. I need OnItemSelected listener because sometimes selected is set pragmatically.
allClues.post(new Runnable() {
public void run() {
listView.setSelection(ind);
}
});
I am expecting that when selection is set its OnSelection listener will be invoked. But its not happening.
listView.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
iSelected = arg2 ;
Log.e("listargs", (String.valueOf(arg1)) + " " + String.valueOf(arg3));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
What could be wrong here ?
OnItemSelectedListener is used for the Spinner not for the Listview For Listview you have to use OnItemClickListener
You have to use OnItemClickListener like this
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int i,
long j) {
// do ur code
}
});

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.

Check selected item of ListView

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

ItemClickListener not working

I have problem with onItemClickListener, its not working for my code
My code is
listView = (ListView)findViewById(R.id.listView1);
CustomAadpter adapter = new CustomAadpter(this,R.layout.file_view_layout,R.id.contact_name,names);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println("view clicked is"+arg1);
}
});
Toast.makeText(context,"Position is"+ arg2,Toast.LENGTH_LONG).show();
Can be used if you want to show the postion as well. Hope this helps.
try
Toast.makeText(context,"message",Toast.LENGTH_LONG).show();

Categories

Resources