Drop down size of spinner - android

I am dynamically populating the spinner based on response from previous selected item.Though the size of list is more than 1 ,size of drop down is sometimes showing only one item(Slider is working). It is not happening everytime.Sometimes it is showing maximum size and sometimes not.
Maximum size of drop down is 400dp.
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/spinnerCollege"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
app:ms_dropdown_max_height="400dp"
app:ms_background_color="#color/offWhite"/>
new CityPresenter().getCities(new OnEntitiesReceivedListener<City>(this) {
#Override
public void onReceived(final List<City> cities) {
UpdateProfileActivity.this.cities = cities;
final List<String> cityNames = new ArrayList<>();
for(City city : cities){
cityNames.add(city.getName());
}
// Collections.sort(cityNames);
Log.d(TAG,cityNames.toString());
cityNames.add(0,"Select City");
citySpinner.setItems(cityNames);
}
}
Expected to show more than one item in drop down but showing only one item.

I don't know how you have set your adapter but this is generally how I get dynamic spinners working.
Dynamic List Spinner
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get reference of spinner from XML layout
Spinner spinner = findViewById(R.id.mySpinnerId);
//Using this button to trigger spinner list change
Button myButton=findViewById(R.id.myButtonId);
final List<String> myArrayList= new ArrayList<>();
myArrayList.add("Some String");
myArrayList.add("Some Other String");
//And....so on
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,myArrayList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//Triggering the spinner list change using a button
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myArrayList.add("New Item");
adapter.notifyDataSetChanged();
}
}
}
Hope this helps you to identify and resolve the issue.

Related

retrieve selected value from multiple spinners in a arraylist

I am developing an e-commerce android application using Firebase real-time database and i am using spinners to show my products category wise in multiple spinners. What i want is when user selects something from first spinner and press add button then that value comes in an array-list named current_products and then user selects something or not from rest of spinners and these values should keep coming to current_products(array-list). I have add button for every spinner so when user press add button for a specific spinner it should get the selected product from that spinner and add into current_products. I am trying to do the same thing the problem i am facing is when a user selects something from a spinner and presses add button the value gets added in current_products but when again user open that spinner and select a different product then this product is also added in current_products instead of removing the previous product from that spinner and adding the newly selected product.
Here is look of my database for further understanding
here is my code:
private Spinner scarfs_spinner, buttons_spinner, pins_spinner, laces_spinner;
private Button addScarfBtn, addButtonBtn, addPinsBtn, addLacesBtn;
private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
private ArrayList<String> scarfsList = new ArrayList<>();
private ArrayList<String> buttonsList = new ArrayList<>();
private ArrayList<String> lacesList = new ArrayList<>();
private ArrayList<String> pinsList = new ArrayList<>();
private ArrayList<Products> all_products = new ArrayList<>();
private ArrayList<String> current_products = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customization);
scarfs_spinner = (Spinner) findViewById(R.id.scarfs_spinner);
buttons_spinner = (Spinner) findViewById(R.id.buttons_spinner);
pins_spinner = (Spinner) findViewById(R.id.pins_spinner);
laces_spinner = (Spinner) findViewById(R.id.laces_spinner);
addScarfBtn = (Button) findViewById(R.id.addScarfBtn);
addButtonBtn = (Button) findViewById(R.id.addButtonsBtn);
addPinsBtn = (Button) findViewById(R.id.addPinsBtn);
addLacesBtn = (Button) findViewById(R.id.addLacesBtn);
showDataSpinner();
}
private void showDataSpinner()
{
databaseReference.child("Products").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
for(DataSnapshot item : dataSnapshot.getChildren()) {
if (item.child("category").getValue(String.class).equals("Scarfs"))
{
scarfsList.add(item.child("pname").getValue(String.class));
}
if (item.child("category").getValue(String.class).equals("Buttons"))
{
buttonsList.add(item.child("pname").getValue(String.class));
}
if (item.child("category").getValue(String.class).equals("Pins"))
{
pinsList.add(item.child("pname").getValue(String.class));
}
if (item.child("category").getValue(String.class).equals("Laces"))
{
lacesList.add(item.child("pname").getValue(String.class));
}
all_products.add(item.getValue(Products.class));
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(CustomizationActivity.this, R.layout.style_spinner, scarfsList);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(CustomizationActivity.this, R.layout.style_spinner, buttonsList);
ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<>(CustomizationActivity.this, R.layout.style_spinner, pinsList);
ArrayAdapter<String> arrayAdapter3 = new ArrayAdapter<>(CustomizationActivity.this, R.layout.style_spinner, lacesList);
scarfs_spinner.setAdapter(arrayAdapter);
buttons_spinner.setAdapter(arrayAdapter1);
pins_spinner.setAdapter(arrayAdapter2);
laces_spinner.setAdapter(arrayAdapter3);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
addScarfBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
current_products.add(scarfs_spinner.getSelectedItem().toString());
}
});
addButtonBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
current_products.add(buttons_spinner.getSelectedItem().toString());
}
});
addPinsBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
current_products.add(pins_spinner.getSelectedItem().toString());
}
});
addLacesBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
current_products.add(laces_spinner.getSelectedItem().toString());
}
});
}
Assign the item to a particular array position and before adding an item from the spinner, check if that position in the database is empty then populate it else clear the position content before adding (replacing) the new item.

