how to create dynamic Spinners in android? - java

this code is working fine but its creating only one spinner dynamically i want to get value from database and want to run the loop till database values and generate dynamic Spinner inside the loop overhere i have mentioned the code of FOR LOOP but its not working and as well as i want to load different item in different spinner please give me idea how to do this?
public class DisciplineActivity extends Activity
{ ArrayList<String> selectDisciplineArrayList,disciplineInfoArrayList;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.discipline_view);
for(int i=0;i<20;i++)
{
disciplineInfoArrayList.add("select location item:"+i);
}
// for(int i=0;i<5;i++)
//{
Spinner disciplineInfoSpinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,disciplineInfoArrayList);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
disciplineInfoSpinner = (Spinner) findViewById(R.id.disciplineinfo_spinner);
disciplineInfoSpinner.setAdapter(spinnerArrayAdapter);
}//
}

You are creating a new Spinner and doing nothing with it.
You need to create an empty LinearLayout on your "discipline_view" layout, and then add your created Spinners on that LinearLayout, like this:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
for(int i=0;i<5;i++) {
Spinner disciplineInfoSpinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,disciplineInfoArrayList);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
disciplineInfoSpinner.setAdapter(spinnerArrayAdapter);
linearLayout.addView(disciplineInfoSpinner);
}

Related

activity only loads data for first ever pressed index after reloading app

I have created Activity with a listView which uses setOnItemClickListener to determine which index has been pressed. Then it should pass the data into the next activity based on the selected index.
Passing of the values works, but it only works for the first listView index pressed after the app is reloaded.
To make it more clear. When the app is loaded I can press on any index of the ListView which will pass the data to the next Activity and display it correctly, but then when I go back to the ListView and select different index it still displays the same data from the previous index without updating it. Therefore, I have to reload the app to get new index results displayed.
Here is my ViewListActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_artist);
artistList = (ListView) findViewById(R.id.artistList);
databasehandler = new SqliteHandler(ViewArtistActivity.this);
allArtists = databasehandler.getAllArtists();
artistName = new ArrayList<>();
singleArtistDetails = new ArrayList<>();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (allArtists.size() > 0) {
for (int i=0; i < allArtists.size(); i++) {
ArtistModel artistModel = allArtists.get(i);
artistName.add(artistModel.getArtistName());
}
}
adapter = new ArrayAdapter(ViewArtistActivity.this, android.R.layout.simple_list_item_1, artistName);
artistList.setAdapter(adapter);
artistList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ArtistModel artistModel = allArtists.get(position);
singleArtistDetails.add(artistModel.getArtistName());
singleArtistDetails.add(artistModel.getArtistDOB());
singleArtistDetails.add(artistModel.getArtistBiography());
Intent intent = new Intent(ViewArtistActivity.this, SavedArtistDetailsActivity.class);
intent.putExtra("NAME", singleArtistDetails.get(0));
intent.putExtra("DOB", singleArtistDetails.get(1));
intent.putExtra("BIOGRAPHY", singleArtistDetails.get(2));
startActivity(intent);
}
});
}
Here is my code for displaying selected index results Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saved_artist_details);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
artistSavedNameLbl2 = (TextView) findViewById(R.id.artistSavedNameLbl2);
artistSavedDOBLbl2 = (TextView) findViewById(R.id.artistSavedDOBLbl2);
artistSavedBiographyLbl2 = (TextView) findViewById(R.id.artistSavedBiographyLbl2);
btnDeleteDetails = (Button) findViewById(R.id.btnDeleteDetails);
updateUI();
}
private void updateUI() {
artistSavedNameLbl2.setText(getIntent().getStringExtra("NAME"));
artistSavedDOBLbl2.setText(getIntent().getStringExtra("DOB"));
artistSavedBiographyLbl2.setText(getIntent().getStringExtra("BIOGRAPHY"));
}
Do one thing , initialize [not saying declare] your array list singleArtistDetails inside the onClick method, before adding any values to it.
like
singleArtistDetails = new ArrayList();
Hope this will help.

constructor cannot resolve Array adapter in android

