I have a simple layout (two TextViews and one Spinner), the array and the MainActivity.java which in the onCreate method, through an adapter populates the Spinner.
When I execute the app I can click on the Spinner but nothing happens in android 4.3(API level 16) and a empty, white row is displayed in android2.3.3(Api level 10).
Shouldn't I at least see and select one of the strings?
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/question" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/question"
android:text="#string/one" />
<Spinner
android:id="#+id/nationalities_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/question"
android:layout_toRightOf="#+id/textView1"
android:entries="#array/nationalities_array"
android:tag="#string/select_string" />
</RelativeLayout>
The array for the Spinner:
<string-array name="nationalities_array">
<item>Italian</item>
<item>German</item>
<item>British</item>
<item>Dutch</item>
</string-array>
and the MainActivity.java:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner nationalities_spinner = (Spinner) findViewById(R.id.nationalities_spinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, R.array.nationalities_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
nationalities_spinner.setAdapter(adapter);
}
public void onResume(){
super.onResume();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You don't see something in your Spinner because you use an incorrect constructor for the ArrayAdapter which results in an empty ArrayAdapter for your Spinner. You use this ArrayAdapter constructor and provide something different than what it wants(a layout file resource and an id of a TextView from that layout file). Instead you probably want this :
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.nationalities_array, android.R.layout.simple_spinner_item);
Related
Just to clarify first, I'm a newbie with Java and Android, and I'm still in a heavy learning process.
Anyway...
I'm working on a simple Parking SMS app, and I'm stuck with Spinner and a TextView.
Basically, I'm trying to do this: when I select a Parking zone from a Spinner (it has five parking zones), I want to populate a Parking zone description to a TextView below Spinner in the Layout.
Spinner has its own StringArray, while Parking zone descriptions are each in its own Strings.
So it looks like this:
activity_glavni.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Glavni">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/txGrad"
android:id="#+id/txOdaberiteGrad"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnergradovi"
android:layout_below="#+id/txOdaberiteGrad"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:id="#+id/space" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txZona"
android:text="#string/txZona"
android:layout_below="#+id/spinnergradovi"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerZona"
android:layout_below="#+id/txZona"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/txPrikazZone"
android:layout_below="#+id/spinnerZona" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editTextRegistracija"
android:layout_below="#+id/txPrikazZone"
android:layout_alignParentStart="true"
android:hint="Unesite broj registracije" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/btn_posalji"
android:id="#+id/btn_posalji"
android:layout_below="#+id/editTextRegistracija"
android:layout_alignParentStart="true"
android:layout_marginTop="33dp"
android:layout_alignParentEnd="false"
/>
strings.xml
<resources>
<string name="app_name">ParkingZagreb</string>
<string name="action_settings">Settings</string>
<string name="txGrad">Odaberite grad</string>
<string name="txZona">Odaberite zonu</string>
<string name="btn_posalji">Pošalji SMS!</string>
<string-array name="gradovi">
<item>Zagreb</item>
<item>Velika Gorica</item>
<item>Zaprešić</item>
</string-array>
<string-array name="zone">
<item>Prva zona</item>
<item>Druga zona</item>
<item>Treća zona</item>
<item>Četvrta zona (1)</item>
<item>Četvrta zona (2)</item>
</string-array>
<string-array name="opisi_zona">
<item>6 kn/h, maksimalno 2 h</item>
<item>3 kn/h, maksimalno 3 h</item>
<item>1,5 kn/h, bez ograničenja</item>
<item>5 kn/dan, 7-16 h</item>
<item>10 kn/dan, 7-20 h</item>
</string-array>
Glavni.java
public class Glavni extends ActionBarActivity {
public TextView txPrikazZone;
private Button btn_posalji;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavni);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
btn_posalji = (Button) findViewById(R.id.btn_posalji);
btn_posalji.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(Glavni.this).create();
alertDialog.setTitle("SMS poslan!");
alertDialog.setMessage("Provjerite SMS aplikaciju. Poruka bi trebala doći za nekoliko trenutaka.");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
// the first city selection Spinner
Spinner spinner_gradovi = (Spinner) findViewById(R.id.spinnergradovi);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.gradovi, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_gradovi.setAdapter(adapter);
// the second Spinner with parking zones
final Spinner spinner_zona = (Spinner) findViewById(R.id.spinnerZona);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.zone, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner_zona.setAdapter(adapter2);
// this is where I'm trying to populate the TextView with the String-Array by selecting an item from the second Spinner
final String[] opisi_zona = getResources().getStringArray(R.array.opisi_zona);
spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
txPrikazZone.setText(opisi_zona[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#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_glavni, 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);
}
}
Thanks for any help...
The reason for the null pointer is that you have not initialized txPrikazZone and you are trying to call setText.
You need to initialize it in your onCreate method like this:
txPrikazZone = (TextView) findViewById(R.id.txPrikazZone);
I hope this helps.
Your adapter it´s the same for both Spinners
EDIT
First try wrapping your ZonaX strings into a string-array
<string-array name="zonas">
...
</string-array>
And now in the code change the set of the ItemSelectedListener
final String[] zonas = getResources().getStringArray(R.array.zonas);
spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
txPrikazZone.setText(zonas[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Ok so I'm running into problems creating an app (force closing) and I think it has to do with the way I implemented the layout. So a few questions: First, I have a relative layout that includes a text input with a button next to it, a list view (still within the relative layout) and another button below that. This is my main xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/edit_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Button1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/Button1"
android:hint="#string/edit_choice" />
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="addString"
android:text="#string/button_add" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="#string/button_random" />
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Button2"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Button1"
tools:listitem="#android:layout/simple_list_item_1" >
</ListView>
</RelativeLayout>
Before I even changed anything in the .java files when I tried running this, only the text input and the 2 buttons appeared and the theme changed from Holo to Holo light. So I'm wondering if this works, I've only seen examples where the list view matches the parent layout completely.
My second question is how do I handle using the input to add values to the list view, can I do that in the main activity class or can I have another class to handle the list view and still reference the main layout.
This is my Main class:
public class MainActivity extends Activity
{
public ArrayList <String> choices = new ArrayList <String>();
public ListView listView = (ListView) findViewById(R.id.listView1);
public String [] choicesArray;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
choicesArray = new String [] {"You have not entered a choice yet"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, choicesArray);
listView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//adds the string to the list
public void addString(View view)
{
choicesArray = (String[]) choices.toArray();
EditText editText = (EditText) findViewById(R.id.edit_choice);
String message = editText.getText().toString();
choices.add(message);
}
}
Hopefully this makes sense and thank you for any help.
Problem is you are calling this line
public ListView listView = (ListView) findViewById(R.id.listView1);
before setting content view by setContentView() method in onCreate() so you are getting NullPointerException. Do not forget that before calling findViewById you have to set content view. So delete above line because you are creating function scope listView in onCreate method and your NullPointerException problem will be solved. And also change first line of addString method like this
choicesArray = choices.toArray(choicesArray);
Define your ArrayAdapter as member for your class (not as variable
in onCreate()).
Set the adapter to your listView in onCreate().
Set the onClickListener for your button.
Add the text from EditText to your adapter when you click on the
button.
Profit!
I need to output data from lines cm1 and cm2 in a ListView, every time when I press the button. Line should be shown in various TextView.
Code TEST.java:
public String cm1;
public String cm2;
public class TEST extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyList = (ListView) this.findViewById(R.id.listStrings);
setListAdapter();
// button
Button setup = (Button) this.findViewById(R.id.send);
setup.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
cm1 = "cm1text";
cm2 = "cm2text";
//xxxx
}
});
}
// adapter
private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row //idknow what im must writehere to set cm1 string to cm1text tv
List.setAdapter(adapter);
}
Code TEST.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listStrings"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
Code row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/cm1tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/cm2tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Should look ListView after 2 clicks:
cm1
cm2
cm1
cm2
Maybe what you should/can do is have this;
String cm = cm1 + "\n" + cm2
Which should output;
cm1
cm2
Then you change the xml to just have one textview and the adapter code as follows;
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> aa = new ArrayAdapter<String>(this, R.layout.row, R.id.textViewCM, listItems);
yourListView.setAdapter(aa);
Then, whenever you press the button;
// ...code to initialise/change cm
listItems.add(cm);
aa.notifyDataSetChanged(); // notifies listView to update it's view
That should do what you're after unless you've got to have the two individual textViews, in which case, you'll need to write a custom adapter, I've done a bit of a tutorial on my website if you follow that link.
This question already has answers here:
How to make an Android Spinner with initial text "Select One"?
(36 answers)
Closed 9 years ago.
I'm trying my Spinner to display "Select City" before the Spinner has itself been clicked by the user.
How can I do this?
My current XML code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.olacabs.customer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/page_background"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="#string/rate_card"
android:textColor="#color/white"
android:textSize="20dp"
custom:customFont="litera_bold.ttf" />
<Spinner
android:id="#+id/select_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:prompt="#string/selectCity" />
</LinearLayout>
Also, what does android:spinnerMode exactly do. I tried changing its value to dropdown but nothing happened and the application still showed a popup dialogue.
My activity that implements this XML file is:
public class RateCardActivity extends OlaActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.rate_card);
Spinner spinner = (Spinner) findViewById(R.id.select_city);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.select_city, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
you can do like this .. Create a custom adapter to insert..
Try the following code
In your resources
<string-array name="listarray">
<item>Select One</item>
<item>Item One</item>
<item>Item Two</item>
<item>Item Three</item>
</string-array>
In your onItemSelected Listener:
TextView selection;
spinnername.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0) {
}else {
// Your code to process the selection
}
}
});
For more info go through this Link & Link
CHange :
extends OlaActivity to extends Activity
I’m tryin to make a list box that has a different image and text for each listed item. I tried to creat two setListAdapter, one for the text string to be displayed and another for the image name. It did not work, I’m assuming there is another way to do this.
My CTICTY CLASS FOR THE LIST
public class cFashions extends ListActivity {
TextView selection;
// list to fill list box wi*/
/*
String[] items={"CASUAL","DR ESSES","CAREER","OUTWEAR","FOOTWEAR",
"JEWELRY","ACCESSORIES"};
*/
String[] items={"CASUAL"};
String[] icons={"icon"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fashions);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,
items));
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.icon,
icons));
selection=(TextView)findViewById(R.id.selection);
}
}
THE XML FILE FOR THE LAYOUT OF EACH CELL IN THE LIST
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/icon"
android:layout_width="22px"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
</LinearLayout>
Ted
You need to use one adapter, not two, where the adapter knows how to handle both your TextView and your ImageView. Here is a free excerpt from one of my books that covers this topic in detail.