Passing a spinner selected value to another value - android

Once a value is selected from the spinner, I am looking to have another list of values populate the spinner from the selection made. For example: When the user clicks the spinner the values "Home Team, Home Subs, Home Other" comes up. The user clicks one of these and then the players that are affiliated with that selection then populate the spinner. Below is code for the original spinners populated.
public class ExampleMain extends Activity {
JSONArray jsonArray = null;
JSONArray str_login = null;
public String kode;
public String Team_Name;
public String Home_team;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
JSONParser jParser = new JSONParser();
String link_url = "http://10.0.2.2/NPD/P_name.php";
JSONObject json = jParser.FunctionParser(link_url);
Spinner d11 = (Spinner)findViewById(R.id.doubles11);
Spinner d12 = (Spinner)findViewById(R.id.doubles12);
Spinner d21 = (Spinner)findViewById(R.id.doubles21);
Spinner d22 = (Spinner)findViewById(R.id.doubles22);
Spinner d31 = (Spinner)findViewById(R.id.doubles31);
Spinner d32 = (Spinner)findViewById(R.id.doubles32);
try {
jsonArray = json.getJSONArray("team");
final String[] items = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
items[i]=jsonObject.getString("P_name");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d11.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d12.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d21.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d22.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d31.setAdapter(adapter);
adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
d32.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
Thanks.

You need to use OnItemSelectedListener
d11.setOnItemSelectedListener(this);
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{
item = spinner.getItemAtPosition(position).toString();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
Now you will have your value and you can use that wherever you need to populate the other Spinners. Make item in this example a member variable so you can use it anywhere in this Activity. You will also need to add implements OnItemSelectedListener to your class definition
public class ExampleMain extends Activity implements OnItemSelectedListener{
Also, you are initializing adapter many times with the same values it looks like. Just initialize it once then set your Adapters on your Spinners

Related

android Spinner set Text give error in setText

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 :)

Get values from dynamic spinners

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...

Null Pointer exception error with spinners int array

So when I try to set dropdown3 as adapter1, I get a null pointer exception. However I do not get any issues with the String arrays. Why is this happening with the Integer array? What can I do to fix it??
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner dropdown1 = (Spinner)findViewById(R.id.spinnertext1);
Spinner dropdown2 = (Spinner)findViewById(R.id.spinnertext2);
Spinner dropdown3 = (Spinner)findViewById(R.id.spinnernumber1);
String[] items = new String[]{"ml", "oz", "L"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
Integer[] numbers = new Integer[]{1,2,3,4,5,6,7,8,9,10};
ArrayAdapter<Integer> adapter1 = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, numbers);
//adapts the string "items" to be an adapter and fit into the spinner.
dropdown1.setAdapter(adapter);
dropdown2.setAdapter(adapter);
dropdown3.setAdapter(adapter1);
I don't think you can use the same adapter object for two different views.
Try instantiating three different adapters.

sqlite Viewlist error

i trying tu put some elements from database to a List view,
my problem is that when i starrt my activity, I get this:
**com.example.restaurant.Restaurant#2be2d1d0
com.example.restaurant.Restaurant#2be3d3a8**
instead of database objects.
public class Liste extends Activity{
private ListView listview;
private Button boutonAjouter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_restaurant);
listview = (ListView)findViewById(R.id.listView1);
Restaurant mcdo = new Restaurant("mcdo","brossard","take-out","fastfood","450-555-5555");
Restaurant burger = new Restaurant("burger","longueuil","take-out","fastfood","450-999-9999");
RestaurantBDD restaurantBDD = new RestaurantBDD(this);
restaurantBDD.openForWrite();
restaurantBDD.insertRestaurant(mcdo);
restaurantBDD.insertRestaurant(burger);
ArrayList <Restaurant> restaurantlist = restaurantBDD.getAllRestaurants();
restaurantBDD.close();
ArrayAdapter <Restaurant> adapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, restaurantlist);
listview.setAdapter(adapter);
boutonAjouter = (Button) findViewById(R.id.btn_nouveau);
boutonAjouter.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Liste.this, Formulaire.class);
startActivity(intent);
}
});
}
}
If you want to show a list with the names of the restaurants, supposing that your Restaurant class contains a string attribute "name", you should first create an array containing the names of the restaurants and then use an ArrayAdapter <String>:
String[] values = new String[restaurantlist.size()];
for(int i=0;i<restaurantlist.size();i++) {
values[i] = restaurantlist.get(i).getName();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listview.setAdapter(adapter);

Change the adapter of a spinner on runtime using onItemSelectedListener

This is my code
[...]
final String[] top_spinner_items = new String[35];
for (int i = 0; i < top_spinner_items.length; i++) {
top_spinner_items[i] = Integer.toString(i+1);
}
ArrayAdapter<String> spn_topItemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, top_spinner_items);
spn_topItemsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
[...]
AdapterView.OnItemSelectedListener lst_bottomSpinner_changed_element = new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
//
final String[] top_spinner_items = new String[35];
for (int i = 0; i < top_spinner_items.length; i++) {
top_spinner_items[i] = Integer.toString(i+1);
}
ArrayAdapter<String> spn_topItemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, top_spinner_items);
spn_topItemsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
};
(all the missing references are in the code are between or before the code)
My question is why inside AdapterView.OnItemSelectedListener
ArrayAdapter<String> spn_topItemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, top_spinner_items);
gives error. It says "the constructor is undefined". I tryed using android.widget.ArrayAdapter, but got none.
I'm trying to change the adapter of a spinner when a previous spinner is changed(i want the second spinner to have the value setted in the previous one)
Do you have any idea?
You got a compile error because you pass an instance of AdapterView.OnItemSelectedListener to the constructor of the adapter instead of context. Modify your code in onItemSelected, adding classname of your Activity before this:
ArrayAdapter<String> spn_topItemsAdapter = new ArrayAdapter<String>(YourActivity.this,
android.R.layout.simple_spinner_item, top_spinner_items);
And, you missed setting the adapter to your ListView in inItemSelected, like parentView.setAdapter(spn_topItemsAdapter);.
However, I think, changing whole adapter in onItemSelected isn't a good idea. You'd better modify the existing one.

Categories

Resources