String-array is not populating in spinner when viewing Design












0















I have added a string-array of "distance" in my string.xml with a list of different distances and applied it within a Spinner in my second activity. I've also incorporated the Spinner in the second activities java folder. The problem I'm having is the distances are not populating in my spinner in the design. I don't know if the coding in Java is incorrect and is the reason it is not being displayed.



This is the strings.xml:



<string name="distancetravel">Distance willing to travel?</string>
<string-array name="distance">
<item>Within 20 miles</item>
<item>Within 50 miles</item>
<item>Within 100 miles</item>
<item>Endless Miles</item>
</string-array>


This is the activity2.xml:



<RelativeLayout
<Spinner
android:id="@+id/distancelist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/distance"
android:layout_below="@+id/distancetravel"
android:layout_centerInParent="true" />
</RelativeLayout>


This is the Activity2.java:



import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class Activity2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);

spinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.distance,android.R.layout.simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity2, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}









share|improve this question





























    0















    I have added a string-array of "distance" in my string.xml with a list of different distances and applied it within a Spinner in my second activity. I've also incorporated the Spinner in the second activities java folder. The problem I'm having is the distances are not populating in my spinner in the design. I don't know if the coding in Java is incorrect and is the reason it is not being displayed.



    This is the strings.xml:



    <string name="distancetravel">Distance willing to travel?</string>
    <string-array name="distance">
    <item>Within 20 miles</item>
    <item>Within 50 miles</item>
    <item>Within 100 miles</item>
    <item>Endless Miles</item>
    </string-array>


    This is the activity2.xml:



    <RelativeLayout
    <Spinner
    android:id="@+id/distancelist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/distance"
    android:layout_below="@+id/distancetravel"
    android:layout_centerInParent="true" />
    </RelativeLayout>


    This is the Activity2.java:



    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;

    public class Activity2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity2);

    spinner= (Spinner) findViewById(R.id.spinner);
    ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.distance,android.R.layout.simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_activity2, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }

    return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
    }









    share|improve this question



























      0












      0








      0








      I have added a string-array of "distance" in my string.xml with a list of different distances and applied it within a Spinner in my second activity. I've also incorporated the Spinner in the second activities java folder. The problem I'm having is the distances are not populating in my spinner in the design. I don't know if the coding in Java is incorrect and is the reason it is not being displayed.



      This is the strings.xml:



      <string name="distancetravel">Distance willing to travel?</string>
      <string-array name="distance">
      <item>Within 20 miles</item>
      <item>Within 50 miles</item>
      <item>Within 100 miles</item>
      <item>Endless Miles</item>
      </string-array>


      This is the activity2.xml:



      <RelativeLayout
      <Spinner
      android:id="@+id/distancelist"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:entries="@array/distance"
      android:layout_below="@+id/distancetravel"
      android:layout_centerInParent="true" />
      </RelativeLayout>


      This is the Activity2.java:



      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.Menu;
      import android.view.MenuItem;
      import android.view.View;
      import android.widget.AdapterView;
      import android.widget.ArrayAdapter;
      import android.widget.Spinner;

      public class Activity2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

      Spinner spinner;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_activity2);

      spinner= (Spinner) findViewById(R.id.spinner);
      ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.distance,android.R.layout.simple_spinner_dropdown_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
      spinner.setOnItemSelectedListener(this);
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_activity2, menu);
      return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
      return true;
      }

      return super.onOptionsItemSelected(item);
      }

      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {

      }
      }









      share|improve this question
















      I have added a string-array of "distance" in my string.xml with a list of different distances and applied it within a Spinner in my second activity. I've also incorporated the Spinner in the second activities java folder. The problem I'm having is the distances are not populating in my spinner in the design. I don't know if the coding in Java is incorrect and is the reason it is not being displayed.



      This is the strings.xml:



      <string name="distancetravel">Distance willing to travel?</string>
      <string-array name="distance">
      <item>Within 20 miles</item>
      <item>Within 50 miles</item>
      <item>Within 100 miles</item>
      <item>Endless Miles</item>
      </string-array>


      This is the activity2.xml:



      <RelativeLayout
      <Spinner
      android:id="@+id/distancelist"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:entries="@array/distance"
      android:layout_below="@+id/distancetravel"
      android:layout_centerInParent="true" />
      </RelativeLayout>


      This is the Activity2.java:



      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.Menu;
      import android.view.MenuItem;
      import android.view.View;
      import android.widget.AdapterView;
      import android.widget.ArrayAdapter;
      import android.widget.Spinner;

      public class Activity2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

      Spinner spinner;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_activity2);

      spinner= (Spinner) findViewById(R.id.spinner);
      ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.distance,android.R.layout.simple_spinner_dropdown_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);
      spinner.setOnItemSelectedListener(this);
      }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_activity2, menu);
      return true;
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
      return true;
      }

      return super.onOptionsItemSelected(item);
      }

      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

      }

      @Override
      public void onNothingSelected(AdapterView<?> parent) {

      }
      }






      java android android-activity android-studio android-spinner






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 16:50









      Gary

      7,591133562




      7,591133562










      asked Oct 9 '15 at 18:43









      C.OC.O

      12316




      12316
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Most of the time if R is not found then the issue is with the project itself and something is not resolving correctly. Try to clean and rebuild the project and see if R can be resolved. If that doesn't work, try passing it your array declared as a string array like so



          String foo_array = mContext.getResources().getStringArray(R.array.foo_array);


          Hope this helps !






          share|improve this answer


























          • I have tried to clean the project and it doesn't help. I'm new to this, so wasn't sure where to put: String foo_array = mContext.getResources().getStringArray(R.array.foo_array); In the strings section or java and do I have to incorporate the distances I need in this string ?

            – C.O
            Oct 9 '15 at 21:27













          • Hmm next I would try maybe your import statements to make sure nothing is miss named. You would place that in your java code as String foo_array is declaring a new java string and mContext.getResources().getStringArray(R.array.foo_array); is pulling your array from your resources.

            – Doug Ray
            Oct 9 '15 at 22:04











          • Thank you! The red R. on java went away. Now the only issue I'm having is my Spinner is not displaying the stringed distances in the Design view. Any ideas why? It just shows the default spinner with "Item1 and Sub Item1".

            – C.O
            Oct 9 '15 at 23:19











          • woot ! thats great. Hmmm I am not sure about that one I actually never use spinners but if I had to guess I would say it has something to do with the XML. Try to follow the example on the dev docs here developer.android.com/guide/topics/ui/controls/spinner.html. and remember to mark as correct answer ~(0_0)~.

            – Doug Ray
            Oct 9 '15 at 23:41













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33045160%2fstring-array-is-not-populating-in-spinner-when-viewing-design%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Most of the time if R is not found then the issue is with the project itself and something is not resolving correctly. Try to clean and rebuild the project and see if R can be resolved. If that doesn't work, try passing it your array declared as a string array like so



          String foo_array = mContext.getResources().getStringArray(R.array.foo_array);


          Hope this helps !






          share|improve this answer


























          • I have tried to clean the project and it doesn't help. I'm new to this, so wasn't sure where to put: String foo_array = mContext.getResources().getStringArray(R.array.foo_array); In the strings section or java and do I have to incorporate the distances I need in this string ?

            – C.O
            Oct 9 '15 at 21:27













          • Hmm next I would try maybe your import statements to make sure nothing is miss named. You would place that in your java code as String foo_array is declaring a new java string and mContext.getResources().getStringArray(R.array.foo_array); is pulling your array from your resources.

            – Doug Ray
            Oct 9 '15 at 22:04











          • Thank you! The red R. on java went away. Now the only issue I'm having is my Spinner is not displaying the stringed distances in the Design view. Any ideas why? It just shows the default spinner with "Item1 and Sub Item1".

            – C.O
            Oct 9 '15 at 23:19











          • woot ! thats great. Hmmm I am not sure about that one I actually never use spinners but if I had to guess I would say it has something to do with the XML. Try to follow the example on the dev docs here developer.android.com/guide/topics/ui/controls/spinner.html. and remember to mark as correct answer ~(0_0)~.

            – Doug Ray
            Oct 9 '15 at 23:41


















          0














          Most of the time if R is not found then the issue is with the project itself and something is not resolving correctly. Try to clean and rebuild the project and see if R can be resolved. If that doesn't work, try passing it your array declared as a string array like so



          String foo_array = mContext.getResources().getStringArray(R.array.foo_array);


          Hope this helps !






          share|improve this answer


























          • I have tried to clean the project and it doesn't help. I'm new to this, so wasn't sure where to put: String foo_array = mContext.getResources().getStringArray(R.array.foo_array); In the strings section or java and do I have to incorporate the distances I need in this string ?

            – C.O
            Oct 9 '15 at 21:27













          • Hmm next I would try maybe your import statements to make sure nothing is miss named. You would place that in your java code as String foo_array is declaring a new java string and mContext.getResources().getStringArray(R.array.foo_array); is pulling your array from your resources.

            – Doug Ray
            Oct 9 '15 at 22:04











          • Thank you! The red R. on java went away. Now the only issue I'm having is my Spinner is not displaying the stringed distances in the Design view. Any ideas why? It just shows the default spinner with "Item1 and Sub Item1".

            – C.O
            Oct 9 '15 at 23:19











          • woot ! thats great. Hmmm I am not sure about that one I actually never use spinners but if I had to guess I would say it has something to do with the XML. Try to follow the example on the dev docs here developer.android.com/guide/topics/ui/controls/spinner.html. and remember to mark as correct answer ~(0_0)~.

            – Doug Ray
            Oct 9 '15 at 23:41
















          0












          0








          0







          Most of the time if R is not found then the issue is with the project itself and something is not resolving correctly. Try to clean and rebuild the project and see if R can be resolved. If that doesn't work, try passing it your array declared as a string array like so



          String foo_array = mContext.getResources().getStringArray(R.array.foo_array);


          Hope this helps !






          share|improve this answer















          Most of the time if R is not found then the issue is with the project itself and something is not resolving correctly. Try to clean and rebuild the project and see if R can be resolved. If that doesn't work, try passing it your array declared as a string array like so



          String foo_array = mContext.getResources().getStringArray(R.array.foo_array);


          Hope this helps !







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 16:51









          Gary

          7,591133562




          7,591133562










          answered Oct 9 '15 at 20:07









          Doug RayDoug Ray

          567420




          567420













          • I have tried to clean the project and it doesn't help. I'm new to this, so wasn't sure where to put: String foo_array = mContext.getResources().getStringArray(R.array.foo_array); In the strings section or java and do I have to incorporate the distances I need in this string ?

            – C.O
            Oct 9 '15 at 21:27













          • Hmm next I would try maybe your import statements to make sure nothing is miss named. You would place that in your java code as String foo_array is declaring a new java string and mContext.getResources().getStringArray(R.array.foo_array); is pulling your array from your resources.

            – Doug Ray
            Oct 9 '15 at 22:04











          • Thank you! The red R. on java went away. Now the only issue I'm having is my Spinner is not displaying the stringed distances in the Design view. Any ideas why? It just shows the default spinner with "Item1 and Sub Item1".

            – C.O
            Oct 9 '15 at 23:19











          • woot ! thats great. Hmmm I am not sure about that one I actually never use spinners but if I had to guess I would say it has something to do with the XML. Try to follow the example on the dev docs here developer.android.com/guide/topics/ui/controls/spinner.html. and remember to mark as correct answer ~(0_0)~.

            – Doug Ray
            Oct 9 '15 at 23:41





















          • I have tried to clean the project and it doesn't help. I'm new to this, so wasn't sure where to put: String foo_array = mContext.getResources().getStringArray(R.array.foo_array); In the strings section or java and do I have to incorporate the distances I need in this string ?

            – C.O
            Oct 9 '15 at 21:27













          • Hmm next I would try maybe your import statements to make sure nothing is miss named. You would place that in your java code as String foo_array is declaring a new java string and mContext.getResources().getStringArray(R.array.foo_array); is pulling your array from your resources.

            – Doug Ray
            Oct 9 '15 at 22:04











          • Thank you! The red R. on java went away. Now the only issue I'm having is my Spinner is not displaying the stringed distances in the Design view. Any ideas why? It just shows the default spinner with "Item1 and Sub Item1".

            – C.O
            Oct 9 '15 at 23:19











          • woot ! thats great. Hmmm I am not sure about that one I actually never use spinners but if I had to guess I would say it has something to do with the XML. Try to follow the example on the dev docs here developer.android.com/guide/topics/ui/controls/spinner.html. and remember to mark as correct answer ~(0_0)~.

            – Doug Ray
            Oct 9 '15 at 23:41



















          I have tried to clean the project and it doesn't help. I'm new to this, so wasn't sure where to put: String foo_array = mContext.getResources().getStringArray(R.array.foo_array); In the strings section or java and do I have to incorporate the distances I need in this string ?

          – C.O
          Oct 9 '15 at 21:27







          I have tried to clean the project and it doesn't help. I'm new to this, so wasn't sure where to put: String foo_array = mContext.getResources().getStringArray(R.array.foo_array); In the strings section or java and do I have to incorporate the distances I need in this string ?

          – C.O
          Oct 9 '15 at 21:27















          Hmm next I would try maybe your import statements to make sure nothing is miss named. You would place that in your java code as String foo_array is declaring a new java string and mContext.getResources().getStringArray(R.array.foo_array); is pulling your array from your resources.

          – Doug Ray
          Oct 9 '15 at 22:04





          Hmm next I would try maybe your import statements to make sure nothing is miss named. You would place that in your java code as String foo_array is declaring a new java string and mContext.getResources().getStringArray(R.array.foo_array); is pulling your array from your resources.

          – Doug Ray
          Oct 9 '15 at 22:04













          Thank you! The red R. on java went away. Now the only issue I'm having is my Spinner is not displaying the stringed distances in the Design view. Any ideas why? It just shows the default spinner with "Item1 and Sub Item1".

          – C.O
          Oct 9 '15 at 23:19





          Thank you! The red R. on java went away. Now the only issue I'm having is my Spinner is not displaying the stringed distances in the Design view. Any ideas why? It just shows the default spinner with "Item1 and Sub Item1".

          – C.O
          Oct 9 '15 at 23:19













          woot ! thats great. Hmmm I am not sure about that one I actually never use spinners but if I had to guess I would say it has something to do with the XML. Try to follow the example on the dev docs here developer.android.com/guide/topics/ui/controls/spinner.html. and remember to mark as correct answer ~(0_0)~.

          – Doug Ray
          Oct 9 '15 at 23:41







          woot ! thats great. Hmmm I am not sure about that one I actually never use spinners but if I had to guess I would say it has something to do with the XML. Try to follow the example on the dev docs here developer.android.com/guide/topics/ui/controls/spinner.html. and remember to mark as correct answer ~(0_0)~.

          – Doug Ray
          Oct 9 '15 at 23:41






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33045160%2fstring-array-is-not-populating-in-spinner-when-viewing-design%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft