How can i set the hash table?I've two variables,which are book name and number of chapter,inside spinner.I want to set text view according to book name and number of chapter.So how can i do it?
Basically, you have your elements inside an Arraylist.
All you need to do is fetch the element that is selected in this spinner.
spinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
//selectedItemView.invalidate()
Toast.makeText(Registration_Screen.this,
arrayList.get(position) + "",
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
Toast.makeText(Registration_Screen.this,
"Nothing Selected", Toast.LENGTH_LONG).show();
spinner.setVisibility(View.GONE);
}
});
So now you have the position of the arraylist, where your item is present.
let mw know if this works.
Related
I am trying to add spinner selected position value and edittext into sqlite database but I don't know how would I use Spinner position value during insertion into sqlite database.
Someone help me please.
Below is the spinner Item position code:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select expense type")) {
// Do nothing
} else {
item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Do nothing
}
Below is the Code where I want to use Spinner Item Position selected value and edittext value togather for inserting into sqlite database. so how could I get spinner position value for use into InsertExpense() function .
public void InsertExpense() {
addExpId.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onClick(View v) {
}
});
declare a global variable to store the selected item in spinner
String selectedItem=";
and here
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//yourList is the array your are using in your spinner adapter
selectedItem=yourList.get(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
//Do nothing
}
and then use this variable in your insertion method
i have to select items from spinner. Actually i am making gpa calculator.I have given grades in spinner and person can select from that. I have to assign grades with like A will 4, B+ will be 3.5 and so on. That's why i have to extract value after user has selected from spinner. But i am getting that spinner cannot be applied to adapter view.
setonitemclicklistener (android.widget.AdapterView.onitemclicklistener) in spinner cannot be applied to (anonymous android.widget.AdapterView.onitemselectedlistener)
you can do :
Spinner spinner = new Spinner(getApplicationContext());
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
android.util.Log.d(TAG, "position: " + position);
}
});
hope this will help you.
You should use setOnItemSelectedListenerto do something on an item selected like below:
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do Something
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This question already has answers here:
Spinner : onItemSelected not called when selected item remains the same
(11 answers)
Closed 5 years ago.
I have used Spinner to display toast on particular item selected in Spinner.
I am displaying toast when "other" is selected in Spinner.
I have done it as below :
spinnerTemp=(Spinner)findViewById(R.id.spinnerTemp);
spinnerTemp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinnerTemp.getSelectedItem().toString().equals("other")){
Toast.makeText(SocialLoginActivity.this, "Displayed", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
The issue is When I am selecting "other" for the first time, toast is displaying.. : FINE. Now, on the spot If i select "other" again, then toast is not displaying. Why ?
Now, if i select some other value and try again selecting "other", its working fine. Issue is with the selecting "other" one after another.
What might be the issue ?
Thanks.
Here a little better implementation:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
Try this
spinnerTemp=(Spinner)findViewById(R.id.spinnerTemp);
spinnerTemp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = spinnerTemp.getSelectedItem().toString();
if(item.equalsIgnoreCase("other")){
Toast.makeText(SocialLoginActivity.this, "Displayed", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I am trying to figure out how to set the text in my editText object based on an item I have saved in my listView. I am in the process of building a complex dice roller for DnD. This takes the users input and calculates the value, the user can also save the rolls for a later use. How do it get it populate the editText object for the user to re roll their dice? Here is the code that I use to save to listView:
public void saveToList(){
String getInput = et_roll1.getText().toString();
if (addArray.contains(getInput)){
Toast.makeText(getBaseContext(), "Item Already Added", Toast.LENGTH_LONG).show();
}
else if (getInput == null || getInput.trim().equals("")){
Toast.makeText(getBaseContext(), "Input Field is Empty", Toast.LENGTH_LONG).show();
}
else{
addArray.add(getInput);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, addArray);
lv_rolls.setAdapter(adapter);
((EditText)findViewById(R.id.et_roll1)).setText(" "); //reset editText to blank
}
Here is the code for my onClickListener:
lv_rolls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
resetValue();
}
});
lv_rolls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
tv_roll1_result.setText(" ");
et_roll1.setText(String.valueOf(lv_rolls.getItemAtPosition(position)));
}
});
I have a need to add 3 related spinners where the selected content in the first affects what is displayed in the second and then what is selected in the second affects the content in the third. Has anyone already faced this problem and how did you over come it?
I was thinking of maintaining a number of arrays in the resources which are numbered and then loaded into the adapter based on the choice selected in the previous spinner. Is this a good way?
Thanks,
m
I would have done this with something like that:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
SpinnerAdapter adapter = ...create or load the second adapter based on selected item...
spinner2.setAdapter (adapter);
spinner3.setAdapter (..create empty adapter...);
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
spinner2.setAdapter (..create empty adapter...);
spinner3.setAdapter (..create empty adapter...);
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
SpinnerAdapter adapter = ...create or load the third adapter based on selected item...
spinner3.setAdapter (adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
spinner3.setAdapter (..create empty adapter...);
}
});