Adding a Item to Listview

Im trying to simply add a Item to a Listview and display it. But if I add it it just shows me the path of that string istead of the tring itself. I'm sure it's a simple mistake but I cant find it.
public class zutatenHinzufuegen extends AppCompatActivity {
private ArrayList<String> zutatenliste;
private ListView zutatenView;
private ArrayAdapter<String> zutatenadapter;
private TextInputLayout zutatInput;
private TextInputLayout anzahlInput;
private int anzahlint;
private String einheitInput;
private Button hinzufuegen_button;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.zutaten_hinzufuegen_linear);
getSupportActionBar().setTitle(R.string.menu_zutaten);
// Spinner
final Spinner einheiten_spinner = (Spinner) findViewById(R.id.spinner_einheiten2);
ArrayAdapter<String> einheiten_adapter = new ArrayAdapter<String>(zutatenHinzufuegen.this,
android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.einheiten));
einheiten_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
einheiten_spinner.setAdapter(einheiten_adapter);
// List
zutatInput =(TextInputLayout) findViewById(R.id.textInputZutat2);
einheitInput =(String) einheiten_spinner.getSelectedItem().toString();
anzahlInput = (TextInputLayout) findViewById(R.id.textInputAnzahl2);
try {
anzahlint = Integer.parseInt(anzahlInput.toString());
}catch (NumberFormatException nfe){}
hinzufuegen_button = findViewById(R.id.zutat_hinzufuegen_button2);
zutatenView =(ListView) findViewById(R.id.zutatenliste2);
zutatenliste= new ArrayList<>();
zutatenadapter = new ArrayAdapter<String>(this,R.layout.zutatenliste_items,R.id.textitemliste,zutatenliste);
// zutatenadapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item, zutatenliste);
zutatenView.setAdapter(zutatenadapter);
hinzufuegen_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hinzufuegen(v , einheiten_spinner);
ZutatClass newzutat = new ZutatClass(zutatInput.toString(),anzahlint,einheitInput);
zutatenliste.add((String) newzutat.zutat_sting);
zutatenadapter.notifyDataSetChanged();
}
});
}
}
This is what I see when i press the button a few times
I'm new to Android Studio and java so I hope it's not to much of a mess.
Thanks in advance.
First of all, you need to add Edittext inside the TextInputLayout and then get a text like this ZutatClass newzutat = new ZutatClass(zutatInput.getEditText().getText().toString(),anzahlint,einheitInput); on your on click method
I guess the issue is that you are trying to convert a TextInputLayout to string instead of getting the text from it and convert it to string , try the code below
hinzufuegen_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hinzufuegen(v , einheiten_spinner);
ZutatClass newzutat = new ZutatClass(zutatInput.getEditText().getText().toString(),anzahlintgetEditText().g etText().toString()
,einheitInput);
zutatenliste.add((String) newzutat.zutat_sting);
zutatenadapter.notifyDataSetChanged();
}
});
zutatInput.toString() --> zutatInput.getEditText().getText()
anzahlint.toString() --> anzahlint.getEditText().getText().toString()

Android spinners dynamic data binding

