How to repeat spinner dynamically? - android

I have RelativeLayout with ScrollView as the root element. In RecyclerView I have spinner and TextView. Now I am getting data from JSON like RollNumber and Marks. Now in TextView, I need to show RollNumber and in Spinner Marks should be displayed. I already got the data in ArrayList<Progress> and what should be the code will look like for this situation;
Do I have to repeat the RelativeLayout in for loop?
OnCreate
for (int i = 0; i < progress.size(); i++) {
progress_addmore_spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
loadspinner
}
private void loadSpinners()
{
if (progress != null || progress.size() > 0) {
ArrayList<String> progresscard = new ArrayList<>();
for (int i = 0; i < progress.size(); i++) {
progresscard.add(progress.get(i).toString());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
progresscard);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
progress_addmore_spinner.setAdapter(dataAdapter);
if (progress.get(0).getMarks() != null && !progress.get(0).getMarks().isEmpty()) {
product_addmore_spinner.setSelection(progresscard.indexOf(toppingtypelist.get(0).getMarks()));
}
}
}

You can set the spinner including the adapter, the default value in the onBindViewHolder of the recyclerview adapter. Also set the position as the tag of the spinner in the adapter like: vh.view.setTag(position);
Now implement the OnItemSelectedListener in the activity or the fragment and pass it onto the recyclerview adapter. Since you have already set the position of the spinner as the tag, you can easily extract the index of the progress object in the ArrayList.
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int index = (int) parent.getTag();
parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};

Related

view of spinner doesn't gets updated

I am using spinners which are dependent on one another. So when i choose one value in a spinner A the arraylist of other spinners B,C,D change accordingly.
when I select item at position 1 on spinner B and then choose any other value in spinner A The data on spinner B gets updated. So I again choose item at position 1 on spinner B the view doesn't changes while selecting item at any other position changes the view. So the same index doesn't get updated value in this piece of code.
ArrayAdapter<String> myAdapterEmployer = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,employerlist);
myAdapterEmployer.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
employer.setAdapter(myAdapterEmployer);
employer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
if(position > 0) {
employer1 = employer.getSelectedItem().toString();
employeridname = employdetail.get(employer1);
Sectorlist(employeridname);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This changes data of Sectorlist everytime a item is selected in employer.
And then any item is choosen in sectorlist.
ArrayAdapter<String> myAdaptersector = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,sectorlist);
myAdaptersector.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sector.setAdapter(myAdaptersector);
sector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
sector1=sector.getSelectedItem().toString();
String selectedsectortext = (String) parent.getItemAtPosition(position);
if(position > 0){
sectoridd=sectordetail.get(selectedsectortext);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Here when i choose item at same index as before it doesn't update in the spinner view.
Inside your onItemSelected of the spinner A you can set Adapter to other spinners to be update and this will work as you want.
employer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
if(position > 0) {
employer1 = employer.getSelectedItem().toString();
employeridname = employdetail.get(employer1);
Sectorlist(employeridname);
// Update other spinners here
myAdaptersector = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,sectorlist);
myAdaptersector.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sector.setAdapter(myAdaptersector);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Spinner And Listview

I am using spinner and listview concurrently, I have some logic that when i scroll on list i have have a data indication that tells which values to set on spinner, and i m using
spinner.setSelection(somePosition);
and when I click on Spinner it has also some data which indicate that to set the position of
listView.setSelection(somePosition);
Problem is that when i m scrolling on listView and in my adapter i need to change the postion of spinner selected item it calls the method
spinnerSurah.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int clickPosition, long l) {
int skipTotal = 0;
for(int i = 0 ; i < clickPosition ; i++)
{
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(i);
skipTotal+= surahObject.getInt("ayas");
}
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(clickPosition);
Log.e("spinnerSurah","spinnerSurah surahObject "+surahObject.toString());
positionSelection = skipTotal;
listView.setSelection(positionSelection);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Help me, I need to just change the postion of spinner without calling its listener.
You could set a condition in your listener that bypasses the selection logic if the ListView selection is called from the listener.
Declare a boolean in your class:
boolean skip_listener = false;
and change your listener to
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int clickPosition, long l) {
if(skip_listener){skip_listener = !skip_listener;return;}
int skipTotal = 0;
for(int i = 0 ; i < clickPosition ; i++)
{
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(i);
skipTotal+= surahObject.getInt("ayas");
}
SafeJSONObject surahObject = jsonArraySurahList.getJSONObject(clickPosition);
Log.e("spinnerSurah","spinnerSurah surahObject "+surahObject.toString());
positionSelection = skipTotal;
skip_listener = true;
listView.setSelection(positionSelection);
}
}

Spinner 2 data not populating properly

