Java Android-动态微调器

Java Android-动态微调器,java,android,layout,dynamic,spinner,Java,Android,Layout,Dynamic,Spinner,我有一个触发1个对话框的游戏视图,从这里触发第二个对话框 在第二个对话框中,我有两个按钮和一个微调器。我遇到的问题是让一个微调器出现在我的视图中,带有动态数据 基本上,我需要微调器,以便在单击按钮时弹出并列出所有相关数据。我有3个不同的ArrayList(1个字符串和2个整数),其中包含需要动态添加到每个微调器选项的单独信息,例如 pt1 - ArrayLists1 ArrayLists2 ArrayLists3 pt2 - ArrayLists1

我有一个触发1个对话框的游戏视图,从这里触发第二个对话框

在第二个对话框中,我有两个按钮和一个微调器。我遇到的问题是让一个微调器出现在我的视图中,带有动态数据

基本上,我需要微调器,以便在单击按钮时弹出并列出所有相关数据。我有3个不同的ArrayList(1个字符串和2个整数),其中包含需要动态添加到每个微调器选项的单独信息,例如

pt1 -   ArrayLists1
        ArrayLists2
        ArrayLists3
pt2 -   ArrayLists1
        ArrayLists2
        ArrayLists3
微调器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>

以下是我当前用于触发微调器单击的代码(此处的日志已触发):

导入android.widget.AdapterView.OnItemSelectedListener;
@凌驾
公共void onClick(单击视图)
{
如果(单击.getId()==R.id.UseItemBtn)
{
微调器微调器=(微调器)findViewById(R.id.UseItemSpin);
ArrayList ArrayList 1=新的ArrayList();
arrayList1.添加(“第一个测试项目”);
arrayList1.add(“非常长的列表项-第2节在这里-最后是第3节”);
ArrayAdapter adp=新的ArrayAdapter(上下文,android.R.layout.simple\u微调器\u下拉菜单\u项,arrayList1);
//应用程序正在此处崩溃
旋转器.设置适配器(adp);
//在微调器中选择项目时调用Set listener
spinner.setOnItemSelectedListener(新的OnItemSelectedListener()
{
已选择公共视图(AdapterView父视图、视图视图、整型位置、长arg3)
{
String city=“城市是”+parent.getItemAtPosition(position.toString();
Toast.makeText(parent.getContext(),city,Toast.LENGTH_LONG.show();
}
未选择公共无效(AdapterView arg0)
{
//TODO自动生成的方法存根
}
});
}
BattleRun.show();
}
如何在此单击中添加动态微调器。最重要的是,单击微调器时,理想情况下,我希望在实际微调器中有一个submit按钮,这样他们可以在选择之前快速浏览选项(应该始终可见)

最后,在微调器中选择并提交项目后,上一个对话框将自动使用新数据刷新(我可以很容易地做到这一点)

如果您需要更多的代码片段,请让我知道,任何帮助或指导将不胜感激

谢谢,
L&L Partners

您可以使用此代码用字符串数组列表填充微调器-

    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");

    }
}
public void onClick(单击视图){
if(clicked.getId()==R.id.Attack){
微调器微调器=(微调器)findViewById(R.id.UseItem);
//示例字符串数组列表
ArrayList ArrayList 1=新的ArrayList();
arrayList1.添加(“班加罗尔”);
arrayList1.添加(“德里”);
arrayList1.添加(“孟买”);
ArrayAdapter adp=新的ArrayAdapter(这是android.R.layout.simple\u spinner\u dropdown\u项目,arrayList1);
旋转器.设置适配器(adp);
spinner.setVisibility(View.VISIBLE);
//在微调器中选择项目时调用Set listener
spinner.setOnItemSelectedListener(新的OnItemSelectedListener()
{
已选择公共视图(AdapterView父视图、视图、,
整数位置,长arg3)
{
String city=“城市是”+parent.getItemAtPosition(position.toString();
Toast.makeText(parent.getContext(),city,Toast.LENGTH_LONG.show();
}
未选择公共无效(AdapterView arg0)
{
//TODO自动生成的方法存根
}
});
//战斗跑。解散();
Log.d(“项目”,“点击”);
}
}
此代码将进入布局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"/>

请注意,您没有在xml文件中添加onClick。这就是为什么没有调用onClick函数的原因。
您还可以使用strings.xml文件中的字符串数组来使用静态列表。

我是整个android和java开发的新手。基本上,我有一个对话。这有一个按钮(可以工作)和一个2 x微调器。我需要它,以便在单击微调器时,它会加载我在问题中显示的格式。这将包括来自3个不同ArrayList(1 x字符串和2 x整数)的数据。总而言之,我遇到的主要问题是检测微调器按钮的点击,然后使用动态内容创建它。谢谢。。答案有用吗。。。你在你的应用程序中得到了你需要的东西了吗?嗨,只是想实现你上面的修复,但是得到了一些错误。您的第三行代码引用了ArrayAdapter,我收到一个错误,说它未定义。此外,无法检测到setOnItemSelectedListener。我应该改用SetonicClickListener吗?谢谢,我从上面的代码中注意到的另一件事是,它只定义1个ArrayList数据进入微调器,而我需要将3个ArrayList组合在一起。你在哪里编写上面的代码?理想情况下,它应该进行活动。您是否在项目中添加了Android依赖项。因为您应该获得ArrayAdapter&setOnItemSelectedListener,否则。
<!-- 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"/>