package org.example.mbtiapplication;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MBTITest extends Activity implements OnItemSelectedListener {
private Spinner firstSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mbtitest);
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, 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
firstSpinner.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
XML Układ:Jak korzystać z opcji onItemSelected w systemie Android?
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="I like to go out more than staying home." />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:textSize="10.5dp"
android:text="Sensing v Intuition" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
Jestem nowy programista Android i kłopoty z korzystaniem Spinner, próbowałem wiele tutoriali i nadal jestem zdezorientowany. Chciałbym wiedzieć, jaki jest mój następny krok, o ile wiem, ustawiłem swój spinner w XML, w Javie zidentyfikowałem tego spinnera, stworzyłem ArrayAdapter dla wspomnianego spinnera i sprecyzowałem kilka opcji. Nie jestem do końca pewien, czy zaludniłem jeszcze spinner, czy też, jak to zrobić, z obiektem typu spinner. Chciałbym móc użyć obiektu typu spinner, aby wybrać jedną z trzech opcji, a następnie zachować tę wartość wewnątrz widoku tekstowego wewnątrz pokrętła.
Czy możesz pokazać mi przykład? To kopie mój tyłek przez ostatnie godziny. –
Zobacz edycję, to wszystko, co do niej należy. – bgse
Po prostu przyłapany na tym, że brakuje ci również "spinner.setOnItemSelectedListener (this)" :) – bgse