Spinner.setOnItemClickListener not working - android

This is my code :
transactionTypeSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
}
});
But unfortunately OnItemClickListener method not working and show this message : setOnItemClickListener cannot be used with a spinner.
How can I solve the problem?
Thanks!

You should not call OnItemClickListener for a spinner. A Spinner does not support item click events.
transactionTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});

You need to use setOnItemSelectedListener()
Register a callback to be invoked when an item in this AdapterView has been selected.
Use
transactionTypeSpinner.setOnItemSelectedListener()
instead of
transactionTypeSpinner.setOnItemClickListener()
SAMPLE CODE
transactionTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Something changed", LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Please try with the following code hope will help you
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Add new category"))
{
// do your stuff
}
} // to close the onItemSelected
public void onNothingSelected(AdapterView<?> parent)
{
}
});

Related

how can i change my search with search position by id?

I have two spinner the second one is to link with the first one with position and a list view that shows a CV when you click on an item that is linked with position too, I want to replace it will be linked with id, how can I do please
this is my code spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
if(arrayListTache.size() > position)
{
spinner_tache.setSelection(position);
pos = position;
valueInteger = arrayListInteger.get(position);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
my code onclick list view
SubjectListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int
pos, long id) {
String selectedItem =
parent.getItemAtPosition(pos).toString();
Intent intent = new
Intent(getApplicationContext(),Main2Activity.class);
intent.putExtra("Position" , String.valueOf(id));
startActivity(intent);
// insert();
}
});

get spinner value and post to data base with volley

I'm going to POST value of spinners to the database
unfortunately, I do not know how to implement the POST method at any part of the CODE.
I'm having Problem with sending Snipper values.
public class FormActivity extends AppCompatActivity implements
....
spinnerStudy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_study = spinnerStudy.getItemAtPosition(spinnerStudy.getSelectedItemPosition()).toString();
if(!Objects.equals(all_study, "")){
Toast.makeText(getApplicationContext(),all_study, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_city = spinnerCity.getItemAtPosition(spinnerCity.getSelectedItemPosition()).toString();
if(!Objects.equals(all_city, "")) {
Toast.makeText(getApplicationContext(), all_city, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
s
pinnerUniversity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_university = spinnerUniversity.getItemAtPosition(spinnerUniversity.getSelectedItemPosition()).toString();
if(!Objects.equals(all_university, "")) {
Toast.makeText(getApplicationContext(), all_university, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
spinnerUnited.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_united = spinnerUnited.getItemAtPosition(spinnerUnited.getSelectedItemPosition()).toString();
if(!Objects.equals(all_united, "")) {
Toast.makeText(getApplicationContext(), all_united, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
button.setOnClickListener(this);
...
PLEASE Help ME...In this CASE, the post method HOW sends parameters?
How to get From Spinner a public String? Thank you ALL
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//position is spinner position
//mySpinner.getSelectedItem().toString(); IS value of position (string)
mMap.put("your Key",mySpinner.getSelectedItem().toString());
//the value add to map of volley
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Why isn't my onitemlongclick listener working?

From MainActivity:
public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {
private DataSourceSql mDataSourceSql;
protected ArrayList<String> mProfileNames;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDataSourceSql = new DataSourceSql(MainActivity.this);
mNames = new ArrayList<String>();
//this part here
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setLongClickable(true);
}
The id of the ListView in this case is set in the XML as android:id="#android:id/list".
And then later on in the same activity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, mNames.get(position), Toast.LENGTH_SHORT).show();
}
#Override
public boolean onItemLongClick(AdapterView<?> l, View v, final int position, long id) {
Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
return true;
}
But when I long-press an item in the list, the only thing that triggers is onListItemClick. I never get the message with the long click.
You shouldn't implement an interface like AdapterView.OnItemLongClickListener directly in your onCreate().. Just use your old way (Your class implement this interface) and with each method override, you have to write like this:
lv.setOnItemLongClickListener(this);
lv.setonListItemLongClickListener(this);
I think I figured it out, but someone please correct me if I'm missing something.
I removed the "implements..." thing and then added the following to the onCreate method:
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener
() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v, int
pos, long id) {
onListItemLongClick(v, pos, id);
return false;
}
});
Although I am not sure if I should be returning true or false there.
I also changed the long click function down below to
public boolean onListItemLongClick(View v, final int position, long id) {
Toast.makeText(this, "long clicked: " + mNames.get(position), Toast.LENGTH_LONG).show();
return true;
}
Edit:
Quicker approach is to just add lv.setOnItemLongClickListener(this); to my onCreate method in the OP.
Use This. Your Problem will Solve.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Long Click", Toast.LENGTH_SHORT).show();
return true;
}
});

Spinner return null value

when i am selected spinner value then some time it return a true value and some time it return null value
My spinner code is
list.add("Please Select Approver Name");
list.add("bishnu");
list.add("bishnu");
list.add("bishnu");
list.add("bishnu");
ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adp1);
sp1.setSelection(0);
Item Selected Listener
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strspin = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Toast.makeText(context, "value"+strspin, Toast.LENGTH_LONG).show();
I am working on it past two day
Please Help Me how i can fix this problem
Use the Toast inside the onITemSelected method as below.
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strspin = parent.getItemAtPosition(pos).toString();
Toast.makeText(context, "value"+strspin, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});

Unable to get Spinner value when click item

I am here trying display seleted value from dropdown spinner, but mysetOnItemSelectedListener is not working
My code is as follows:-
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.array.location,list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(
parent.getContext(),
"The color is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.location,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(
parent.getContext(),
"The color is "
+ spinner.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Make necessary changes and try the above code.If it not works then change Charsequence to String.
try this code
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Employee employee = employees.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Categories

Resources