I have an activity which has a spinner and this is the method that handles a selection of on item within that spinner.
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
// Do stuff based on selection
}
}
Further on I have a button and the code looks like this
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// This doesnt work
onItemSelected(MyProfileActivity.this, v, 0, 2);
}
});
}
So essentially what I'm trying to do is simulate an item being selected in the spinner via a button press. Is this even possible?
Instead of calling onItemSelected method on Button Click you should use View.performItemClick method to click on Spinner Item on Button Click do it as:
int item_postion=0;// item which you want to click
your_spinner.setSelection(item_postion, true);
View item_view = (View)your_spinner.getChildAt(item_postion);
long item_id = your_spinner.getAdapter().getItemId(item_postion);
your_spinner.performItemClick(item_view, 0, item_id);
Try this..
btnChangeDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinner.setSelection(index); // which you what set the spinner position
String item = spinner.getSelectedItem().toString().trim(); //which you need to get the selected item from the spinner
System.out.println("Selected item : "+item):
}
});
Try this code:
spin.performItemClick(view, position, id);
Related
Hey guys This is my first question on stackoverflow.
I am searching on a topic almost for 1 month but did not find any relevant answer.
I want to set clicklistner to the list child items in the main activity.
I know about Listview.setOnItemClickListner... and also know how to set listview's child item clicking in own adapter.
But i don't want this..
I want to set listview's child item clicking in the main activity
this is my main activity code for list item clicking...
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
final TextView tx;
tx=(TextView)view.findViewById(R.id.textList);
tx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(listviewclick_Activity.this, "in= "+String.valueOf(position), Toast.LENGTH_SHORT).show(); // clicking for textview on list
}
});
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(listviewclick_Activity.this,"out= "+ String.valueOf(position), Toast.LENGTH_SHORT).show(); // clicking for whole listitem
}
});
}
});
This code works fine but it has a problem that a user has to click first
time after that all clicking will be set...
Remember i dont want to click a childitem in adapter. i only want to click it in main activity.
Hoping for a good answer...
I don't understands what you are doing but this part:
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(listviewclick_Activity.this, "out= " + String.valueOf(position), Toast.LENGTH_SHORT).show(); // clicking for whole listitem
}
});
is wrong !, because view is the list view item (row) that is already clicked.
instead replace it with:
Toast.makeText(listviewclick_Activity.this, "out= " +String.valueOf(position),Toast.LENGTH_SHORT).show(); // clicking for whole listitem
Also this part:
tx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(listviewclick_Activity.this, "in= "+String.valueOf(position), Toast.LENGTH_SHORT).show(); // clicking for textview on list
}
});
will not work unless you click the listview item, so u should move it inside the adapter.
I am trying to get tapped list item position in Adapter class on button click, but did not get any success
First of all i have to get tapped list item and then want to delete that row
getting something like this: D/strPosition::-(1922): com.and.field.Transport#b3ddf5c0
using below code:
viewHolder.btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
strPosition = arrayList.get(position).toString();
Log.d("strPosition::-", strPosition);
// and then want to delete that row using tapped position
strName = arrayList.get(position).getName().toString();
}
});
You can not use position in your onClick method, instead use next approach
viewHolder.btnDelete.setTag(position);
viewHolder.btnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
Integer taggedPosition = (Integer) v.getTag();
Log.d("Position::-", Integer.toString(taggedPosition));
strName = arrayList.get(taggedPosition).getName().toString();
}
});
I have ListView, and everubody listItem have a button.
I cant get event on first click on imageView, but after first when I click second time I get Event. Why I cant get response on first click on imageView?
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
ImageView addLarge = (ImageView) view.findViewById(R.id.addLargeImage);
ImageView addSmall = (ImageView) view.findViewById(R.id.addSmallImage);
addLarge.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addProductToCart = true;
addToOrderListClick = true;
OrderProduct order = new OrderProduct(listProductId.get(position), listProductName.get(position),
listProductNameEn.get(position), listProductImageUrl.get(position), "large", listProductPriceLarge.get(position));
orderListProduct.add(order);
animCopyListItemAddToCart(view, position, mainRelativeLaout);
Toast.makeText(getBaseContext(), "AddLarge", Toast.LENGTH_SHORT).show();
}
});
addSmall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addProductToCart = true;
addToOrderListClick = true;
OrderProduct order = new OrderProduct(listProductId.get(position), listProductName.get(position),
listProductNameEn.get(position), listProductImageUrl.get(position), "small", listProductPriceSmall.get(position));
orderListProduct.add(order);
animCopyListItemAddToCart(view, position, mainRelativeLaout);
Toast.makeText(getBaseContext(), "addSmall", Toast.LENGTH_SHORT).show();
}
});
}
}
Because you are setting your onClickListener() for your ImageViews within your ListView onItemClickListener().
So the user selects an item in your list, and only then does an onClickListener for your ImageViews get assigned. So the second time, the onclick listener of the ImageView gets successfully fired.
To navigate around this issue, assign the individual ImageView onClickListeners within a custom adapter so they are assigned when they come into view as opposed to when a user clicks an item in list.
I cannot retrieve the selected value of the spinner. Can somebody help me? Thanks.
This is how I retrieve the data. It's inside the onCreate() method.
mPrefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SpinnerLang.setSelection(mPrefs.getInt(PREF_SPINNER, 0));
Then I save the Spinner's value in a button, so when the user selects from the Spinner and clicks the button the selected value will be saved.
//Listening to button event
btnDone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Editor e = mPrefs.edit();
int Spinnervalue = SpinnerLang.getSelectedItemPosition();
e.putInt("PREF_SPINNER", Spinnervalue);
e.commit();
}
Use a spinner.setOnItemSelectedListener() to store the selected item position in preference
spinner.setAdapter(mySpinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
/**
* Called when a new item is selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
mPrefs.edit().putInt("PREF_SPINNER", position).commit();
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}); // (optional)
//UPDATE - When you are using a button onCLick()
// to set the spinner selection:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
spinner.setSelection(mPrefs.getInt("PREF_SPINNER", 0));
}
});
// set the spinner selected item from preference after
// spinner.setAdapter(mySpinnerAdapter);
// -or it will default to the first item
// spinner.setSelection(mPrefs.getInt(PREF_SPINNER, 0));
You have these methods that you can use depending on how you have declared your spinner.
getSelectedItem()
getSelectedItemPosition()
getSelectedItemId()
Make sure constant PREF_SPINNER and "PREF_SPINNER" used has same value assigned to it in your code
mPrefs.getInt(PREF_SPINNER, 0)
e.putInt("PREF_SPINNER", Spinnervalue);
I am trying to use a spinner control that will enable the user to delete any list element.
I have an 'add' button to add elements to the list, and a 'delete' button that removes the currently-displayed item from the list.
It works as expected except when the user deletes the last item in the list. At that point, all of the list's items are deleted.
My code is as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// grab our UI elements so we can manipulate them (for the Spinner)
// or add listeners to them (in the case of the buttons)
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
// create an arrayAdapter an assign it to the spinner
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
((ArrayAdapter)m_adapterForSpinner).setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
// add listener for addButton
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
clearButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
// add listener for addButton
private void addNewSpinnerItem() {
if (m_addItemText.getText().length() == 0) {
Toast.makeText(getApplicationContext(), "The textView is empty", Toast.LENGTH_LONG).show();
} else {
CharSequence textHolder = "" + m_addItemText.getText();
((ArrayAdapter) m_adapterForSpinner).add(textHolder);
}
m_addItemText.setText("");
}
private void clearSpinnerItems() {
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object t = m_adapterForSpinner.getItem(pos);
((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO
}
});
}
Does anyone have any ideas or suggestions on how to make this work?
The problem with your code is that the deletion is inside the onItemSelected callback, which gets called every time you are deleting an entry, thus deleting recursively until you effectively do not have any more entries to select. If you add a log inside that method:
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
you will see what I mean. I'm sure you can come up with more elegant code, but a quick and dirty hack is to set up a boolean flag to stop the recursion after the first deletion. See the snippet below and add the commented lines to your own code:
public class SpinnerTest extends Activity {
Spinner m_myDynamicSpinner;
EditText m_addItemText;
ArrayAdapter m_adapterForSpinner;
public static boolean cleared = false; // <--- set up a static boolean here
#Override
public void onCreate(Bundle savedInstanceState) {
// all your code unchanged
clearButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
cleared=false; // <--- nope, we did not clear the value yet
clearSpinnerItems();
}
});
}
// code unchanged
private void clearSpinnerItems() {
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object t = m_adapterForSpinner.getItem(pos);
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
if (!cleared) // <--- did I do it already?
((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
cleared=true; // I did it!
}
// code unchanged
i cann't understand your question.any way you can get the position of the selected item by using getSelectedItemPosition() method.