I want to remove spinner specific item using other spinner , both spinner are get data from string-array string.xml , such as if i choose kg from one spinner so kg is remove from other spinner
Here is a code to update you spinners :
List<String> data = new ArrayList<>();
List<String> data2 = new ArrayList<>();
//populate you lists in onCreate
String tempRemovedItem ;
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, data);
ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line, data2);
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//when item is selected remove it from data2 and update the adapter of the second spinner
data2.remove(spinner1.getSelectedItem().toString());
if(tempRemovedItem != null){
data2.add(tempRemovedItem);
}
tempRemovedItem = spinner1.getSelectedItem().toString();
adapter2.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related
String[] categories = {
"All",
"Surgeon",
"Pediatrician",
"Cardiologist",
"Dermatologist"
};
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, categories);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
drspin.setAdapter(aa);
drspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView << ? > parent, View view, int position, long id) {
String a = drspin.getSelectedItem().toString();
sd.setText(a);
}
#Override
public void onNothingSelected(AdapterView << ? > parent) {
}
});
I have created a spinner having categories, and now I want to add the doctor's name in the subcategories, but I am not able to do it.
I'm trying to pass the value of a spinner into a different fragment so that it can be used in a different fragment to convert it to a different unit. I have the code here. I have an onitemselected method which takes the value of the spinner and displays it at the bottom of the application.
Any help would be appreciated.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
String[] values =
{"Select Units:", "Miles", "Kilometers", "Meters", "Yards",};
final Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
String[] values1=
{"Select Units:", "Miles", "Kilometers", "Meters", "Yards",};
Spinner spinner1 = (Spinner) view.findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values1);
adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(this);
button1 =view.findViewById(R.id.button_confirm);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence input = spinner.
}
});
return view;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),text,Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
How to transfer some data to another Fragment?
In your fragment that creates the data, store it in a bundle and send it to the next fragment via arguments, then in a method which takes a bundle (like onCreateView), use the value that is found in the bundle.
I have display multiple row in the RecyclerView and top of the one filter select the filter open the Spinner and select the Spinner item to open only selected list how to show
i m new in android programming
My class
private List<People> peolesListAll = new ArrayList<>();
private RecyclerView recyclerView;
private AlertAllCustomeAdapter alertAllCustomAdapter;
private DataBaseHelper db;
Spinner spinner;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alertlist_all);
BuildData();
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
spinnerData();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
alertAllCustomAdapter = new AlertAllCustomeAdapter(this, peolesListAll);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(alertAllCustomAdapter);
alertAllCustomAdapter.notifyItemRangeChanged(0, alertAllCustomAdapter.getItemCount());
private void spinnerData() {
db = new DataBaseHelper(getApplicationContext());
List<String> lables = db.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// lables.set(0, "ALL");
spinner.setAdapter(dataAdapter);
}
private List<People> BuildData() {
db = new DataBaseHelper(getApplicationContext());
try {
db.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
if (db.open()) {
peolesListAll = db.getAllPeople();
}
return peolesListAll;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
In the onItemSelected method you can retrieve the selected filter with position parameter from your spinner data (here the List lables). Then, re-populate your recylcer adadpter and call adapter.notifyDataSetChanged() to update the view like this :
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String filter = (String) spinner.getAdapter().getItem(position);
// update your peolesListAll according to the selected filter (with loop or anything else)
alertAllCustomAdapter.setData(peolesListAll) // write setData(List<People> data)
alertAllCustomAdapter.notifyDataSetChanged();
}
I have multiple spinners with a string array that has 3 items called "70","80" and "90". After the user selects these different spinner items I want to be able to sum up all of the items which the user has selected and display the total in a textview.For example a user selects "70" from one spinner and "70" from another, a textview will show the total 140. How can I do this?
public class FragmentCalc3 extends Fragment{
Context context;
TextView Results;
public static FragmentCalc3 newInstance()
{
FragmentCalc3 fragment = new FragmentCalc3();
return fragment;
}
public FragmentCalc3() {}
// Required empty public constructor
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_calculator3, container, false);
Results = (TextView) rootView.findViewById(R.id.Results);
context = container.getContext();
final Spinner spinner1 = (Spinner) rootView.findViewById(R.id.TestSpinner2);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(context, R.array.GradeTest, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinner1.setAdapter(adapter1); // Apply the adapter to the spinner
final Spinner spinner2 = (Spinner) rootView.findViewById(R.id.TestSpinner1);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(context, R.array.GradeTest, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Specify the layout to use when the list of choices appears
spinner2.setAdapter(adapter2); // Apply the adapter to the spinner
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner2Value = Integer.parseInt(spinner2.getSelectedItem().toString());
int spinner1Value = Integer.parseInt(spinner1.getSelectedItem().toString());
int Results = spinner2Value + spinner1Value;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return rootView;
}
}
If you have two spinners spinner2 and spinner1, And you want to get result when item of spinner2 is selected then simply do like this,
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int spinner2Value = Integer.parseInt(spinner2.getSelectedItem().toString()); //Add try catch block if necessary
int spinner1Value = Integer.parseInt(spinner1.getSelectedItem().toString());
int yourResult = spinner2Value + spinner1Value; //Result
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
//Do nothing
}
});
hi how is fetching data from database and setting it in spinner on android app.how can i develop dis.the below code is update data from spinner to android mysql database.but i wish to need fetch the data from database and that fetched value is display on my spinner value on android app.how can i develop this..
this is my android code:
public class InsertionExample extends Activity {
private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://192.168.1.168:8089/XcartLogin/services/update?wsdl";
private final String SOAP_ACTION = "http://xcart.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
String selectedItem;
static final String KEY_NAME = "orderid";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.change_status);
/* Intent in = getIntent();
// Get XML values from previous intent
String orderid = in.getStringExtra(KEY_NAME);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.textView1);
lblName.setText(orderid); */
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
btninsert = (Button)findViewById(R.id.btn_insert1);
btninsert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent in = getIntent();
String orderid = in.getStringExtra(KEY_NAME);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo unameProp =new PropertyInfo();
unameProp.setName("Status");//Define the variable name in the web service method
unameProp.setValue(selectedItem);//Define value for fname variable
unameProp.setType(String.class);//Define the type of the variable
request.addProperty(unameProp);
PropertyInfo idProp =new PropertyInfo();
idProp.setName("Orderid");//Define the variable name in the web service method
idProp.setValue(orderid);//Define value for fname variable
idProp.setType(String.class);//Define the type of the variable
request.addProperty(idProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
TextView result = (TextView) findViewById(R.id.textView2);
result.setText(response.toString());
}
catch(Exception e){
}
}
});
//attach the listener to the spinner
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
//Dynamically generate a spinner data
createSpinnerDropDown();
}
//Add animals into spinner dynamically
private void createSpinnerDropDown() {
//get reference to the spinner from the XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
//Array list of animals to display in the spinner
List<String> list = new ArrayList<String>();
list.add("Q");
list.add("P");
list.add("F");
list.add("I");
list.add("C");
//create an ArrayAdaptar from the String Array
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
//set the view for the Drop down list
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//set the ArrayAdapter to the spinner
spinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
//attach the listener to the spinner
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selectedItem = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
please help me..how can i develop this.
Have you tried using ArrayAdapter<?> on your spinner? I used this once.
private class ColorSpinner extends ArrayAdapter<Integer> {
public ColorSpinner(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return this.getView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Here can I set a custom layout or view of each item in the spinner.
TextView textView = new TextView(PincodeTextView.this.context);
String message = Integer.toHexString(this.getItem(position)).toUpperCase();
textView.setBackgroundColor(this.getItem(position));
textView.setText("0x" + message);
return textView;
}
}
Then I used this to load the spinner with my ColorSpinner:
final View colorbox = this.findViewById(R.id.edit_cell_dialog_color);
if (colorbox instanceof Spinner) {
ColorSpinner list = new ColorSpinner(context, android.R.layout.simple_spinner_item);
// Load from my database the need items in my case a list of color codes.
int[] colors = PincodeTextView.this.pincodeDB.getColors();
int indexOfColor = 0;
// Add each item into the Spinner.
for (int i = 0; i < colors.length; i++) {
list.add(colors[i]);
}
((Spinner) colorbox).setAdapter(list);
}