make not run onItemSelected after reset spinner - android

I have 3 spinner in my app, and I set all by
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_function_A();
}
});
spinner_3.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_function_B();
}
});
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_functionC();
}
});
Code above, look run function if Spinner Selected item, but
i want to reset all spinner with button like this
clear_all_spinner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spinner_1.setSelection(0);
spinner_2.setSelection(0);
spinner_3.setSelection(0);
}
});
I want after all reset with not running run_function_A(),run_function_B(),run_function_C() .. how the way?

you can try setting setOnItemSelectedListener(null) on all spinner before resetting the default selected item to the one at zeroth index and then setOnItemSelectedListener back to the one you set. I have modified you code as below:
clear_all_spinner.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spinner_1.setOnItemSelectedListener(null);
spinner_2.setOnItemSelectedListener(null);
spinner_3.setOnItemSelectedListener(null);
spinner_1.setSelection(0);
spinner_2.setSelection(0);
spinner_3.setSelection(0);
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_function_A();
}
});
spinner_3.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_function_B();
}
});
spinner_1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
run_functionC();
}
});
}
});
Also i see one bug in your code, you are setting setOnItemSelectedListener for spinner_1 twice ... see run_functionA() and run_functionC() are both set for spinner_1 selection. i have copied your code as it but you need to modify it.
It would be better to set the code of setting up ItemSelectedListener in a method block so instead of writing the same code at two places, you can simply call the method.

Related

Android 4.4 setOnItemLongClickListener bug?

I have a ListView with a setOnItemClickListener and setOnItemLongClickListener.
Since 1 year no problem with it. But with Android 4.4 I'll get with a LongClick both Methods executed.
For example:
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0,View arg1,int arg2, long arg3){
ListView lv = (ListView) findViewById(R.id.listView1);
final String Name = lv.getAdapter().getItem(arg2).toString();
// Make sth on click
}});
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
// Make sth on longclick
}
});
On long click both will be executed. Is this a bug or a problem with my code?
There are no problem with your code it's just that you haven't returned a value on your onItemLongClick boolean. Change it into:
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
// Make sth on longclick
return true;
}
});
This will prevent long click on doing further actions since take note that a longclick is still a click which is why onclicklistener triggers on this event.

Spinner on Button click not responding to Click events:

I have a spinner which drops down when a button is clicked. But when I am trying to set onItemSelectedListener, it is not taking the click events.
spnrLocation.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
System.out.println("location clicked" + arg2);
edtLocation.setText(parent.getItemAtPosition(arg2).toString());
System.out.println("wfefe"
+ parent.getItemAtPosition(arg2).toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
// try this
spnrLocation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
System.out.println("location clicked" + i);
edtLocation.setText(spnrLocation.getSelectedItem().toString());
System.out.println("wfefe"
+ spnrLocation.getSelectedItem().toString());
}
});
Any View supports the android:clickable attribute (which lets you make any View behave like a Button).
There is also android:focusable.

Not Getting Click Event On List View

here is my click listner...
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.i("", "adfadf");
Toast.makeText(ScoreListActivity.this, "Clicked",Toast.LENGTH_SHORT).show();
}
});
Whats wrong in this code ....\
I am not Getting Click when I click an item of LISTVIEW...
Call the following code on your list items as they are created:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

how to add onItemClick method in ListView?

I want to add onItemClick method in listview of my app. I created a listview by using http://jsharkey.org/blog/2008/08/18/separating-lists-with-headers-in-android-09/
but unable add click method.
please suggest solution.
Use this:
yourList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
}
});
I think you hava to use following method.
list.setAdapter(new EfficientAdapter(getParent()));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
}
});
Before creating acticity make sure it is ListActicity. After that pass the selected value (item) to the below method.
public void onListItemClick(ListView parent, View v, int position,
long id) {
selection.setText(items.get(position).toString());
}
Set an OnItemClickListener to your ListView.
Use the following code:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
// Put your custom code here
}
});
public void onListItemClick(ListView parent, View v, int arg1,
long id) {
selection.setText(items.get(arg1).toString());
}

create Listview without using listactivity.only use activty

I know how can implement listview without using listactivity.But i dont know how get the clicked item from the listview.Please anyone tell me how listen the clicked item from the listview without using listactivity.
Please Follow this Link. Here you can find simple Listview example using Activity.
Using listview.setOnItemClickListener you can get Click on Listview.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
You can implements OnItemClickListener and can write the method like below or just simply use eclipse to implement unimplemented method
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
}
First you have to make instance of list view Clickable like
lv.setClickable(true);
then
use following
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
use ListView.
Then with the listview object invoke
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//do your stuff
}
}
for click on listview in activiyt try this code...
ListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//write you action here
}
});

Categories

Resources