spinner a have 2 item : poison and non_poison
spinner b have 2 item : dart frog and viper
spinner c have 2 item : chicken and eggs
long story short, i want to make
"IF" selected item on spinner a = poison
"THEN" spinner b = visible, spinner c = gone
heres my code, tried using setOnItemSelectedListener, no error shown, but its not working the way it supposed to be, am i missing something in here ? please help me....
public class eatable extends Fragment {
public String spinner1x;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final FrameLayout mRelative = (FrameLayout) inflater.inflate(R.layout.berita_layout, container, false);
final TextView text1 = (TextView) mRelative.findViewById(R.id.text_test1);
final TextView text2 = (TextView) mRelative.findViewById(R.id.text_test2);
final TextView text3 = (TextView) mRelative.findViewById(R.id.text_test3);
final Spinner spinner1 = (Spinner) mRelative.findViewById(R.id.spinner_test1);
final Spinner spinner2 = (Spinner) mRelative.findViewById(R.id.spinner_test2);
final Spinner spinner3 = (Spinner) mRelative.findViewById(R.id.spinner_test3);
spinner1x = spinner1.getSelectedItem().toString();
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// your code here
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
});
return mRelative;
}
}
You forgot to retrieve the currently selected item of your spinner & save it to your spinner1x string, & to implement the onNothingSelected method:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
spinner1x = mRelative.getItemAtPosition(position).toString();
// your code here
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
});
The AdapterView.OnItemSelectedListener requires the onItemSelected()
and onNothingSelected() callback methods.
Android Spinners
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// Use spinner item position for this.
if (position == 0){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}
}
});
I am just using your code with corrections..
public class eatable extends Fragment {
public String spinner1x;
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final FrameLayout mRelative = (FrameLayout) inflater.inflate(R.layout.berita_layout, container, false);
final TextView text1 = (TextView) mRelative.findViewById(R.id.text_test1);
final TextView text2 = (TextView) mRelative.findViewById(R.id.text_test2);
final TextView text3 = (TextView) mRelative.findViewById(R.id.text_test3);
final Spinner spinner1 = (Spinner) mRelative.findViewById(R.id.spinner_test1);
final Spinner spinner2 = (Spinner) mRelative.findViewById(R.id.spinner_test2);
final Spinner spinner3 = (Spinner) mRelative.findViewById(R.id.spinner_test3);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> mRelative, View selectedItemView, int position, long id) {
// your code here
spinner1x = spinner1.getSelectedItem().toString();
if (spinner1x.equals("poison")){
spinner2.setVisibility(View.VISIBLE);
spinner3.setVisibility(View.GONE);
}else {
spinner2.setVisibility(View.GONE);
spinner3.setVisibility(View.VISIBLE);
}
}
});
return mRelative;
}
}
Related
i got a spinner with some options, and i want to change visibility of text box when the specific "other" option get selected , how should i do this ?
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
Try this:
.................
........................
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
// Views
categories= (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView);
// Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
// Set onItemSelected listener to spinner
categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView spinnerText = (TextView) view;
// Checking weather selected item is "Other" or not
if (spinnerText.getText().equals("Other"))
{
// Hide TextView
textView.setVisibility(View.INVISIBLE); // or View.GONE
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
.............
...................
Hope this will help
you can Implement OnItemSelected in the spinner and do the things what you want.
categories.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(arraySpinner!=null && arraySpinner[position].equalIgnoreCase("Other")){
// your code here
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I am trying to make app with one text view, one button and one spinner. I make button work showing random values from string array, but i have a lot different string arrays(list1, list2 etc). Now my question is when change spinner position, how to get button get another string array(from spinner) and show it to textview.
How to pass value from spinner when something is selected in spinner to button.
Any help is appreciated.
Here is my code:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Button btn;
public String[] myString,myString1;
public static final Random rgenerator = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//textview
final TextView tv = (TextView) findViewById(R.id.textureView);
final Resources res = getResources();
//string
myString = res.getStringArray(R.array.World_War_I);
myString1 = res.getStringArray(R.array.World_War_II);
//button
btn = (Button) findViewById(R.id.buttonxx);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
if (==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
//drop list
Spinner spinner = (Spinner) findViewById(R.id.spinnerrrr);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.kategorije, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
Use this
// when some cat selected
public void onItemSelected(AdapterView<?> parent, View view,
final int pos, long id) {
// An item was selected. You can retrieve the selected item using
btn.setTag(pos+""); // Passing as string
parent.getItemAtPosition(pos);
parent.setSelection(0);
parent.getSelectedItemPosition();
}
and then
btn.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
//i am missing code here, spinner position, and pass spinner position to if statement.
int index = Integer.parseInt(btn.getTag().toString());
if (index==0){
myString = res.getStringArray(R.array.list1);
String q = myString[rgenerator.nextInt(myString.length)];
tv.setText(q);
}
if (index==1){
myString1 = res.getStringArray(R.array.list2);
String q1 = myString1[rgenerator.nextInt(myString.length)];
tv.setText(q1);
}
}
});
I have an OnItemClick(), after that I wanted to get the ItemClicked name's and display it in a TextView.I do it normally, but the problem is that I get the ItemClicked name's inside a bracket! How do I avoid this?Here is the output:
{titre=hello}
I want only in my TextView:
hello.
Here is the code:
public void onItemClick(AdapterView<?> parent, View view, int position, long id ) {
displayTextViewTitle = (TextView)findViewById(R.id.text_view_title);
Object item = parent.getItemAtPosition(position);
String value = item.toString();
displayTextViewTitle.setText(String.valueOf(value));
}
Try this :
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
TextView item = (TextView) parent.getItemAtPosition(position);
String value = item.getText();
displayTextViewTitle.setText(value);
}
or even better try :
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
ListView lv = (ListView) findViewById(yourListViewName);
String value = lv.get(position).tostring();
displayTextViewTitle.setText(value);
}
If you are getting your String {titre=hello} like this after click on ListView. then you should use this:
String[] no = item.split("=");
String m = no[0];
String k = no[1];
String name = k.substring(0, k.length() - 1);
name is your final result String .
value is already a String, you don't have to use String.valueOf(value).
The listener method works fine, check this:
public class SplashActivity extends Activity implements OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
ListView lv = (ListView)findViewById(R.id.lv);
ArrayList<String> members = new ArrayList<String>();
members.add("titre=hello");
members.add("world");
ArrayAdapter s = new ArrayAdapter(this, android.R.layout.simple_list_item_1, members);
lv.setAdapter(s);
lv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
TextView displayTextViewTitle = (TextView) findViewById(R.id.textView1);
Object item = parent.getItemAtPosition(position);
String value = item.toString();
displayTextViewTitle.setText(String.valueOf(value));
}
}
Recheck the values inside your listview, the problem might be there
I am totally at my wits end with trying to change the value of a TextView based on what is selected in the adjoining Spinner.
public class SpinnerSelectItemListener implements OnItemSelectedListener {
private Context context;
public SpinnerSelectItemListener(Context c){
this.context = c;
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
View view = null;
TextView textView = null;
LayoutInflater inflater = LayoutInflater.from(context);
parent.getItemAtPosition(position);
view = new View(context);
view = inflater.inflate(R.layout.common_app_header, null);
textView = (TextView)view.findViewById(R.id.customer_name_value);
textView.setText("John");
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
When I run this is debug mode everything is happening as expected but when all is done, the value of the textView doesn't change on the emulator even when the debugger is showing the new value.
There is definitely something really silly that I am missing. Please help.
EDIT: The situation is something like I selected the id number of an employee from the spinner and depending on the selection, the TextView displaying the employee's name changes. The TextView I want to modify is outside the spinner.
EDIT2: This runs fine when I define the listener inline i.e. I write something like
modelspinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
TextView textView = null;
textView = (TextView)findViewById(R.id.customer_segment_value);
textView.setText("Commercial");
textView = (TextView)findViewById(R.id.TIV_value);
textView.setText(R.string.app1_name);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Can someone explain what is wrong with the code that I had written earlier.
See here
http://developer.android.com/guide/topics/ui/controls/spinner.html
Modify this method:
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
to
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
String value = (String) parent.getItemAtPosition(pos)
textView = (TextView)view.findViewById(R.id.customer_name_value);
textView.setText(value );
}
but I recommend you to move
textView = (TextView)view.findViewById(R.id.customer_name_value);
to the method onCreate of your Activity
I had a similar issue. I fixed it by getting the textview before I got into the onItemSelected. In my case, the spinner was part of a dialog. Inside the onCreateDialog, that is where I fetched the textview.
protected Dialog onCreateDialog(int id) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
switch (id) {
case DIALOG_ADD:
builder.setTitle("Create New Action");
final View textEntryView = getLayoutInflater().inflate(
R.layout.addactionrow, null);
builder.setView(textEntryView);
workingAmount = (TextView) textEntryView
.findViewById(R.id.WorkingActionamount);
Then inside the OnItemSelected I just used the textview and things started working as expected.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view,
int pos, long id) {
spinnerSelectedAssetID = id;
//Get the amount currently held here.....
long x = pfdata.getActionCurrentTotalForAssetByID(spinnerSelectedAssetID);
Log.d("X+", "X="+x);
workingAmount.setText(Long.toString(x));
workingAmount.setVisibility(View.VISIBLE);
Try This Code:
public class MainActivity extends Activity {
String[] text1 = { "SUNDAY", "MONDAY", "TUESDAY",
"WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY" };
int[] val1 = { 0, 1, 2, 3, 4, 5, 6};
Spinner spinner1;
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)findViewById(R.id.text1);
spinner1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}
OnItemSelectedListener onItemSelectedListener1 =
new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String s1 = String.valueOf(val1[position]);
textView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
};
}
XML CODE
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Try this code.And main key in this is " bResult.setText( spinner1csr.getString( spinner1csr.getColumnIndex(DatabaseHandler.KEY_ID1) ) );"
public class MainActivity extends AppCompatActivity {
Spinner s1, s2, s3;
TextView tex, tex1, bResult;
Cursor spinner1csr, spinner2csr, spinner3csr, spinner4csr, search;
SimpleCursorAdapter sca, sca2, sca3, sca4, sca6;
long spinner1_selected = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1 = (Spinner) findViewById(R.id.spinner1);
s2 = (Spinner) findViewById(R.id.spinner2);
s3 = (Spinner) findViewById(R.id.spinner5);
final TextView bResult = (TextView)
findViewById(R.id.barcodeResult);
dbhndlr = new DatabaseHandler(this);
// Get Cursors for Spinners
spinner1csr = dbhndlr.getAllLabelsAsCursor();
//Setup Adapter for Spinner 1
sca = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, spinner1csr,
new String[] {
DatabaseHandler.KEY_ID
},
new int[] {
android.R.id.text1
},
0
);
s1.setAdapter(sca);
s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
// bResult.setText(s1.getSelectedItem().toString());
spinner1_selected = id;
}
#Override
public void onNothingSelected(AdapterView << ? > parent) {}
});
spinner4csr = dbhndlr.getByRowid(spinner1_selected);
sca4 = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
spinner4csr,
new String[] {
DatabaseHandler.KEY_ID1
},
new int[] {
android.R.id.text1
},
0
);
I have problem with my spinners,
in my app are two spinners which gets data from database,
when i choose one option from first spinner, i want to remove this option from second spinner,
and add option which i removed from second spinner to first spinner and vice versa.
How can i do that ?
This is my present code:
public void showScoresCalculator() {
setContentView(R.layout.scores_calculator);
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/HelveticaProCn.ttf");
TextView txt = (TextView) findViewById(R.id.txt);
txt.setTypeface(font);
TextView team = (TextView) findViewById(R.id.choose_team);
team.setTypeface(font);
TextView group = (TextView) findViewById(R.id.choose_group);
group.setTypeface(font);
TextView title = (TextView) findViewById(R.id.textView1);
title.setTypeface(font);
TextView title2 = (TextView) findViewById(R.id.textView2);
title2.setTypeface(font);
final Spinner sp = (Spinner) findViewById(R.id.spinner3);
final Spinner sp1 = (Spinner) findViewById(R.id.spinner4);
final DataBaseHelper.MyData[] items = this.dh.getTeamsNameAndIDs("38");
final DataBaseHelper.MyData d = items[(int) sp.getSelectedItemId()];
final DataBaseHelper.MyData[] items2 = this.dh
.getTeamsNameAndIDs("47");
final DataBaseHelper.MyData d1 = items2[(int) sp1.getSelectedItemId()];
final ArrayAdapter<DataBaseHelper.MyData> adapter = new ArrayAdapter<DataBaseHelper.MyData>(
this, android.R.layout.simple_spinner_item,
this.dh.getTeamsNameAndIDs("38"));
final ArrayAdapter<DataBaseHelper.MyData> adapter1 = new ArrayAdapter<DataBaseHelper.MyData>(
this, android.R.layout.simple_spinner_item,
this.dh.getTeamsNameAndIDs("47"));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
DataBaseHelper.MyData d = items2[(int) sp1.getSelectedItemId()];
DataBaseHelper.MyData d1 = items[position];
home = d.getValue();
away = d1.getValue();
if(Integer.valueOf(home.toString()) == Integer.valueOf(away.toString())) {
Toast.makeText(getApplicationContext(), "Nie możesz wybrać dwóch tych samych drużyn.", Toast.LENGTH_SHORT).show();
}else {
showChart(d1.getValue(), d.getValue(), d1.getSpinnerText(), d.getSpinnerText(), "REMIS");
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter1);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
DataBaseHelper.MyData d = items[(int) sp.getSelectedItemId()];
DataBaseHelper.MyData d1 = items2[position];
home = d.getValue();
away = d1.getValue();
if(Integer.valueOf(home.toString()) == Integer.valueOf(away.toString())) {
Toast.makeText(getApplicationContext(), "Nie możesz wybrać dwóch tych samych drużyn.", Toast.LENGTH_SHORT).show();
}else {
showChart(d.getValue(), d1.getValue(), d.getSpinnerText(), d1.getSpinnerText(), "REMIS");
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
final Button next = (Button) findViewById(R.id.next);
final Button prev = (Button) findViewById(R.id.prev);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showNewsPage();
}
});
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showGroupCalculator();
}
});
Page = true;
}
Please, help me ! :)
Update the adapter by removing the selected data from the list. Use Handler concept to update adapter.
Set a new adapter on the Spinner via setAdapter(). Or, if it is an ArrayAdapter, use add(), insert(), and remove() to modify the data. Or, if it is a CursorAdapter, requery() it to get fresh data.