I don't know what error I did. But it shows me can not resolve constructor Arrayadapter on my array adapter code.Help me to fix it out.( I have used getActivity() because I have written this code on my fragment section) and it gets crashes after it.I have tried almost all issue fixing methods in stack overflow .(Before down voting suggest me some solutions )
spinner = (Spinner) view.findViewById(R.id.spinner3);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,getActivity(),android.R.layout.simple_spinner_item,def);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
here is my error log
Try this, if you use a fragment:
1. This code is designed to fetch a static array
ArrayAdapter myAdapter = ArrayAdapter.createFromResource(
getActivity(), R.array.my_array, android.R.layout.simple_spinner_item);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
2. If you are fetching a dynamic array, then the below sample code would work.
In this sample, I've fetched the list from db. You can change upon your requirement.
Spinner maritalSpinner = (Spinner) view.findViewById(R.id.marital_spinner);
List<MaritalStatus> maritalStatusList = new ArrayList<>();
List<String> maritalStatusArray = new ArrayList<>();
maritalStatusList = dbConnection.getMaritalStatus(maritalStatusDao);
maritalStatusArray.clear();
maritalIdArray.clear();
maritalStatusArray.add("~Select~");
maritalIdArray.add(0l); // this is number '0' and alphabet 'l'(small 'L') not number '1'
for (MaritalStatus marital : maritalStatusList) {
String maritalStatus = marital.getMaritalStat();
long maritalId = marital.getMaritalId();
maritalStatusArray.add(maritalStatus);
maritalIdArray.add(maritalId);
}
maritalAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, maritalStatusArray);
maritalAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
maritalAdapter.notifyDataSetChanged();
maritalSpinner.setAdapter(maritalAdapter);
your are passing to Context to your adapter so just pass one context
if your spinner in fragment than just change your adapter like this
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item,def);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

Dynamically Delete an element from listview - Android

I want to delete an element from the list when a button x is clicked on the layout of a list element.
Here is my java code, onNext() is working for adding an element to the list . but the onDelete isnt working.The textView4 element has numbers starting from 1 to n when new elements are added
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list2);
arrayList = new ArrayList<String>();
arrayList.add("1");
adapter2 = new ArrayAdapter<String>(this, R.layout.list2_layout, R.id.textView4, arrayList);
ListView myList2 = (ListView)
findViewById(R.id.listView2);
myList2.setAdapter(adapter2);
}
public void onNextItem(View v)
{
counter++;
String str=Integer.toString(counter);
arrayList.add(str);
adapter2.notifyDataSetChanged();
}
public void onDeleteItem(View v2)
{
arrayList.remove(R.id.textView4);
adapter2.notifyDataSetChanged();
}
you should not be passing the R.id.textView4 to remove the 4th textView
arrayList.remove(R.id.textView4);
You will need to do something like arrayList.remove(4); to delete the forth element from the list
on onNextItem(View v):
you are adding an integer(counter) converted to string to the list(arrayList.add(str);)
but on onDeleteItem(View v2):
you are trying to remove a view from the list (arrayList.remove(R.id.textView4);)
Your trying to remove Textview
arrayList.remove(R.id.textView4);
do it like this
arrayList.remove(clickeditempostion);
adapter2.notifyDataSetChanged();
You can do something like this.
arrayList.remove(CURRENT_INDEX_OF_ARRAYLIST);
adapter2.notifyDataSetChanged();

Using 1 spinner to dynamically change 2 spinners with if functions and notifyDataSetChanged?

