using spinner to converter - java

I'm creating converter (distance) 4 Android. My idea was do it like:
EditText - user put value f.e. 40
two Spinners - first to set started unit and second to set destination unit f.e. first [cm] second [meters]
TextView with result
Button to calculating
I did functions with calculating values, the problem is how to choose units
this is my code which didn't work
spinnerP = (Spinner) findViewById(R.id.spinnerPocz);
ArrayAdapter<CharSequence> adapterP = ArrayAdapter.createFromResource(this, R.array.odlegloscArray, R.layout.support_simple_spinner_dropdown_item);
adapterP.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinnerP.setAdapter(adapterP);
spinnerP.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getBaseContext(), parent.getItemIdAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerD = (Spinner) findViewById(R.id.spinnerDoc);
ArrayAdapter<CharSequence> adapterD = ArrayAdapter.createFromResource(this, R.array.odlegloscArray, R.layout.support_simple_spinner_dropdown_item);
adapterD.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinnerD.setAdapter(adapterD);
spinnerD.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (spinnerP.getItemAtPosition(position) == 0) {
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "zmień wartość docelową", Toast.LENGTH_SHORT).show(); //units the same
break;
case 1:
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
metryNaKm(); //meters to km
}
});
break;
case 2:
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
metryNaCm(); //meters to cm
}
});
break;
case 3:
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
metryNaCale(); //meters to inch
}
});
break;
//etc.
//then there is
else if (spinnerP.getItemAtPosition(position) == 1) {
switch (position) {
case 0:
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
kilometryNaM(); //km to meters
//etc
I guess that doing it like this is horrible, but I'mma not advanced, I am open to suggestions

What you should do is to remove all your OnItemselectedListeners, and use only one OnClickListener for your button. Then use getSelectedItemPosition.
btnOblicz.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedP = spinnerP.getSelectedItemPosition();
int selectedD = spinnerD.getSelectedItemPosition();
switch(selectedP){
case 0:
switch(selectedD){
case 1:
convertFrom0to1();
break;
case 2:
convertFrom0to2();
....
}
break;
....
}
}
});

Related

How To Set Title In Another Activity Based on The Text of The Selected Spinner Option On Clicking A Button In The Main Activity

