我是Android中的一个begginer,我想在从微调器中选择一个值后更新我的主要视图,我的主视图。
public class MainActivity extends Activity { ArrayAdapter<String> adapter = null; Spinner spinnerMois, spinnerAnnee; DateAdapter dataAdapterMois , dataAdapterYear;spinnerMois = (Spinner) findViewById(R.id.spinnerMois); dataAdapterMois = new DateAdapter(this, android.R.layout.simple_spinner_item, loadMonth()); dataAdapterMois.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerMois.setAdapter(dataAdapterMois); spinnerAnnee = (Spinner) findViewById(R.id.spinnerAnnee); dataAdapterYear = new DateAdapter(this, android.R.layout.simple_spinner_item, loadYear()); dataAdapterYear.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerAnnee.setAdapter(dataAdapterYear); values = depenseBDD.getAllDepense(); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values); //Here the values should be update by my spinners}
我的“Depense”是一个目标:
public class Depense { int id; String dateDepense; float montant; String categ;}我希望,那些要求每个月和每年选择更新我的主要内容,并且只将“依赖性”更新为良好的月份和年份的2spinners ...并且说实话,我真的不知道该怎么做。 你有一些建议吗?
I'm a begginer in Android, and I would like to update my main, my principal view after select a value from a spinner.
public class MainActivity extends Activity { ArrayAdapter<String> adapter = null; Spinner spinnerMois, spinnerAnnee; DateAdapter dataAdapterMois , dataAdapterYear;spinnerMois = (Spinner) findViewById(R.id.spinnerMois); dataAdapterMois = new DateAdapter(this, android.R.layout.simple_spinner_item, loadMonth()); dataAdapterMois.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerMois.setAdapter(dataAdapterMois); spinnerAnnee = (Spinner) findViewById(R.id.spinnerAnnee); dataAdapterYear = new DateAdapter(this, android.R.layout.simple_spinner_item, loadYear()); dataAdapterYear.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerAnnee.setAdapter(dataAdapterYear); values = depenseBDD.getAllDepense(); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values); //Here the values should be update by my spinners}
My "Depense" is an objet :
public class Depense { int id; String dateDepense; float montant; String categ;}I would like, by the 2spinners who ask the month and year to chose to update my main and diplay only the "depense" with to good month and year... And to be honest, I dont really know how to do. Have u some suggestions to do ?
最满意答案
基本上,它很容易。 你可以使用“onItemSelect”像这样/如何在Android中使用onitemselected
它会看起来像这样:
public class spinner extends AppCompatActivity implements AdapterView.OnItemSelectedListener { Spinner year; TextView dateView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_spinner); dateView = (TextView) findViewById(R.id.date); year = (Spinner) findViewById(R.id.anneeSpinner); year.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { dateView.setText(adapterView.getItemAtPosition(i).toString()); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }和布局:
<Spinner android:id="@+id/anneeSpinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/year"/> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="helloo"/>并为该月添加另一个微调器和textview。 然后你可以将这些值添加到main中
basically, its pretty easy. you could use "onItemSelect" like this /how-can-i-use-onitemselected-in-android
and it will look something like this:
public class spinner extends AppCompatActivity implements AdapterView.OnItemSelectedListener { Spinner year; TextView dateView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_spinner); dateView = (TextView) findViewById(R.id.date); year = (Spinner) findViewById(R.id.anneeSpinner); year.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { dateView.setText(adapterView.getItemAtPosition(i).toString()); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }and for layout:
<Spinner android:id="@+id/anneeSpinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/year"/> <TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="helloo"/>and just add another spinner and textview for the month. and then you could add the values to main
更多推荐
发布评论