Pretty new to Java in general. I have 3 Spinners in my code, and my 2 spinners would display lists depending on 1 main spinner ( which has 2 selections). After reading a few threads I read about refreshing the lists using notifySetDataChanged(); but the spinner lists never changed. Few questions :
Am I using the notifySetDataChanged correctly?
Is there another way to populate the lists?
Is the IF function the appropriate method?
Here's the code upto the onCreate method.
public class MainActivity extends Activity {
private Spinner spinner1, spinner2, spinner3, spinner4;
private Button convertButton;
private EditText from;
private List <String> list1 = new ArrayList<String>();
private List <String> list2 = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
from = (EditText) findViewById(R.id.amount);
//spinners for units
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner4 = (Spinner) findViewById(R.id.spinner4_main);
List<String>list4 = new ArrayList<String>();
list4.add("Distance");
list4.add("Weight");
//adapter for main scale
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter <String> (this,
android.R.layout.simple_spinner_dropdown_item, list4);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner4.setAdapter(dataAdapter4);
//adapter for "from" currency
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
//adapter for "to" currency
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
Object choice = spinner4.getSelectedItemPosition();
if (choice.equals("Weight")) {
//units to convert from
dataAdapter1.clear();
dataAdapter1.add("Milligrams");
dataAdapter1.add("Grams");
dataAdapter1.add("Kilograms");
dataAdapter1.add("Metric Ton");
//units to convert to
dataAdapter2.clear();
dataAdapter2.add("Milligrams");
dataAdapter2.add("Grams");
dataAdapter2.add("Kilograms");
dataAdapter2.add("Metric Ton");
}
else //(spinner4.getSelectedItem().toString().equals("Distance"))
{
//spinner1 = (Spinner) findViewById(R.id.spinner1);
dataAdapter1.clear();
dataAdapter1.add("Millimeter");
dataAdapter1.add("Centimeter");
dataAdapter1.add("Meter");
dataAdapter1.add("Kilometer");
dataAdapter2.clear();
dataAdapter2.add("Millimeter");
dataAdapter2.add("Centimeter");
dataAdapter2.add("Meter");
dataAdapter2.add("Kilometer");
}
dataAdapter1.notifyDataSetChanged();
dataAdapter2.notifyDataSetChanged();
}`
If anyone could explain what's wrong please enlighten this newbie. =)
U are approaching this wrong. Here is what you should do..
Define Spinner 1, 2 and 3.
Define arrayAdapters for spinner 1,2,3.
Populate ArrayAdapter and define this as the adapter for Spinner 1 (spinner1.setadapter(arrayadapter1)
Then call the spinner1.setOnItemSelectedListener. In the method onItemSelected, populate the logic to populate the array for spinner 2 and spinner 3 as per your requirement. Then call spinner2.setadapter(arrayadapter2) and spinner3.setadapter(arrayadapter3).
This should work.
So initially, spinner2 and spinner3 will not have any content. even if user clicks, there will be nothing in the dropdown. But once Spinner1 is selected by user, spinner2 and spinner3 will have dropdown values.
Please read up on setOnItemSelectedListener. Lots of tutorials are available. You can refer here to get a start.
If this works for you, please accept my answer.
Object choice = spinner4.getSelectedItemPosition();
This can't work.
getSelectedItemPosition()
returns an int (documentation) which won't equal your String as in
if (choice.equals("Weight")
Never ever (except in rare occasions) use the Object class as a reference, as any and every object in Java will be an Object (duh!) so you probably compare apples with oranges (which both are fruits). Seems like you're coming from a more weakly typed language, eh? ;)
Solution: Compare the positions:
if (spinnerX.getSelectedItemPosition == 0)...
Also, are you sure you want to change the content of spinner1 if something is selected on spinner4?

I need help with my first Spinner!! Not Displaying values

This is the first time I am trying to use a spinner and I need some help.. I made a layout with a spinner object on it and then I also made a array.xml with the numbers that I would like in the spinner in it. I run the following code and the screen displays without any values in my spinner??
public class SpinnerExaple extends Activity {
private Spinner numbersSpinner;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.numbersSpinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> numbersArray =
new ArrayAdapter<String>(this, R.layout.main,
getResources().
getStringArray(R.array.numbers));
}
}
You got the reference to the spinner, you got your AdapterArray set correctly- but you didn't attach the adapter to your spinner.
Add the line:
numbersSpinner.setAdapter( numbersArray );
You never set your numbers array into the spinner. Try adding the following at the end of the method:
numbersSpinner.setAdapter(numbersArray);
Also check out the hello-spinner tutorial.

Categories

Resources