I set out to create a project of an Android app consisting of 3 activities.
The first activity has a layout with 3 elements, 2 spinners and a button.
I set out with an intention to make them work in the following manner:
MainActivity.java:
1. Let's just say that I wanted the first spinner to load a set of categories on app startup. e.g., Income, Expenditure.
2. After I select a category from the first spinner, the second spinner will load up its respective sub-categories in the second spinner. e.g., If I choose Income in the first spinner, the second spinner with load up Commission, Salary, Bonus, etc. and if Expenditure is chosen, Monthly Expenditure, Conveyance, Bills, etc are loaded in the second spinner.
3. Now, on the basis of which sub-category I chose, when I click the button, it will open a new activity and set the title of the action bar for that particular category and sub-category. e.g., If I chose Income and Salary, the next activity will show Income, Salary as title after I click the button in the main activity.
I have created a class named Utils.java to store the set of category and sub-category names as string arrays.
public class Utils{
public static String[] mainCategoryList =
{
"Choose One",
"Income",
"Expenditure"
};
//
public static String[] chooseOne_list=
{
"Choose One"
};
public static String[] income_subCategoryList =
{
"Bonus",
"Commision",
"Salary"
};
public static String[] expenditure_subCategoryList=
{
"Bills",
"Conveyance",
"Monthly Expenditure"
};
public static String[] income=
{
"Income, Bonus",
"Income, Commission",
"Income, Salary"
};
public static String[] expenditure=
{
"Expenditure, Bills",
"Expenditure, Conveyance",
"Expenditure, Monthly Expenditure"
};
}
The coding I have done for MainActivity.java is given below,
public class MainActivity extends AppCompatActivity {
Spinner spinner1_mainCategory, spinner2_subCategory;
Button buttonChoose;
String spinner1Item, spinner2Item;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Spinner element
spinner1_mainCategory = (Spinner)findViewById(R.id.spinner1_mainCategory);
spinner2_subCategory = (Spinner)findViewById(R.id.spinner2_subCategory);
//Button element
buttonChoose = (Button)findViewById(R.id.buttonChoose);
//To keep onCreate() clean of multiple adapter setting
adapterMethod(spinner1_mainCategory,mainCategoryList);
// Spinner click listener
spinner1_mainCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
//String.valueOf(array_name.getSelectedItem()) captures the value of the selected option of a specific string array onClick event
spinner1Item = String.valueOf(spinner1_mainCategory.getSelectedItem());
switch (position){
case 0:
adapterMethod(spinner2_subCategory,chooseOne_list);
spinner2_subCategory.setVisibility(View.GONE);
//Button click listener
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (spinner1Item.contentEquals(Utils.mainCategoryList[0])){
Toast toast = Toast.makeText(getApplicationContext(), "Choose a Category First", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL,0,0);
toast.show();
}
}
});
break;
case 1:
adapterMethod(spinner2_subCategory,income_subCategoryList);
spinner2_subCategory.setVisibility(View.VISIBLE);
break;
case 2:
adapterMethod(spinner2_subCategory,expenditure_subCategoryList);
spinner2_subCategory.setVisibility(View.VISIBLE);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner2_subCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, final int position, long id) {
spinner2Item = String.valueOf(spinner2_subCategory.getSelectedItem());
switch (position){
case 0:
//Income Category, Bonus Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[0])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.income[0]);
}
});
}
//Expenditure Category, Bills Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[0])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.expenditure[0]);
}
});
}
break;
case 1:
//Income Category, Commission Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[1])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.income[1]);
}
});
}
//Expenditure Category, Conveyance Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[1])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.expenditure[1]);
}
});
}
break;
case 2:
//Income Category, Salary Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[1]) && spinner2Item.contentEquals(income_subCategoryList[2])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.income[2]);
}
});
}
//Expenditure Category, Monthly Expenditure Sub-category
if (spinner2Item.contentEquals(Utils.mainCategoryList[2]) && spinner2Item.contentEquals(expenditure_subCategoryList[2])){
buttonChoose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeTitleInNextActivity(Utils.expenditure[2]);
}
});
}
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void adapterMethod(View v, String[] list){
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, list);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
// attaching adapter to spinner for district
if (v.getId()==R.id.spinner1_mainCategory){
spinner1_mainCategory.setAdapter(dataAdapter);
spinner1_mainCategory.setSelection(0, false);
}
if (v.getId()==R.id.spinner2_subCategory){
spinner2_subCategory.setAdapter(dataAdapter);
spinner2_subCategory.setSelection(0, false);
}
}
public void changeTitleInNextActivity(String title){
intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.putExtra("Title", title);
startActivity(intent);
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
String title = intent.getStringExtra("Title");
getSupportActionBar().setTitle(title);
}
}
I don't understand why, but I can't get the coding to work for me the way I wanted it to. Selection from the first and second spinner works perfectly, but its the button that I can't get to work to set the title in the next activity no matter how many approaches I try.
Tell me, guys, what did I do wrong? How to better code to avoid this kind of pitfall.
Can't make the Button "Choose":
to go to the SecondActivity
In your second activity,
get your passed string
String title=getIntent().getStringExtra("Title");
Then set your title
getSupportActionBar().setTitle(title);

Reading seekarc position value in Android

I am beginner in Android. I created a circular seekarc with temperature showing text and I added in middle of seekarc. The text value is starting with 10 and ending with value is 20.
I want to read each text value position for seekarc. If the text value position is false, then seekarc value position is also false.
How can I achieve this? Can any one help me?
Below is my code:
seekarc = (SeekArc) v.findViewById(R.id.seekAr);
seekArcprogres = (TextView) v.findViewById(R.id.seekArcProgres);
seekarc.setOnSeekArcChangeListener(new SeekArc.OnSeekArcChangeListener() {
#Override
public void onProgressChanged(
SeekArc seekArc, final int progress,
boolean fromUser) {
seekArcprogres.setText(String.valueOf(progress + 10));
switch (progress) {
case 0:
Op.processInputCmd(getArguments().getString(""), Manager.Command.EMP_16, false);
break;
case 1:
Op.processACButtonInputCmd(getArguments().getString(""), Manager.Command.EMP_10, false);
break;
case 2:
;
Op.processInputCmd(getArguments().getString(""), Manager.Command.EMP_11, false);
break;
case 3:
Op.processInputCmd(getArguments().getString(""), Manager.Command.EMP_12, false);
break;
}
seekArcprogres.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Op.processInputCmd(getArguments().getString(""), Manager.Command.EMP_10_LR, false);
}
});
seekArcprogres.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Op.processInputCmd(getArguments().getString(""), Manager.Command.EMP_10_LR, true);
return true;
}
});
seekArcprogres.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Op.processInputCmd(getArguments().getString(""), Manager.Command.EMP_11_LR, false);
}
});
seekArcprogres.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Op.processInputCmd(getArguments().getString(""), Manager.Command.EMP_17_LR, true);
return true;
}
#Override
public void onStartTrackingTouch(
SeekArc seekArc) {
}
#Override
public void onStopTrackingTouch(
SeekArc seekArc) {
}
});

