I am getting successfully previous activity value through intent. The get value store in TextView.
Only work else part of this code. 'If' part not working. I place this code in 'onCreate' method.
I want to do if text view contains "English" spinner should be run "English_array. if "Arabic" should be run "Arabic_array.
I want to work 'if' also depending on Textview's change.
public class TweetDetailActivity extends AppCompatActivity implements com.wdullaer.materialdatetimepicker.date.DatePickerDialog.OnDateSetListener,
DialogInterface.OnCancelListener{
Toolbar toolbar;
String[] subject;
String[] stages;
String[] via;
int position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tweet_detail);
TextView Title = (TextView) findViewById(R.id.tweetTitle);
if (TitleName.toString().equals("English")){
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.English_array,
android.R.layout.simple_spinner_item);
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setAdapter(staticAdapter);
}else if(TitleName.toString().equals("Arabic")){
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.Arabic_array,
android.R.layout.simple_spinner_item);
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setAdapter(staticAdapter);
}else {
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);
ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
.createFromResource(this, R.array.Tamil_array,
android.R.layout.simple_spinner_item);
staticAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
staticSpinner.setAdapter(staticAdapter);
}
// Retrieve data from MainActivity on listview item click
Intent i = getIntent();
// Get the listview item click subject
subject= i.getExtras().getInt("subject");
// Get the list of stages
stages = i.getStringArrayExtra("stages");
// Get the list of via
via = i.getStringArrayExtra("via");
// Locate the TextViews in singleitemview.xml
Title = (TextView) findViewById(R.id.tweetTitle);
Body = (TextView) findViewById(R.id.tweetBody);
Date = (TextView) findViewById(R.id.tweetDate);
// Load the text into the TextViews followed by the position
Title.setText(subject[position]);
Body.setText(stages[position]);
Date.setText(via[position]);
}
}
Title(Transfer the listview value to textview) :
public class MainActivity_Third extends android.support.v4.app.ListFragment {
ListView list;
TweetAdapter adapter;
String[] subject;
String[] stages;
String[] via;
int i = 0;
View v;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
//getActivity().setContentView(R.layout.activity_main_activity__third);
v = inflater.inflate(R.layout.activity_main_activity__third, container, false);
i++;
subject= new String[]{"Arabic","English","Tamil"};
stages = new String[]{"Certificate" ,"| Diploma ","| Higher Diploma"};
via= new String[]{"Tamil | Distance Learning, Direct", "Tamil | Distance Learning, Direct",
"Tamil | Distance Learning, Direct", "Tamil | Distance Learning, Direct"};
list = (ListView) v.findViewById(android.R.id.list);
adapter = new TweetAdapter(MainActivity_Third.this.getActivity(), rank, country, population);
list.setAdapter(adapter);
return v;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(MainActivity_Third.this.getActivity(), TweetDetailActivity.class);
// Pass all data subject
i.putExtra("subject", subject);
// Pass all data stages
i.putExtra("stages", stages);
// Pass all data via
i.putExtra("via", via);
// Pass listview item click position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
}
My res XML code below:
<string-array name="English_array">
<item>Certificate in English</item>
<item>Diploma in English</item>
<item>Higher Diploma in English</item>
</string-array>
<string-array name="Arabic_array">
<item>Diploma in Arabic</item>
<item>Higher Diploma in Arabix</item>
</string-array>
<string-array name="Tamil_array">
<item>Diploma in Tamil</item>
<item>Higher Diploma in Tamil</item>
</string-array>
here is spinner XML :
<Spinner
android:id="#+id/static_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp" />
Related
i have too spinners , spinner and spinner2, first one has a standard menu, the second must change relative to items from spinner.
am trying to get the position of item selected in spinner and then put it as a parametre into my method item2sp to change spinner's 2 items.
but i doesn't change, i keep getting only the first item menu in spinner2, how can i fix it help me please.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner2;
List<String> categories;
List<String> ndp;
List<String> ps;
ArrayAdapter<String> dataAdapter;
ArrayAdapter<String> ndpAdapter;
ArrayAdapter<String> psAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
// Spinner Drop down elements
categories = new ArrayList<String>();
categories.add("NDP");
categories.add("PS");
categories.add("PA");
categories.add("PC");
categories.add("PH");
ps = new ArrayList<String>();
ps.add("AA");
ps.add("BB");
ps.add("CC");
ndp = new ArrayList<String>();
ndp.add("RP");
ndp.add("AFF");
ndp.add("RPA");
// Creating adapter for spinner
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
psAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ps);
ndpAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ndp);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
psAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ndpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void itemsp2 (int position){
switch (position){
case 0:
spinner2.setAdapter(ndpAdapter);
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
break;
case 1:
spinner2.setAdapter(psAdapter);
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
break;
}
}
// int position = spinner.getSelectedItemPosition();
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
itemsp2(position);
// Showing selected spinner item
// Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
iller is my First List and I set this list my firs spinner then I implements onItemSelected method for this spinner. In this method you can add your second spinner with difrent List.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
List<String> iller = new ArrayList<String>();
iller.add("İl Seçiniz..");
iller.add("Antalya");
iller.add("Burdur");
iller.add("Bursa");
iller.add("Balıkesir");
iller.add("Giresun");
iller.add("İstanbul");
ArrayAdapter<String> illerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, iller);
illerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(illerAdapter);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setVisibility(View.INVISIBLE);
editText = (EditText) findViewById(R.id.editText);
editText.setVisibility(View.INVISIBLE);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int itemId = (int) id;
if (itemId == 1) {
iller.remove("İl Seçiniz..");
antalyaİlceler = new ArrayList<String>();
antalyaİlceler.add("İlçe Seçiniz..");
antalyaİlceler.add("Konyaaltı");
antalyaİlceler.add("Muratpaşa");
antalyaİlceler.add("Alanya");
antalyaİlceler.add("Manavgat");
ArrayAdapter<String> antalyaİlcelerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, antalyaİlceler);
antalyaİlcelerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(antalyaİlcelerAdapter);
spinner2.setVisibility(View.VISIBLE);
editText.setVisibility(View.VISIBLE);
} else if (itemId == 2) {
burdurİlceler = new ArrayList<String>();
burdurİlceler.add("İlçe Seçiniz..");
burdurİlceler.add("Bucak");
burdurİlceler.add("Tefenni");
burdurİlceler.add("Gölhisar");
burdurİlceler.add("Ağlasun");
ArrayAdapter<String> burdurİlcelerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, burdurİlceler);
burdurİlcelerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(burdurİlcelerAdapter);
spinner2.setVisibility(View.VISIBLE);
editText.setVisibility(View.VISIBLE);
}
i have on my Activity1 a ListView with 10 items. Based on which item i click, i want to pass just a part of a StringArray to my next Activity. I want to bind passed StringArray over an ArrayAdapter to a GridView.
First Problem:
I don´t understand how can i pass something in the next Activity, DEPENDING on the clicked item in the ListView of my Activity1
Second Problem:
How can i get just parts of my StringArray. My String Array has 200 items. Now i want to pass (depending on itemclick in Activity1) just the items i really need.
Here is my code
public class MainActivity extends Activity {
// ListView items
String[] provinces = new String[]{
"Prozentrechnung, Terme und Brüche",
"Gleichungen",
"Ungleichungen und Beträge",
"Geraden, Parabeln und Kreise",
"Trigonometrie",
"Potenzen, Wurzeln und Polynome",
"Exponentialfunktionen und Logarithmen",
"Trigonometrische Funktionen",
"Differenzialrechnung",
"Integralrechnung"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView provincelist = (ListView)findViewById(R.id.lvProvinceNames);
//add header to listview
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listheader, provincelist, false);
provincelist.addHeaderView(header, null, false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, provinces);
provincelist.setAdapter(adapter);
provincelist.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
//we use the items of the listview as title of the next activity
String province = provinces[position-1];
//we retrieve the description of the juices from an array defined in arrays.xml
String[] provincedescription = getResources().getStringArray(R.array.provincedescription);
//List<String> aufgabenListe = new ArrayList<String>(Arrays.asList(provincedescription));
//final String provincedesclabel = provincedescription[position-1];
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra("position",position);
intent.putExtra("province", province); //aktualisieren der Titel in der DetailActivity
intent.putExtra("provincedescription", provincedescription); //befüllen der GridView
startActivity(intent);
}
});
}
}
Here is the Activity2 where i have to bind my items to a GridView.
public class DetailActivity extends Activity {
String title;
String[] array;
int position;
//int image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detailactivity);
TextView tvTitleLabel = (TextView)findViewById(R.id.tvTitleLabel);
GridView gridView = (GridView) findViewById(R.id.gridView);
ArrayAdapter<String> adapter;
Bundle extras = getIntent().getExtras();
position = extras.getInt("position");
if (extras != null) {
title = extras.getString("province");
tvTitleLabel.setText(title);
/////Fehlermeldung: array = null --> NullPointerException
array = extras.getStringArray("provincedescription");
gridView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
}
});
//adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
//gridView.setAdapter(adapter);
}
}
}
UPDATE: Here is my string array
<string-array name="provincedescription">
<item>A1.1</item>
<item>A1.2</item>
<item>A1.3</item>
<item>A1.4</item>
<item>A1.5</item>
<item>A1.6</item>
<item>A1.7</item>
<item>A1.8</item>
<item>A1.9</item>
<item>A1.10</item>
<item>A1.11</item>
<item>A1.12</item>
<item>A1.13</item>
<item>A1.13</item>
<item>A1.14</item>
<item>A2.1</item>
<item>A2.2</item>
<item>A2.3</item>
<item>A2.4</item>
<item>A2.5</item>
<item>A2.6</item>
<item>A2.7</item>
<item>A2.8</item>
<item>A2.9</item>
<item>A2.10</item>
<item>A2.11</item>
<item>A2.12</item>
<item>A3.1</item>
<item>A3.2</item>
<item>A3.3</item>
<item>A3.4</item>
<item>A3.5</item>
<item>A3.6</item>
<item>A3.7</item>
<item>A3.8</item>
<item>A3.9</item>
<item>A3.10</item>
<item>A3.11</item>
<item>A3.12</item>
</string-array>
if I understand what you want maybe you should take a look at Singleton, I use and works great for now
https://gist.github.com/Akayh/5566992
Credits: https://stackoverflow.com/a/16518088/3714926
For resources like yours that are fixed its better to use string-array in your xml file. From Java I prefer to use static array in your case. Heres a sample:
public class Constants {
public static final String[] provinces = new String[] {
"Prozentrechnung, Terme und Brüche", "Gleichungen",
"Ungleichungen und Beträge", "Geraden, Parabeln und Kreise",
"Trigonometrie", "Potenzen, Wurzeln und Polynome",
"Exponentialfunktionen und Logarithmen",
"Trigonometrische Funktionen", "Differenzialrechnung",
"Integralrechnung" };
}
Then I can access the provinces from anywhere from my class like this:
String iWant = Constants.provinces[0];
Very Important Note
Static objects are dangerous in a number of scenarios and they are usually present in memory so use them sparingly.
As for the string array you cannot get a single element directly from the string-array defined in xml. For that you need to first get all the elements from the array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.provincedescription);
I have a spinner which displays both options, but when I accept the female option, it still takes the answer as a male, any suggestions?
List<String> SpinnerArray = new ArrayList<String>();
SpinnerArray.add("Male");
SpinnerArray.add("Female");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner Items = (Spinner) findViewById(R.id.genderSpinner);
Items.setAdapter(adapter);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
String selected = Items.getSelectedItem().toString();
if (selected.equals("Male")) {
inputGen = "Male";
}
if (selected.equals("Female")){
inputGen = "Female";
}
Following the official guide:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
You can try a switch of position and then try your same conditions.
Hey guys I have code shown bellow, which is supposed to get the item selected from the first spinner - Poets and then show another spinner depending on input poem 1,2 or 3. Once An item is selected from the second spinner it should then show a text view.
My problem is that sometimes it works, sometimes it chooses randomly which spinner to show it would seem and almost every single time when I select an item from spinner 2, the text view is almost never shown and when it is it's normally text view entitled tree.
can anyone help? I do not see any errors so I am confused
code:
public class Poets extends Activity implements OnItemSelectedListener{
//defining variables
Spinner PoetSpinner, PoemSpinner1, PoemSpinner2, PoemSpinner3;
String Poets[] = {"","Robert Burns", "James Beattie", "Duncan MacGregor Crerar"};
String Poems1[] ={"", "Tam O Shanter", "Auld Lang Syne"};
String Poems2[] ={"", "Judgement Of Paris", "The Minstrel"};
String Poems3[] ={"", "A Poem Commemorating Robbie Burns", "My Bonnie Rowan Tree"};
TextView Tam, Auld, Judge, TheMin, rob, tree ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set to fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
//set content view
setContentView(R.layout.poet);
initialize(); //call this method
}
public void initialize(){
//initializing items that are in the class
PoetSpinner = (Spinner) findViewById(R.id.spPoet);
PoemSpinner1 = (Spinner) findViewById(R.id.spPoem1);
PoemSpinner2 = (Spinner) findViewById(R.id.spPoem2);
PoemSpinner3 = (Spinner) findViewById(R.id.spPoem3);
Tam = (TextView) findViewById(R.id.tvSelectPoem1);
Auld = (TextView) findViewById(R.id.tvSelectPoem2);
Judge = (TextView) findViewById(R.id.tvSelectPoem3);
TheMin = (TextView) findViewById(R.id.tvSelectPoem4);
rob = (TextView) findViewById(R.id.tvSelectPoem5);
tree = (TextView) findViewById(R.id.tvSelectPoem6);
//setting up array adapters
//
//for each spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.poet_arrays, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.poem_arrays1, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.poem_arrays2, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this, R.array.poem_arrays3, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
PoetSpinner.setAdapter(adapter);
PoetSpinner.setOnItemSelectedListener(this);
PoemSpinner1.setAdapter(adapter2);
PoemSpinner1.setOnItemSelectedListener(this);
PoemSpinner2.setAdapter(adapter3);
PoemSpinner2.setOnItemSelectedListener(this);
PoemSpinner3.setAdapter(adapter4);
PoemSpinner3.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String val = Poets[pos];
String val2 = Poems1[pos];
String val3 = Poems2[pos];
String val4 = Poems3[pos];
if (val.equals("Robert Burns")) {
PoemSpinner2.setVisibility(Spinner.GONE);
PoemSpinner1.setVisibility(Spinner.VISIBLE);
PoemSpinner3.setVisibility(Spinner.GONE);
}
if (val.equals("James Beattie")) {
PoemSpinner2.setVisibility(Spinner.VISIBLE);
PoemSpinner1.setVisibility(Spinner.GONE);
PoemSpinner3.setVisibility(Spinner.GONE);
}
if (val.equals("Duncan MacGregor Crerar")) {
PoemSpinner2.setVisibility(Spinner.GONE);
PoemSpinner1.setVisibility(Spinner.GONE);
PoemSpinner3.setVisibility(Spinner.VISIBLE);
}
if (val2.equals("Tam O Shanter")) {
Tam.setVisibility(View.VISIBLE);
Auld.setVisibility(View.GONE);
Judge.setVisibility(View.GONE);
TheMin.setVisibility(View.GONE);
tree.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
if (val2.equals("Auld Lang Syne")) {
Auld.setVisibility(View.VISIBLE);
Judge.setVisibility(View.GONE);
TheMin.setVisibility(View.GONE);
tree.setVisibility(View.GONE);
Tam.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
if (val3.equals("Judgment Of Paris")) {
Judge.setVisibility(View.VISIBLE);
TheMin.setVisibility(View.GONE);
tree.setVisibility(View.GONE);
Tam.setVisibility(View.GONE);
Auld.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
if (val3.equals("The Minstrel")) {
TheMin.setVisibility(View.VISIBLE);
tree.setVisibility(View.GONE);
Tam.setVisibility(View.GONE);
Auld.setVisibility(View.GONE);
Judge.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
if (val4.equals("A Poem Commemorating Robbie Burns")) {
rob.setVisibility(View.VISIBLE);
tree.setVisibility(View.GONE);
Tam.setVisibility(View.GONE);
Auld.setVisibility(View.GONE);
Judge.setVisibility(View.GONE);
TheMin.setVisibility(View.GONE);
}
if (val4.equals("My Bonnie Rowan Tree"))
tree.setVisibility(View.VISIBLE);
Tam.setVisibility(View.GONE);
Auld.setVisibility(View.GONE);
Judge.setVisibility(View.GONE);
TheMin.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I have a feeling you have overcomplicated things a little. You can make what you want with 2 spinners and only one textview.
The first spinner will contain the poets, and based on the poet selected in this spinner you can modify the content of the second spinner in which you can have the poems of the selected poet. You can then show the selected poems name in the textview when the poem is selected in the second spinner. I would try with something like this:
public class Poets extends Activity implements
OnItemSelectedListener {
// defining variables
Spinner PoetSpinner, PoemSpinner;
String Poets[] = { "", "Robert Burns", "James Beattie",
"Duncan MacGregor Crerar" };
String Poems1[] = { "", "Tam O Shanter", "Auld Lang Syne" };
String Poems2[] = { "", "Judgement Of Paris", "The Minstrel" };
String Poems3[] = { "", "A Poem Commemorating Robbie Burns",
"My Bonnie Rowan Tree" };
TextView selectedPoem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set to fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set content view
setContentView(R.layout.poet);
initialize(); // call this method
}
public void initialize() {
// initializing items that are in the class
PoetSpinner = (Spinner) findViewById(R.id.spPoet);
PoemSpinner = (Spinner) findViewById(R.id.spPoem);
// Hide the second spinner
PoemSpinner.setVisibility(View.GONE);
selectedPoem = (TextView) findViewById(R.id.tvSelectPoem1);
// setting up array adapters
//
// for each spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.poet_arrays,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
PoetSpinner.setAdapter(adapter);
PoetSpinner.setOnItemSelectedListener(this);
PoemSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object poem = parent.getItemAtPosition(position);
selectedPoem.setText(poem.toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
ArrayAdapter<CharSequence> adapter = null;
switch (pos) {
case 1:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays1, android.R.layout.simple_spinner_item);
break;
case 2:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays2, android.R.layout.simple_spinner_item);
break;
case 3:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays3, android.R.layout.simple_spinner_item);
break;
default:
// Hide the poem spinner
PoemSpinner.setVisibility(View.GONE);
break;
}
if (adapter != null) {
// Set the poem spinners content and show the spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
PoemSpinner.setAdapter(adapter);
PoemSpinner.setVisibility(View.VISIBLE);
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
Of course you will have to delete the unused views from your xml.
I hope i was clear enough, and the code works as you wanted to.
I am trying to populate a a spinner but I am getting an error with my String array saying "Array constants can only be used in initializers". My code works fine when i employ the string array as a local variable, but as a global variable it doesn't. I really need to be able to use my string array as a global variable. Thank you in advance. Here is my code:
deleteselection = (Spinner)view.findViewById(R.id.deletespinner);
ArrayAdapter<String> adapterdeletetype;
//createdenominationsarray = getResources().getStringArray(R.array.createdenominations); //<--works
//String [] createdenominationsarray = {"Select Portfolio", "Two", "Three"}; //<--works
createdenominationsarray = {"Select Portfolio", "Two", "Three"};// <--doesn'twork
adapterdeletetype = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,createdenominationsarray){
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View v = null;
// If this is the initial dummy entry, make it hidden
if (position == 0) {
TextView tv = new TextView(getContext());
tv.setHeight(0);
tv.setVisibility(View.GONE);
v = tv;
}
else {
// Pass convertView as null to prevent reuse of special case views
v = super.getDropDownView(position, null, parent);
}
// Hide scroll bar because it appears sometimes unnecessarily, this does not prevent scrolling
parent.setVerticalScrollBarEnabled(false);
return v;
}
};
adapterdeletetype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
denominationselection.setAdapter(adapterdeletetype);
I did the same thing for one of my project and it works for me. Below is the code snippet for your reference..
ArrayList<String> languages = new ArrayList<String>();
languages.add("English");
languages.add("German");
languages.add("French");
ArrayAdapter<String> langAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,languages);
ListView lv =(ListView)findViewById(R.id.listmain);
lv.setAdapter(langAdapter);
lv.setOnItemClickListener(new listclklisten(MainActivity.this));
public class listclklisten implements OnItemClickListener{
private Context parent;
public listclklisten(Context p){
parent=p;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TO DO your code here
}
}
inside string.xml Write:
<string-array name="spinner_array_environtment">
<item>Test</item>
<item>Production</item>
</string-array>
Inside your MainActivity.java :
public class MainActivity extends Activity {
Spinner spinner_environment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner_environment = (Spinner) findViewById(R.id.spinnerview);
adapter =ArrayAdapter.createFromResource(this, R.array.spinner_array_environtment,R.layout.spinner_phone);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner_environment.setAdapter(adapter);
}
Inside spinner_phone.xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13dp"
android:textColor="#4C4646" />
try this out. Hope it will help you.