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);
Related
// RecyclerAdapter (On click listener for the image)
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer p = getAdapterPosition()
Drawable d = imageView.getDrawable();
((MainActivity) itemView.getContext()).showOrganisation(p,d);
}
});
// Main Activity
public void showOrganisation(Integer p, Drawable d) {
myDialogOrg.setContentView(R.layout.popup_organisation);
TextView selectButton = myDialogOrg.findViewById(R.id.popOrgClose);
ImageView imageView = myDialogOrg.findViewById(R.id.popUpLogo);
imageView.setImageDrawable(d);
myDialogOrg.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
selectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences pref = v.getContext().getSharedPreferences("CheckBoxStateOrganisationer", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
// Send mysql query to database that a charity is selected
// Code
response{
// Store State of checkbox
editor.putBoolean("CheckBoxOrganisationer"+p, true);
editor.commit();
}
////////////////////////////////
// update state of checkbox in//
////////////////////////////////
-> I've added code here <-
// Dismiss popup window
myDialogOrg.dismiss();
}
});
myDialogOrg.show();
}
What I've tried is Fragment.adapter.notifyItemChanged(p);
and Fragment.adapter.notifyDataSetChanged();
The outcome is that the cardView in the recyclerview gets cleared and the original layout of the recycled cardView is displayed.
Is there a way to reload the "MyViewHolder" of the recycler.adapter for one position so that the cardview updates?
// Organisation_Fragment
Initiates the adapter
When the button in the AlertDialog is clicked and the alert is dismissed the checkbox of a specific item(position) in the recyclerView should appear selected.
In this case, I usually add a method in my Adapter which reacts to the data change
Adapter:
public void updateData(List<String> newList){
suggestions.clear();
if(newList!=null) suggestions.addAll(newList);
notifyDataSetChanged();
}
Your Activity:
private AutoCompleteAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
adapter = new AutoCompleteAdapter
(context, suggestions, etAnswer);
recyclerView.setAdapter(adapter);
}
your click listener
selectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your other code
////////////////////////////////
// update state of checkbox in//
////////////////////////////////
-> I've added code here <-
adapter.updateData(yourNewList);
}
});
Use the adapter method
adapter.notifyItemChanged(position, your Object);
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 am trying to save listview selected position in a string and passing to next activity as shown below:
I have set my listview default selection to first row by setting it as :
lv1.setAdapter(adapter);
lv1.setItemChecked(0, true);
lv1.setSelection(0);
Declaration variables:
int myposition;
String myvalue;
LIstView ItemClick:
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
myposition=position;
myvalue = String.valueOf(myposition);
}
});
This is how I move it to next activity using button click:
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),Step3Activity.class);
// Pass a single position
i.putExtra("selposition", myvalue);
// Open SingleItemView.java Activity
startActivity(i);
}
});
This is how I get the intent on the newActivity:
Intent i = getIntent();
// Get a single position
position = i.getExtras().getInt("selposition");
myvalue = String.valueOf(position);
Toast.makeText(getBaseContext(),
myvalue,
Toast.LENGTH_SHORT).show();
But it always gives me the position '0'. So am i getting this by setting the default selection of 1st row ? Can anyone clarify me with this?
try something like this to get the value:
position = Integer.parseInt(i.getExtras().getString("selposition"));
Hope it will work.
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);
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.