Spinner on Button click not responding to Click events: - android

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.

Related

When calling onItemLongClickListener,onItemClickListener also called even though I returning true in onItemLongClick

I don't want to call onItemClick when onItemLongClick is called.
I am using SlideAndDragListView and returning true only in onItemLongClick.
matchedUsersListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
InviteMatchedUsersFragment.this.onItemLongClick(parent,view,position,id);
return true;
}
});
#Override
public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ImageView view1 = (ImageView) view.findViewById(position);
matchedPassengersAdapter.onUserClick(view1, position);
}
In OnItemClick I am doing it differently
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(LOG_TAG, "onMatchedUserSelection()");
if(matchedPassengersAdapter.getSelectedPassengersCount() != 0)
{
ImageView view1 = (ImageView) view.findViewById(position);
matchedPassengersAdapter.onUserClick(view1, position);
}
else {
//onItemClickGoesHere
}
}
#Override
public void onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
ImageView view1 = (ImageView) view.findViewById(position);
matchedPassengersAdapter.onUserClick(view1, position);
}
This May helps You
Ref: Set long click listener for listview
Your question is very similar to this one, but it looks like it's not an exact duplicate.
What you've noticed is that the ListActivity class does not have a method override specifically for this case.
In order to add this functionality as a method override, your class should implement the AdapterView.OnItemLongClickListener interface, and then you can add the onItemLongClick() method override, which acts just as the onListItemClick() method override you already have, but responds to long clicks.
Just make sure that you follow instructions from this answer, you must use android:longClickable="true" in the layout xml, or call listview.setLongClickable(true);
Ex
public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener {
ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.list);
listview.setLongClickable(true);
}
#Override
public boolean onItemLongClick(AdapterView<?> l, View v,
final int position, long id) {
Toast.makeText(this, "long clicked pos: " + position, Toast.LENGTH_LONG).show();
return true;
}
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "short clicked pos: " + position, Toast.LENGTH_LONG).show();
}
//....................

make not run onItemSelected after reset spinner

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.

on long click listener ListActivity class

I have an app which use ListView,I have set onListItemClick event to view details about contact,and I want to implement onLongListItemClick to show a dialog and I don't know why it doesn't work,I mean nothing happens,I tried like this:
public void onListItemClick(ListView parent, View v, int position, long id)
{
//code
}
public void onLongListItemClick(ListView parent, View v, int position, long id)
{
showdialog();
}
Try like this :
listview.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Long Clicked : ", Toast.LENGTH_LONG).show();
return true;
}
});
You need to add return true; here otherwise it will transfer control to single click event listener once you release your touch.
Hope it helps you.
Thanks.

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

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

Categories

Resources