java Android - 动态微调器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11564891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Dynamic Spinner
提问by Raj Labana
I have a Game View which triggers 1 Dialog, from here a second Dialog is triggered.
我有一个触发 1 个对话框的游戏视图,从这里触发了第二个对话框。
Within this second dialog, I have 2 buttons and 1 spinner. The issue I am having is getting a spinner to appear within my view, with dyamic data.
在第二个对话框中,我有 2 个按钮和 1 个微调器。我遇到的问题是让一个带有动态数据的微调器出现在我的视图中。
Essentially, I need to spinner to popup when the button is clicked on and list all the relevent data. I have 3 different ArrayLists (1 String and 2 integers), which hold sepearte information which will need to be dynamically added to each spinner choice e.g.
本质上,当单击按钮并列出所有相关数据时,我需要旋转以弹出窗口。我有 3 个不同的 ArrayList(1 个字符串和 2 个整数),它们包含需要动态添加到每个微调器选择的单独信息,例如
pt1 - ArrayLists1
ArrayLists2
ArrayLists3
pt2 - ArrayLists1
ArrayLists2
ArrayLists3
Spinner XML layout code:
Spinner XML 布局代码:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<Button android:text="@string/attack"
android:id="@+id/Attack"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:text="@string/useitem"
android:id="@+id/UseItemBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner android:text="@string/useitem"
android:id="@+id/UseItemSpin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<Button android:text="@string/equipweapon"
android:id="@+id/EquipWeapon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Here is the code that I am currently using to trigger the click of the spinner (the log here is triggered):
这是我目前用来触发微调器点击的代码(这里的日志被触发):
import android.widget.AdapterView.OnItemSelectedListener;
@Override
public void onClick(View clicked)
{
if(clicked.getId() == R.id.UseItemBtn)
{
Spinner spinner = (Spinner) findViewById(R.id.UseItemSpin);
ArrayList<String> arrayList1 = new ArrayList<String>();
arrayList1.add("test item the first one");
arrayList1.add("really long list item - section 2 goes here - finally section 3");
ArrayAdapter<String> adp = new ArrayAdapter<String> (context,android.R.layout.simple_spinner_dropdown_item,arrayList1);
// APP CURRENTLY CRASHING HERE
spinner.setAdapter(adp);
//Set listener Called when the item is selected in spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int position, long arg3)
{
String city = "The city is " + parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), city, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
BattleRun.show();
}
How can I add the dynamic spinner on this click. On top of this when the spinner is clicked, ideally I would like a submit button within the actual spinner so they can flick through the choices before selecting (which should be visible at all times).
如何在此单击时添加动态微调器。最重要的是,当点击微调器时,理想情况下,我希望在实际微调器中有一个提交按钮,以便他们可以在选择之前轻弹选择(应该始终可见)。
Finally, when the item has been selected in the spinner and submitted the previous dialog will automatically refresh with the new data (this I can do easy enough).
最后,当项目在微调器中被选中并提交时,前一个对话框将自动使用新数据刷新(我可以很容易地做到这一点)。
If you require any more code snippets please let me know, any help or guidence would be much appreciated.
如果您需要更多代码片段,请告诉我,任何帮助或指导将不胜感激。
Thanks, L & L Partners
谢谢,L&L合作伙伴
回答by Sonam Daultani
You can use this code to populate your spinner with the string array list -
您可以使用此代码用字符串数组列表填充微调器 -
public void onClick(View clicked){
if(clicked.getId() == R.id.Attack){
Spinner spinner = (Spinner) findViewById(R.id.UseItem);
//Sample String ArrayList
ArrayList<String> arrayList1 = new ArrayList<String>();
arrayList1.add("Bangalore");
arrayList1.add("Delhi");
arrayList1.add("Mumbai");
ArrayAdapter<String> adp = new ArrayAdapter<String> (this,android.R.layout.simple_spinner_dropdown_item,arrayList1);
spinner.setAdapter(adp);
spinner.setVisibility(View.VISIBLE);
//Set listener Called when the item is selected in spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3)
{
String city = "The city is " + parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), city, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
//BattleRun.dismiss();
Log.d("Item","Clicked");
}
}
This code will go inside your layout xml file -
此代码将进入您的布局 xml 文件 -
<!-- Add onClick attribute on this button -->
<Button android:text="@string/attack"
android:id="@+id/Attack"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick" />
<Spinner android:text="@string/useitem"
android:id="@+id/UseItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<Button android:text="@string/equipweapon"
android:id="@+id/EquipWeapon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
Note that you are not adding onClick inside the xml file. That's why your onClick function is not getting called. You can also use the string-array in strings.xml file for using static list.
请注意,您没有在 xml 文件中添加 onClick。这就是为什么您的 onClick 函数没有被调用的原因。您还可以使用 strings.xml 文件中的字符串数组来使用静态列表。