Raj*_*ana 4 java layout android dynamic spinner
我有一个触发1对话框的游戏视图,从这里触发第二个对话框.
在第二个对话框中,我有2个按钮和1个微调器.我遇到的问题是让一个微调器出现在我的视图中,带有动态数据.
基本上,我需要在单击按钮时弹出窗口并列出所有相关数据.我有3个不同的ArrayLists(1个String和2个整数),它们包含需要动态添加到每个微调器选择的sepearte信息,例如
pt1 - ArrayLists1
ArrayLists2
ArrayLists3
pt2 - ArrayLists1
ArrayLists2
ArrayLists3
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
这是我当前用来触发微调器点击的代码(此处的日志被触发):
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();
}
Run Code Online (Sandbox Code Playgroud)
如何在此单击上添加动态微调器.最重要的是,当点击微调器时,理想情况下我想在实际微调器内提交一个提交按钮,这样他们就可以在选择之前浏览选择(这应该始终可见).
最后,当在微调器中选择了项目并提交时,前一个对话框将自动刷新新数据(这我可以做得很容易).
如果您需要更多代码段,请告诉我们,我们将非常感谢您提供任何帮助或指导.
谢谢,L&L Partners
Son*_*ani 11
您可以使用此代码使用字符串数组列表填充您的微调器 -
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");
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码将放在你的布局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"/>
Run Code Online (Sandbox Code Playgroud)
请注意,您不是在xml文件中添加onClick.这就是你的onClick函数没有被调用的原因.您还可以使用strings.xml文件中的字符串数组来使用静态列表.
| 归档时间: |
|
| 查看次数: |
12865 次 |
| 最近记录: |