在微调器中选择一个值后更新我的视图(Update my view after select a value in a spinner)

编程入门 行业动态 更新时间:2024-10-12 12:23:34
在微调器中选择一个值后更新我的视图(Update my view after select a value in a spinner)

我是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

更多推荐

本文发布于:2023-08-01 12:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   器中   Update   spinner   select

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!