我有九个具有相同功能的微调器,输入值为int数组。
所以,我用'编写了代码。环。我觉得这很有效。但它没有用。
当我执行此操作并检查微调器时,int数组没有变化。
如何使用'为'输入值到int数组?循环?
{
"dev": {
"public": {
"facebook": {
"appId": "abc123"
}
},
"private": {
"facebook": {
"secret": "456def789"
}
}
},
"prod": {
"public": {
"facebook": {
"appId": "def234"
}
},
"private": {
"facebook": {
"secret": "789ghi101112"
}
}
}
答案 0 :(得分:1)
这不起作用的原因
values [num] = position;
当事件侦听器触发时,num值始终为9。
您可以创建一个实现侦听器接口的内部类,并在构造函数上分配索引变量,如下所示:
private class IndexedItemSelectedListener implements AdapterView.OnItemSelectedListener {
private final int index;
public IndexedItemSelectedListener(int index) {
this.index = index;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
values[index] = position;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
并像这样使用它:
//OnItemSelectedListener
for (int i = 0; i < spinner.length; i++) {
spinner[i].setOnItemSelectedListener(new IndexedItemSelectedListener(i));
}
答案 1 :(得分:1)
试试这个......
<强> 1。 activity_main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/spinner_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dynamic Spinner"
android:id="@+id/textView3"
android:textColor="#000000"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
</RelativeLayout>
<强> 2.MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
LinearLayout mContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GenerateView();
}
private void GenerateView() {
mContainer = (LinearLayout)findViewById(R.id.spinner_container);
LinearLayout.LayoutParams mRowLayoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
mRowLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;
mRowLayoutParams.setMargins(8, 8, 8, 8);
String[] ITEMS = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"};
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ITEMS);
adapter0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for (int i = 0; i < 9; i++) {
Spinner spinner = new Spinner(this);
spinner.setId(i);
spinner.setAdapter(adapter0);
spinner.setOnItemSelectedListener(mOnItemSelectedListener);
spinner.setLayoutParams(mRowLayoutParams);
mContainer.addView(spinner);
}
}
private AdapterView.OnItemSelectedListener mOnItemSelectedListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner = (Spinner) parent;
switch (spinner.getId()) {
case 0:
shpwToast(spinner.getSelectedItem().toString());
break;
case 1:
shpwToast(spinner.getSelectedItem().toString());
break;
case 3:
shpwToast(spinner.getSelectedItem().toString());
break;
case 4:
shpwToast(spinner.getSelectedItem().toString());
break;
case 5:
shpwToast(spinner.getSelectedItem().toString());
break;
case 6:
shpwToast(spinner.getSelectedItem().toString());
break;
case 7:
shpwToast(spinner.getSelectedItem().toString());
break;
case 8:
shpwToast(spinner.getSelectedItem().toString());
break;
case 9:
shpwToast(spinner.getSelectedItem().toString());
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
private void shpwToast(String content) {
Toast.makeText(MainActivity.this, "Item Selected : " + content, Toast.LENGTH_SHORT).show();
}
}
第3。结果强>
快乐的编码......