ListView OnItemClickListener not being triggered in bottom sheet

I have created a bottom sheet using the newly updated support library. Basically, whenever an item in my recyclerview is long-clicked, a bottom sheet is shown (containing a linearlayout which contains a listview). But whenever I tap the items in the listview nothing happens. I added toasts and it is never triggered when I tap on the sheet items. Any ideas? Thanks in advance!
Here is the listview initialization method:
public void initSheet() {
bottomSheet = findViewById(R.id.list_sheet);
ListAdapter adapter = new ListAdapter(this, R.layout.custom_sheet_row, getSheetInfo(), "Sheet");
list = (ListView) findViewById(R.id.list_sheet_list);
list.setAdapter(adapter);
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setHideable(true);
}
Here is the recyclerview long click:
#Override
public void onLongClick(View view, int position) {
final String itemText = ((TextView) view.findViewById(R.id.textRow)).getText().toString();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(ListActivity.this, "sdjakfjs", Toast.LENGTH_SHORT).show();
switch (position) {
case 0:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_TEXT, itemText);
startActivity(Intent.createChooser(sharingIntent, "Share the item"));
break;
case 1:
final ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setPrimaryClip(ClipData.newPlainText("New", itemText));
break;
case 2:
editText(itemText, position);
break;
case 3:
listAdapter.deleteItem(position);
break;
}
behavior.setState(BottomSheetBehavior.STATE_HIDDEN);
}
});
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
try this.
implement custom clicklistener it will work pefectly.
// creating interface to handle clicks for Recycler view items
public interface ClickListener
{
void onClick(View view,int position);
void onLongClick(View view,int position);
}
public static class CustomRecyclerTouchListener implements RecyclerView.OnItemTouchListener
{
private GestureDetector gestureDetector;
private MainActivity.ClickListener clickListener;
public CustomRecyclerTouchListener(Context context,final RecyclerView recyclerView,final MainActivity.ClickListener clickListener)
{
this.clickListener=clickListener;
gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener()
{
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child=recyclerView.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clickListener !=null)
{
clickListener.onLongClick(child,recyclerView.getChildPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View child=recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(motionEvent))
{
clickListener.onClick(child,recyclerView.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean b) {
}
}
}
Then implement like this.
recyclerView.addOnItemTouchListener(new CustomRecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
#Override
public void onClick(View view, int position) {
User user=users.get(position);
Toast.makeText(MainActivity.this,user.getName()+" is clicked",Toast.LENGTH_SHORT).show();
}
#Override
public void onLongClick(View view, int position) {
User user=users.get(position);
Toast.makeText(MainActivity.this,user.getName()+" is long clicked",Toast.LENGTH_SHORT).show();
}
}));
For more details refer here:- http://coderzpassion.com/android-working-with-recycler-view/
Ok, what I ended up doing was creating 4 textviews inside of the linear layout instead of using the listview. Then I just added an onclick listener for all of the textviews to do their respective commands

How to send null string using spinner on android

