error help! syntax and OnItemSelectedListener - android

OK, so I'm pretty new to this, but I'm getting it. I just ran into this error and I have seen other people have had the same problem, but the standard fix (clean) doesn't work for me. I have no clue how to fix these errors! HELP PLEASE!
first error:
on sp2.setOnItemSelectedListener(new OnItemSelectedListener() { in my else if statement I keep geting this error:
The type new AdapterView.OnItemSelectedListener(){} must implement the inherited abstract method AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView)
I have the onNothingSelected there, and it works in my if statement, I mean all I did was copy and paste and edit.
second error:
on }); at the end of my else if statement i get the error:
Syntax error, insert ";" to complete Statement
but everything is there. the statement is completed!
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelecteds(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String reg_select = (sp1.getSelectedItem().toString());
if (reg_select.contentEquals("Southwest")){
sp2.setAdapter(sw_cit_adp);
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String sw_cit_select = (sp2.getSelectedItem().toString());
if (sw_cit_select.contentEquals("Lake Charles")){
sp3.setAdapter(sw_lake_charles_adp); }
else if (sw_cit_select.contentEquals("Iowa")){
sp3.setAdapter(sw_iowa_adp); }
;}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
};});}
else if (reg_select.contentEquals("South Central")){
sp2.setAdapter(sc_cit_adp);
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String sc_cit_select = (sp2.getSelectedItem().toString());
if (sc_cit_select.contentEquals("Lafayette")){
sp3.setAdapter(sc_lafayette_adp); }
else if (sc_cit_select.contentEquals("Gueydan")){
sp3.setAdapter(sc_gueydan_adp); }
;}
public void onNothingSelected(
AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
;
});
}

You have that method in the wrong place. You can't have an item selected and nto select anything, that's non-sensical.
You need to put the method under the listener, like the onItemSelected, but not inside onItemSelected.
This is what it should (basically) look like:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// Your code to do something with the selected item
}
});
Oh, and you need to use the exact method name... it's onItemSelected not onItemSelecteds

Related

set spinner item selected value in onitemclick android

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.

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.

No text is being set once I select an Item on an Android spinner.

No text is being set when I select any item from a spinner. What might be the error?
ArrayAdapter<String> pSelectAdapter = new ArrayAdapter<String>(PCreate.this,android.R.layout.simple_spinner_item, pNames);
pSelectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectP.setAdapter(pSelectAdapter);
selectP.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String nameSelected = pNames.get(arg2);
pID = pMatch.get(nameSelected);
p.setText(nameSelected);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I think here you missed to create the object of View class, Please match this line with your code, and make this change, This may help you..
selectP.setOnItemSelectedListener(new View.OnItemSelectedListener() {
(new View.OnItemSelectedListener() instaed (new OnItemSelectedListener()

Categories

Resources