I am trying to load spinner2 data based on spinner1 item selection. My spinner1 loads without any issues. I have got two categories in spinner1. Before selecting any value on spinner1, my spinner2 is loaded with second categories values.
EDIT:
One thing i realized now. i have got 2 values in spinner1(category). when nothing is selected in spinner1 , spinner2 is loaded with item2's values. if i select item1 in spinner1, it loads properly. If i select item2 in spinner1 nothing is populated in spinner2. Because of my hint addition there is some issue i think.
minimal spinner2 part in MainActivity
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString();
getSpinner2(id);
}
private void getSpinner2(Long id) {
MyRestClient.getForSpinner2(MainActivity.this, "MyRestService/product/"+id,
headers.toArray(new Header[headers.size()]), null, new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
ArrayList<String> spinnerArray2 = new ArrayList<String>();
final SpinnerAdapter2 spinnerAdapter2 = new SpinnerAdapter2(MainActivity.this, spinnerArray2);
for (int i = 0; i < response.length(); i++) {
try {
JSONObject c = response.getJSONObject(i);
String productArray = c.getString("product");
spinnerAdapter2.add(productArray);
}
catch (JSONException e) {
e.printStackTrace();
}
}
spinnerAdapter2.add("Select One");
spinner2.setAdapter(spinnerAdapter2);
spinner2.setSelection(spinnerAdapter2.getCount());
spinnerAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
and i have got this to show hint on spinneradapter2 class
#Override
public int getCount() {
int count = super.getCount();
return count > 0 ? count - 1 : count;
}
.
.
public View getView(int position, View convertView, ViewGroup parent) {
.
.
if (position == getCount()) {
viewHolder.product.setText("");
viewHolder.product.setHint(products);
} else {
viewHolder.product.setText(products);
}
}
Please try spinnerAdapter2.notifyDataSetChanged(); after adding all elements to your adapter. Maybe it will help. And I thik you should add this items to spinnerArray2 and then create adapter using this object.
You need to know that onitemselected will be called first time without user actions and since you are adding a hint you should check that position is larger than 0 which is the hint position if there is no hint then skip the check of zero position
so your new selection will be like
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position>0){
parent.getItemAtPosition(position).toString();
getSpinner2(position);
}
}
also
spinnerAdapter2.add("Select One");
call this before adding other values to the adapter to make sure it is in first location
Edit:
Put spinner2.setOnItemSelectedListener(MainActivity.this); inside spinner1 setOnItemSelectedListner.
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString();
spinner2.setOnItemSelectedListener(MainActivity.this);
Log.e("Position", ""+position);
if(position > 0){
getSpinner2(position);
} else {
Toast.makeText(getApplicationContext(), "Spinner oth postion is selected", Toast.LENGTH_SHORT).show();
}
}

Android: Pass iteration value to onItemSelectedListener

Hi I am creating a list of spinners dynamically based on a user choice. Here I am also implementing OnItemSelectedListener for each of the spinners, since there are multiple spinners I want to know which spinner's method is currently being accessed. Here's the code,
for (int i = 0; i < count; i++) {
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object item = parent.getItemAtPosition(position);
Log.d("vij-debug", "selector1 no is " + item);
Log.d("vij-debug", "selector1 id is"+ view.getId());
//medicineArray1[i][1]=(String)item;
// here I want to access the iteration value i
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_dropdown_item,
spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
Can anyone suggest a suitable solution?
Set the Spinner's tag to i upon creation, then retrieve it from the AdapterView<?> parent parameter in onItemSelected().
Spinner spinner = new Spinner(this);
spinner.setTag(i);
...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
int spinnerNumber = parent.getTag();
}
...
}
);
you could use a List and store your spinner there. in your for loop
//inside for loop
List<Spinner> spinnerList=new ArrayList<Spinner>();
Spinner spinner = new Spinner(this);
spinnerlist.add(spinner);
//end
//outside for loop
for(int i=0;i<spinnerList.size();i++){
setListenerToSpinner(spinnerList.get(i));
}
//end
//function
public void setListenerToSpinner(Spinner spinner){
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object item = parent.getItemAtPosition(position);
Log.d("vij-debug", "selector1 no is " + item);
Log.d("vij-debug", "selector1 id is"+ view.getId());
//medicineArray1[i][1]=(String)item;
// here I want to access the iteration value i
}
}

setting itemListener of variable number of spinners

int numberofSpinner = TransportResult.Transfers.size();
Spinner spin=null;
for(int i=0;i<numberofSpinner;i++)
{
spin = new Spinner(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
spinLayout.addView(spin,p);
spin.setId(i);
Transfer transfer = TransportResult.Transfers.get(i);
ArrayList<CharSequence> s = new ArrayList<CharSequence>();
for( Line l : transfer.TransferLine)
{
s.add(l.ShortName+" - "+Helper.FindTransportTypeText(l.LineType));
}
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,s);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
}
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==0){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==1){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
If the number of spinner is more than 1, only the last spinner is triggered. For example; i have 3 spinner on screen, when i select the item of first or second spinner, the listener is never triggered. Only the third spinner triggers the listener. How can i solve that?
Thank you
UPDATE
when you use more than one spinner then setid for each spinner spin.setId(int) .and you can check id in OnItemSelected method. Mind that when you set OnitemSelected first time onItemSelected is called.
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==1){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==2){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Categories

Resources