How to submit spinner selecteditem text via btnup and why selectedReport on btnup cases is declared not used?I have search hard enough to solve this but i still blankon what i need to do in order to get text on spinner to suit the coding.
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername, uploadImageName;
Button bLogout;
ImageView viewImage;
Button b,btnup;
private String selectedReport = null;
} private void createSpinnerDropDown() {
//get reference to the spinner from the XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Array list of report to display in the spinner
List<String> list = new ArrayList<String>();
list.add("Crime");
list.add("Bribery");
list.add("Schools problem");
list.add("Homeless");
list.add("Rural Problems");
list.add("Public Transport");
//create an ArrayAdaptar from the String Array
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selectedItem = parent.getItemAtPosition(pos).toString();
switch (parent.getId()) {
case R.id.spinner:
if (selectedReport != null) {
Toast.makeText(parent.getContext(), "Report you select is " + selectedItem,
Toast.LENGTH_LONG).show();
}
selectedReport = selectedItem;
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
case R.id.btnup:
Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String selectedReport = spinner.getSelectedItem().toString();
break;
}
}
Declare your views before onCreate in your class:
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername, uploadImageName;
ImageView viewImage;
Button b, bLogout, btnup;
Spinner spinner;
String selectedReport;
In your onCreate, add the following:
String[] list = {"Crime", "Bribery", "Schools Problem", "Homeless", "Rural Problems", "Public Transport"}
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Toast.makeText(this, "Report selected: " + list[position], Toast.LENGTH_LONG).show();
//You can remove this switch statement, if you don't need it.
switch (position) {
case 0:
//Do something
break;
case 1:
//Do something
break;
case 2:
//Do something
break;
case 3:
//Do something
break;
case 4:
//Do something
break;
case 5:
//Do something
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
I am assuming here that you have already set an onClickListener to your buttons in onCreate.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
case R.id.btnup:
Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
selectedReport = spinner.getSelectedItem().toString();
break;
}
}

How to Pass an Int from non-activity class to Activity class

Android is similar to Java in terms of convention but why does my setter and getter methods won't work.
I have a spinner(which is a non-activity class) with code like this:
int finalposition;
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
for(int i=0; i<40; i++) {
if (i==pos){
setPosition(pos);
}
}
}
public void setPosition(int position){
finalposition= position;
}
public int getPosition(){
return finalposition;
}
and in onCreate(), here is my MainActivity(which extends Activity):
//spinner
spinner2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, paths);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener2());
//button
gPath = (Button) findViewById(R.id.getPath);
gPath.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CustomOnItemSelectedListener2 coisl2= new CustomOnItemSelectedListener2();
if(coisl2.getPosition()==1)
Toast.makeText(getApplicationContext(), "It Works!", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "position:"+coisl2.getPosition(), Toast.LENGTH_SHORT).show();
}
});
What I need to do is that if I select the item at position 1 in my spinner and clicked gPath button, a toast saying "It Works!" should be displayed. However, it does not work. I put a toast(displaying the position that was taken from calling getPosition()) on the button. When I selected the item at position 1 and click the button, it returned position:0. So that's why it won't enter the if-condition that I implemented and I'm wondering why. I do this in Java but why am I having troubles in Android. What is wrong/missing in my code?
Any help is appreciated
Try this.
You are creating a new instance of the listener on each onClick, and that listener would obviously not know which item was selected in the spinner, since it isn't effectively listening over anything.
//declare this as a class object
CustomOnItemSelectedListener2 coisl2= new CustomOnItemSelectedListener2();
//This follows in your onCreate
//spinner
spinner2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, paths);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(coisl2);
//button
gPath = (Button) findViewById(R.id.getPath);
gPath.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(coisl2.getPosition()==1)
Toast.makeText(getApplicationContext(), "It Works!", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "position:"+coisl2.getPosition(), Toast.LENGTH_SHORT).show();
}
});
You Can try this:
Under MainActivity.java
spinner2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, paths);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
coisl2= new CustomOnItemSelectedListener2(MainActivity.this,spinner2);
//button
gPath = (Button) findViewById(R.id.getPath);
gPath.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(coisl2.getPosition()==1)
Toast.makeText(getApplicationContext(), "It Works!", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "position:"+coisl2.getPosition(), Toast.LENGTH_SHORT).show();
}
});
Under CustomOnItemSelectedListener2.java( You can create this class like below)
public class CustomOnItemSelectedListener2
{
int finalposition;
public CustomOnItemSelectedListener2(Context context,Spinner spinner)
{
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
setPosition(pos);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
public void setPosition(int position){
finalposition= position;
}
public int getPosition(){
return finalposition;
}
}
Hope this is what you want...

Categories

Resources