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) {
}
Related
Hello friends i have spinner as following
When i click on any item click event than it should be set like following
it means when i select Afghnistan at that time it should be set value as AF and rest of country value remain as it is my code is as follow
On Spinner item click
mSpinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
mStringCountryID=mArrayListCountryDatas.get(position).getId();
mSpinnerCountry.setPrompt(mArrayListCountryDatas.get(position).getIso_alpha()+"
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
When i run above code it is not set value any idea how can i make it possible ?
mSpinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int mStringCountryID = s1.getSelectedItemPosition(); Toast.makeText(getBaseContext(),
"You have selected item : " + presidents[mStringCountryID],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this... variable mStringCountryID will hold your specific complete value do what ever you want to do with this value.
I have a Grid View that have 4 images .So i want to identify which image has been clicked so that corresponding to that i can start a new activity .
So please help me how can i get this
I have tried this
dataView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
getView can be used for this purpose.for this in getView find View and apply onClicklistner their.to make all views clickable you need to setFocusable(false) on all focusable views.
I have got the solution
dataView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
links[arg2], Toast.LENGTH_SHORT).show();
}
});
int arg2 in the OnItemClick method specifies the position.Using that you can get the item clicked.
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
}
});
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();
}
expListAdapter = new ColorAdapter(this, GrouppList, colors);
// setListAdapter(expListAdapter);
exlv1=(ExpandableListView) findViewById(R.id.expandableListView1);
this.exlv1.setAdapter(expListAdapter);
//exlv1.setAdapter(expListAdapter);
this.exlv1.setOnItemClickListener(new OnItemClickListener() {
#Override //THIS NOT WORKING
public void onItemClick(AdapterView<?> aview, View v,
int position, long id) {
Toast.makeText(FindFilesByType.this, "position "+position, Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
});
this.exlv1.setOnChildClickListener(new OnChildClickListener() {
#Override//THIS NOT WORKING
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
int arg3, long arg4) {
// TODO Auto-generated method stub
Toast.makeText(FindFilesByType.this, "position 1 : "+arg3, Toast.LENGTH_LONG).show();
return false;
}
});
into the above code i wat to know which item has clicked from the list but i am not able to know that
i want to know its position i've used expandable list what is i am missing?
thaks in advance..
Hi Please check below link
http://about-android.blogspot.com/2010/04/steps-to-implement-expandablelistview.html
http://techdroid.kbeanie.com/2010/09/expandablelistview-on-android.html