I am new to android , I have an activity with 3 multiple spinners as bellow
Spinner 1:department
Spinner2:semester
Spinner3:subjects
and one button
spinner1 shows the department on select of department it has to display the semester on select of semester it has to display the subject spinner
NOTE:-SUBJECT SPINNER DEPENDS ON THE SELECTION OF SEMESTER SPINNER AS WELL AS DEPARTMENT SPINNER.
something like country, state, city spinners but suggest me some other ideas to do it because i am using string resources to list the items of spinners
Please help me
Try something like this in your activity.
Firstly, make your activity implement AdapterView.OnItemSelectedListener & View.OnClickListener.
private Spinner mDepartmentSpinner;
private Spinner mSemesterSpinner;
private Spinner mSubjectSpinner;
private Button mButton;
// Values to fill first spinner with.
private String[] mDepartments = {
"Department 1",
"Department 2",
"Department 3",
"Department 4",
};
private String[] mSemesters;
private String[] mSubjects;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
// Find the spinners and button, declared in the activity's resource file
mDepartmentSpinner = (Spinner) findViewById(R.id.department_spinner);
mSemesterSpinner = (Spinner) findViewById(R.id.semester_spinner);
mSubjectSpinner = (Spinner) findViewById(R.id.subject_spinner);
mButton = (Button) findViewById(R.id.button);
// Initialise spinner with values, placing them into a textview
mDepartmentSpinner.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.text_view, mDepartments));
// Attach a listener for item selection
departmentSpinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Work out which spinner was selected
switch (view.getId()) {
case R.id.department_spinner:
// Get the selected item
String selectedDepartment = mDepartments[position];
// Get values to fill the semester spinner with, based on initial selection.
mSemesters = getSemestersFor(selectedDepartment);
// Initialise spinner with values, placing them into a textview
mSemesterSpinner.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.text_view, mSemesters));
// Attach a listener for item selection
mSemesterSpinner.setOnItemSelectedListener(this);
break;
case R.id.semester_spinner:
// Get the selected item
String selectedSemester = mSemesters[position];
// Get values to fill the subject spinner with, based on second spinner selection.
mSubjects = getSubjectsFor(selectedSemester);
// Initialise spinner with values, placing them into a textview
mSubjectSpinner.setAdapter(new ArrayAdapter<String>(getContext(), R.layout.text_view, subjects));
// Attach a listener for item selection
mSubjectSpinner.setOnItemSelectedListener(this);
break;
case R.id.subject_spinner:
mButton.setOnClickListener(this);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Please select an option from each spinner.", Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onClick(View v) {
// Go to your new activity, passing the selected subject
Intent intent = new Intent(this, SubjectActivity.class);
intent.putExtra("KEY_SUBJECT", (String) mSubjectSpinner.getSelectedItem());
startActivity(intent);
}
You can get the selected subject in the SubjectActivity like so...
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent().getExtras().containsKey("KEY_SUBJECT") {
String subject = getIntent().getExtras().getString("KEY_SUBJECT");
}
}
Code for R.layout.text_view
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
You will need to implement the following methods yourself, I don't know your logic!
private String[] getSemestersFor(String department) {
// your code here
}
private String[] getSubjectsFor(String semester) {
// your code here
}

Android Add Listview item dynamically?

i want to add listview item dynamically. i had done this but when i restart the app listview item disappear. How can i save this item permanently to listview. please suggest
public class ChatWithAttorney extends Activity {
ImageView btnBack;
ListView chatList;
EditText etMsg;
Button btnSend;
String msg_to_send;
ArrayAdapter<String> adapter;
ArrayList<String> listMsg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chatwithattorny);
btnBack = (ImageView) findViewById(R.id.btnback);
chatList = (ListView) findViewById(R.id.chatList);
etMsg = (EditText) findViewById(R.id.etMsg);
btnSend = (Button) findViewById(R.id.btnSend);
listMsg = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, R.layout.chat_list_text, R.id.tvSentMsg,listMsg);
chatList.setAdapter(adapter);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
try {
msg_to_send = URLEncoder.encode(etMsg.getText().toString(),
"utf-8");
listMsg.add(msg_to_send);
adapter.notifyDataSetChanged();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
}
Put those ArrayList items in a database or SharedPreference and read those items when resuming your activity.
You can use Local database , SharedPreference or Static arraylist for storing the messages
AT the Time of Destroy Serialize the List data.
And Deserilize when Application Starts Again.

how to store spinner selected value and retrieve it in another activity for display

i've searched for a solution on google ,and on stack overflow.Yes i also know this could be a duplicate of another question asked in stack overflow but i tried the code snippets offered but none of them worked.Like: Taking user selection in a spinner, storing it in sharedPrefences, and using it in another activity and not getting integer value from SharedPreference and http://www.acnenomor.com/4379973p1/how-to-save-spinner-selecteditem-to-sharedpreferences
Here is my problem.I have the first activity,with two spinners from this tutorial http://www.mkyong.com/android/android-spinner-drop-down-list-example/ .On pressing the submit button,id like to save the selected values on SharesPreferences.On going to the second activity,i retrieve the selected values and display them on a text view.So that on the next app launch i go to the second activity to find the displayed saved values.I've been really stranded on this.Please point me in the right direction.Any help will be very much appreciated.
main activity
public class MyAndroidAppActivity extends Activity {
private Spinner spinner1, spinner2;
private Button btnSubmit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
}
// add items into spinner dynamically
public void addItemsOnSpinner2() {
spinner2 = (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.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
// get the selected dropdown list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyAndroidAppActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}
}
CustomOnItemSelectedListener.java
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I was finally able to do it. I used this tutorial (http://androidopentutorials.com/android-sharedpreferences-tutorial-and-example/)
I modified this code to fit what I wanted
public void onClick(View v) {
text = String.valueOf(spinner1.getSelectedItem()) + String.valueOf(spinner2.getSelectedItem());
sharedPreference.save(context, text);

Categories

Resources