I am creating spinners dynamically when i click add button.but i dont know how to get the values from created dynamic spinners.I need to get values how many shirt selected, saree selected and jeans selected when i click a submit button
this is my code
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
final List<String> dressArray = new ArrayList<String>();
dressArray.add("Select Dress");
dressArray.add("Shirt");
dressArray.add("Saree");
dressArray.add("Jeans");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, dressArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner dress = (Spinner) findViewById(R.id.dress);
dress.setAdapter(adapter);
final List<String> dresscountArray = new ArrayList<String>();
dresscountArray.add("Count");
dresscountArray.add("1");
dresscountArray.add("2");
dresscountArray.add("3");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, dresscountArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner dresscount = (Spinner) findViewById(R.id.dresscount);
dresscount.setAdapter(adapter1);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
Spinner dresscount = (Spinner)addView.findViewById(R.id.dresscount);
Spinner dress = (Spinner)addView.findViewById(R.id.dress);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
PickupOrder.this, android.R.layout.simple_spinner_item, dressArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dress.setAdapter(adapter);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
PickupOrder.this, android.R.layout.simple_spinner_item, dresscountArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dresscount.setAdapter(adapter1);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
Firstly define globally
String value1;
int value_position1;
And then call these methods inside onCreate after setting value in each Spinner
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
value_position1 = position;
value1 = parent.getSelectedItem().toString()
.trim();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
And when you call your submit button, use values of each String.
showallPrompt = "";
int Count = new_container.getChildCount();
try {
for (int cc = 0; cc < Count; cc++) {
View child = new_container.getChildAt(cc);
Spinner value = (Spinner)(child.findViewById(R.id.sp_dynamic));
String spin_value = value.getSelectedItem().toString();
showallPrompt += spin_value;
}
} catch (Exception e) {
}
Toast.makeText(MainActivity.this,
showallPrompt,
Toast.LENGTH_LONG).show();
}
}
Do like this on your button click...
Related
I'm trying to populate value of the next spinner from the previous spinner. In this case, what I did yet is generating all values inside the spinner means, no matter which value I select in the previous spinner it is always showing all values in the next spinner. I'm using five spinners that needs to populate values according to the previous one. Here I'm using a library for the spinner. The library is as follows.
'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'
Here I'm sharing my whole code/class for your consideration. There is no error and I get all values in respective spinner but I want to get values according to the previous one. here is my code.
import androidx.appcompat.app.AppCompatActivity;
import com.toptoche.searchablespinnerlibrary.SearchableSpinner;
public class Catchment extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public ArrayAdapter<String> regionAdapter;
public ArrayAdapter<String> districtAdapter;
public ArrayAdapter<String> upzAdapter;
public ArrayAdapter<String> unionAdapter;
public ArrayAdapter<String> villageAdapter;
public String[] Region = {"1", "2",
};
public String[] District = {"1","2","3","4","5","6","7","8",};
public String[] Upz = {"1","2"...."23";
public String[] Union = {"1","2"...."25"..."50"...."115"};
public String[] Village = { "1","2"...."25"..."50"...."150"...."500"...."947" };
Button catchment_code;
SearchableSpinner spinner_region, spinner_district, spinner_upz, spinner_union, spinner_village;
String[] region_code;
String[] district_code;
String[] upz_code;
String[] union_code;
String[] vill_code;
int positions;
String test;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catchment);
spinner_region = (SearchableSpinner) findViewById(R.id.region);
spinner_district = (SearchableSpinner) findViewById(R.id.district);
spinner_upz = (SearchableSpinner) findViewById(R.id.upz);
spinner_union = (SearchableSpinner) findViewById(R.id.union);
spinner_village = (SearchableSpinner) findViewById(R.id.village);
catchment_code = findViewById(R.id.catchment_code);
// Set spninner adapter
regionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Region);
districtAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, District);
upzAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Upz);
unionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Union);
villageAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Village);
//Set dropdown to the spinner
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
upzAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
unionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
villageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//assign to spinner
spinner_region.setAdapter(regionAdapter);
spinner_district.setAdapter(districtAdapter);
spinner_upz.setAdapter(upzAdapter);
spinner_union.setAdapter(unionAdapter);
spinner_village.setAdapter(villageAdapter);
spinner_region.setOnItemSelectedListener(this);
spinner_district.setOnItemSelectedListener(this);
spinner_upz.setOnItemSelectedListener(this);
spinner_union.setOnItemSelectedListener(this);
spinner_village.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.region) {
positions = spinner_region.getSelectedItemPosition();
region_code = this.getResources().getStringArray(R.array.region_code);
//Toast.makeText(this, region_code[positions], Toast.LENGTH_SHORT).show();
}
if (parent.getId() == R.id.district) {
positions = spinner_district.getSelectedItemPosition();
district_code = this.getResources().getStringArray(R.array.district_code);
//Toast.makeText(this, district_code[positions], Toast.LENGTH_SHORT).show();
}
if (parent.getId() == R.id.upz) {
positions = spinner_upz.getSelectedItemPosition();
upz_code = this.getResources().getStringArray(R.array.upz_code);
//Toast.makeText(this, upz_code[positions], Toast.LENGTH_SHORT).show();
}
if (parent.getId() == R.id.village) {
positions = spinner_village.getSelectedItemPosition();
vill_code = this.getResources().getStringArray(R.array.village_code);
//Toast.makeText(this, vill_code[positions], Toast.LENGTH_LONG).show();
}
if (parent.getId() == R.id.union) {
positions = spinner_union.getSelectedItemPosition();
union_code = this.getResources().getStringArray(R.array.union_code);
//Toast.makeText(this, union_code[positions], Toast.LENGTH_SHORT).show();
}
if (!(vill_code == null || region_code == null || district_code == null || upz_code == null ||
union_code == null)) {
test = region_code[positions] +
district_code[positions] +
upz_code[positions] +
union_code[positions] + vill_code[positions];
Toast.makeText(this, test, Toast.LENGTH_SHORT).show();
}
catchment_code.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Catchment.this, Participant_Details.class);
intent.putExtra("catment_code", test);
startActivity(intent);
}
});
//Toast.makeText(this, union_code[positions]+upz_code[positions], Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onBackPressed() {
Intent intent = new Intent(Catchment.this, SplashScreen.class);
Catchment.this.startActivity(intent);
Catchment.this.finish();}
}
So how can I get rid of this problem? I need help.
You are declaring all adapter at once in onCreate().
Instead declare just first adapter with values in onCreate() and declare other adapters with empty values.
#Override
protected void onCreate(Bundle savedInstanceState) {
--------;
// Set spninner adapter
regionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, Region);
districtAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]());
upzAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]());
unionAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]());
villageAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]());
------
spinner_region.setOnItemSelectedListener(this);
}
Now on itemSelectedListener, filter the desired values if needed and add the values to the next required adapter and notify the values along with adding listener to next spinner(adding listener is up to you, how you want to handle the things)
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.region) {
positions = spinner_region.getSelectedItemPosition();
String[] filteredDistrict = District.filter(Region[position]) // District.filter is not any predefined function, assuming some filter needed for selected Districts
districtAdapter.addValues(filteredDistrict);
districtAdapter.notifyDataSetChanged()
spinner_district.setOnItemSelectedListener(this);
}
}
Other than that filtering logic depends on your usecase.
my problem is the following. I have 2 Spinners in one activity. I want that both spinner items should be saved in the DB. But there is a slight difference between both spinners:
Spinner 1: Is populated by data from other db-table - when it is the only spinner in the mentioned activity, it works.
Spinner 2: Has default values. I am not sure if added it correctly to my activity. Anyway, after i added it to my saving the data is not working anymore.
Please help me what am I doing wrong??
My Activity Code:
public class activity_transport extends AppCompatActivity {
private BottomNavigationViewEx bottomNavigationViewEx;
DatabaseHelper myDb;
Button btn_save;
Spinner ChooseProject,ChooseTransport;
EditText Entfernung,Price,MWST;
String selectedspinner, selectedTransport;
ImageView imageView2;
private static int PICK_IMAGE = 100;
Uri imageUri;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
myDb = new DatabaseHelper(this);
ChooseProject = (Spinner) findViewById(R.id.ChooseProject);
ChooseTransport = (Spinner) findViewById(R.id.ChooseTransport);
Entfernung = (EditText) findViewById(R.id.Entfernung);
Price = (EditText) findViewById(R.id.Preis);
MWST = (EditText) findViewById(R.id.MwSt);
btn_save=(Button) findViewById(R.id.btn_save);
//bildanzeigebereich im layout
imageView2=(ImageView) findViewById(R.id.imageView2);
loadProjectSpinnerData();
//loadTransportSpinnerData();
SaveData();
//+++++++++++++++Function to load the spinner data from SQLite database++++++++++//
private void loadProjectSpinnerData() {
// database handler
DatabaseHelper db = new DatabaseHelper (getApplicationContext());
// Spinner Drop down elements
List<String> projects = db.getAllProjects();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseProject.setPrompt("Projekt auswählen");
// attaching data adapter to spinner
ChooseProject.setAdapter(dataAdapter);
//Choose Transport Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseProject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.ChooseProject)
{
selectedspinner = (String) ChooseProject.getSelectedItem();
}
else if(spinner.getId() == R.id.ChooseTransport)
{
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/*private void loadTransportSpinnerData() {
// Spinner method to read the on selected value
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Creating adapter for spinner
//ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
//++++++++++++Save Data++++++//
public void SaveData(){
btn_save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.createTransport(
selectedspinner,
selectedTransport,
Integer.valueOf(Price.getText().toString()),
Integer.valueOf(MWST.getText().toString()),
Integer.valueOf(Entfernung.getText().toString()),
imageViewToByte(imageView2)
);
if(isInserted=true)
Toast.makeText(activity_transport.this, "Daten gespeichert", Toast.LENGTH_LONG).show();
else
Toast.makeText(activity_transport.this, "Daten nicht gespeichert", Toast.LENGTH_LONG).show();
}
}
);
}
}
EDIT:
I tried to run the app by setting a few breakpoints.
When trying to run this line selectedTransport = (String) ChooseTransport.getSelectedItem(); the debugger posts Cannot find local variable 'ChooseTransport'
I solved the problem:
I implemented an own method for ChooseTransport- Spinner and now it works. I also figured out that the order how I add the data to the DB was not right. I changed this too.
See here my method for my second spinner:
private void loadTransportSpinnerData(){
ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.transport, android.R.layout.simple_spinner_dropdown_item);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
ChooseTransport.setAdapter(spinnerArrayAdapter);
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
I am working on EditText and Spinner . When I set text for EditText it were fine but when I try to set text for Spinner it give error.
this is code for set text EditText
New_Name.setText(NewName);
here New_Name is EditText
this is code for set text Spinner
New_Quantity.setText(NewQuantity);
here New_Quantity is Spinner
this is the full code.
public class UpdateFoodActivity extends Activity {
EditText Name_Search,New_Name,New_Calorie,New_Fat,New_Protein,New_Sugar,New_carbohydrates;
Spinner New_Quantity;
TextView title_text;
String SearchName,NewName,NewQuantity,NewCalorie,NewFat,NewProtein,NewSugar,Newcarbohydrates;
FoodDbHelper foodDbHelper;
SQLiteDatabase sqLiteDatabase;
Button UpdateButton;
ArrayAdapter<CharSequence> adapter;
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.update_food_activity);
Name_Search = (EditText)findViewById(R.id.name_search);
New_Name = (EditText)findViewById(R.id.new_name);
New_Quantity = (Spinner)findViewById(R.id.new_quantity);
adapter = ArrayAdapter.createFromResource(this, R.array.quant, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
New_Quantity.setAdapter(adapter);
New_Quantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
New_Calorie = (EditText)findViewById(R.id.new_calorie);
New_Fat = (EditText)findViewById(R.id.new_fat);
New_Protein = (EditText)findViewById(R.id.new_protein);
New_Sugar = (EditText)findViewById(R.id.new_sugar);
New_carbohydrates = (EditText)findViewById(R.id.new_vitamins);
UpdateButton = (Button)findViewById(R.id.update_button);
title_text = (TextView)findViewById(R.id.title_text);
New_Name.setVisibility(View.GONE);
New_Quantity.setVisibility(View.GONE);
New_Calorie.setVisibility(View.GONE);
New_Fat.setVisibility(View.GONE);
New_Protein.setVisibility(View.GONE);
New_Sugar.setVisibility(View.GONE);
New_carbohydrates.setVisibility(View.GONE);
UpdateButton.setVisibility(View.GONE);
title_text.setVisibility(View.GONE);
}
public void searchFood(View view){
SearchName = Name_Search.getText().toString();
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getReadableDatabase();
Cursor cursor = foodDbHelper.getFood(SearchName,sqLiteDatabase);
if (cursor.moveToFirst()) {
NewQuantity = cursor.getString(0);
NewCalorie = cursor.getString(1);
NewFat = cursor.getString(2);
NewProtein = cursor.getString(3);
NewSugar = cursor.getString(4);
Newcarbohydrates = cursor.getString(5);
NewName = SearchName;
New_Name.setText(NewName);
New_Quantity.setText(NewQuantity);
New_Calorie.setText(NewCalorie);
New_Fat.setText(NewFat);
New_Protein.setText(NewProtein);
New_Sugar.setText(NewSugar);
New_carbohydrates.setText(Newcarbohydrates);
New_Name.setVisibility(View.VISIBLE);
New_Quantity.setVisibility(View.VISIBLE);
New_Calorie.setVisibility(View.VISIBLE);
New_Fat.setVisibility(View.VISIBLE);
New_Protein.setVisibility(View.VISIBLE);
New_Sugar.setVisibility(View.VISIBLE);
New_carbohydrates.setVisibility(View.VISIBLE);
UpdateButton.setVisibility(View.VISIBLE);
title_text.setVisibility(View.VISIBLE);
}
else
{
Toast.makeText(getApplicationContext(),
"No Dish Found " ,
Toast.LENGTH_SHORT).show();
}
}
public void updatebutton(View view)
{
foodDbHelper = new FoodDbHelper(getApplicationContext());
sqLiteDatabase = foodDbHelper.getWritableDatabase();
String name,quantity,calorie,fat,protein,sugar,carbohydrates;
name = New_Name.getText().toString();
quantity = New_Quantity.getSelectedItem().toString();
calorie = New_Calorie.getText().toString();
fat = New_Fat.getText().toString();
protein = New_Protein.getText().toString();
sugar = New_Sugar.getText().toString();
carbohydrates = New_carbohydrates.getText().toString();
int count = foodDbHelper.updateInformation(SearchName,name,quantity,calorie,fat,protein,sugar,carbohydrates,sqLiteDatabase);
Toast.makeText(getApplicationContext(),count+" dish updated",Toast.LENGTH_LONG).show();
Intent intent = new Intent(this,UpdateFoodActivity.class);
startActivity(intent);
}
}
In order to add entries to the spinner you need to define an Adapter i give you an example:
spinner = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.spinner_item, list);
spinner.setAdapter(dataAdapter);
Spinner likes ListView, need an adapter for rendering data. Adapter, in simple term, is a collection of items. (As you see ListView always contains list of item and also Spinner). Nevertheless, Adapter of spinner often a list of string. (because spinner just represents user choice)
In your example, You can simply create ArrayAdapter and assign again to Spinner.
private void setTextForSpinner(Spinner spinner) {
String[] items = {"Stack", "Over", "Flow"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
}
If apply above code to your example, you can change again to:
private void setTextForSpinner(Spinner spinner, String NewName) {
String[] items = {NewName};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
spinner.setAdapter(adapter);
}
Hope this help :)
I used two spinners, country and state which i get from web-service.
i get Country in first web-service then i should pass selected country from spinner to the second web-service, How can i call the dependent Spinner in Web-service ?
Give me the example,
Thanks.
First Spinner
int i = 0;
String[] stringArray = new String[List.length];
for (i = 0; i < List.length; i++) {
stringArray[i] = Arrays.asList(List[i].Country);
}
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stringArray);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I had the same query some days back I guess this will solve your problem.
Add this after your spinner1 code
// here onitemselected call your second method
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
statewebservice((String) parent.getItemAtPosition(pos));
}
And for state spinner in your second web service
int i = 0;
String[] stringArray2 = new String[List2.length];
for (i = 0; i < personList1.length; i++) {
stringArray2[i] = Arrays.asList(List2[i].State);
}
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, stringArray2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent,
View view, final int pos1, long id) {
//************Code after selecting second spinner ***********
}
});
}
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.