所以我试图在文本视图中显示微调器中的选定项目,但我不知道为什么,它不会在微调器上显示所选项目,它显示为如果你可以帮助我,我会非常感激。 ps:微调器中的所有项目都是String。
Java代码:
function myScript($myVar){
return $myVar * $myVar;
}
$data1 = myScript(3);
$data2 = myScript(4);
$data3 = myScript(5);
echo $data1." - ".$data2." - ".$data3; //9 - 16 - 25
.xml principal
setContentView(R.layout.activity_verpedido);
RelativeLayout verpedido = (RelativeLayout) findViewById(R.id.verpedido);
View sanduche = getLayoutInflater().inflate(R.layout.activity_aprisasanduche, null);
verpedido.addView(sanduche);
String item1, item2;
int price;
TextView tvItem1, tvItem2, tvItem3, tvItem4;
Spinner Sping1, Sping2, Sping3, Sping4;
String Ssanduche1, Ssanduche2, Ssanduche3, Ssanduche4;
Sping1 = (Spinner)findViewById(R.id.Sing1);
Sping2 = (Spinner)findViewById(R.id.Sing2);
Sping3 = (Spinner)findViewById(R.id.Sing3);
Sping4 = (Spinner)findViewById(R.id.Sing4);
tvItem1 = (TextView)findViewById(R.id.vering1);
tvItem2 = (TextView)findViewById(R.id.vering2);
tvItem3 = (TextView)findViewById(R.id.vering3);
tvItem4 = (TextView)findViewById(R.id.vering4);
Ssanduche1 = (String) Sping1.getSelectedItem();
Ssanduche2 = (String) Sping2.getSelectedItem();
Ssanduche3 = (String) Sping3.getSelectedItem();
Ssanduche4 = (String) Sping4.getSelectedItem();
tvItem1.setText(Ssanduche1);
tvItem2.setText(Ssanduche2);
tvItem3.setText(Ssanduche3);
tvItem4.setText(Ssanduche4);

.xml secondary
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/verpedido"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.unlunch.unlunch.VerPedido"
android:background="#CC2124"
>
<TextView
android:id="@+id/ting1"
android:text="@string/Str_ing1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:textColor="#FFF1FE"
android:textColorHint= "#7A02CC"
android:textSize="17dp"
android:textStyle="italic"
/>
<TextView
android:id="@+id/vering1"
android:text=" "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ting1"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:textColor="#FFF1FE"
android:textColorHint= "#7A02CC"
android:textSize="17dp"
android:textStyle="italic"
/>
<TextView
android:id="@+id/ting2"
android:text="@string/Str_ing2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:textColor="#FFF1FE"
android:textSize="17dp"
android:textStyle="italic"
android:layout_below="@+id/vering1"
/>
<TextView
android:id="@+id/vering2"
android:text=" "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ting2"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:textColor="#FFF1FE"
android:textColorHint= "#7A02CC"
android:textSize="17dp"
android:textStyle="italic"
/>
<TextView
android:id="@+id/ting3"
android:text="@string/Str_ing3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/vering2"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:textColor="#FFF1FE"
android:textSize="17dp"
android:textStyle="italic"
/>
<TextView
android:id="@+id/vering3"
android:text=" "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ting3"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:textColor="#FFF1FE"
android:textColorHint= "#7A02CC"
android:textSize="17dp"
android:textStyle="italic"
/>
<TextView
android:id="@+id/ting4"
android:text="@string/Str_ing4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/vering3"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:textColor="#FFF1FE"
android:textSize="17dp"
android:textStyle="italic"
/>
<TextView
android:id="@+id/vering4"
android:text=" "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ting4"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:textColor="#FFF1FE"
android:textColorHint= "#7A02CC"
android:textSize="17dp"
android:textStyle="italic"
/>
</RelativeLayout>
&#13;
答案 0 :(得分:0)
试试这个..
首先将适配器设置为您的微调器,并且也适用于所有微调器
String[] mArray; //parameter initialize
mArray=getResources().getStringArray(R.array.mArray);
Sping1.setAdapter(new MyCustomAdapter(getActivity(), R.layout.row_spinner_data_sync, mArray));
Sping1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
str_sping1 =sp_post.getSelectedItem().toString();
tvItem1.setText(str_sping1);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
数组字符串接受string.xml
<!--Array String-->
<string-array name="mArray">
<item>arrayvalue1</item>
<item>arrayvalue2</item>
<item>arrayvalue3</item>
<item>arrayvalue4</item>
<item>arrayvalue5</item>
</string-array>
在您的课程中添加MyCustomAdapter ..您也可以尝试使用内置布局..
public class MyCustomAdapter extends ArrayAdapter<String>{
String[] arrayAdapt;
public MyCustomAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
arrayAdapt=objects;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
LayoutInflater inflater=getActivity().getLayoutInflater();
View row=inflater.inflate(R.layout.row_spinner_data_sync, parent, false);
TextView label=(TextView)row.findViewById(R.id.txt_organization);
label.setTextSize(20);
label.setText(arrayAdapt[position]);
return row;
}
}
row_spinner_data_sync.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt_organization"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/txt_color"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="2dp"
android:gravity="center_vertical"
android:textSize="20sp"
android:textStyle="bold"
android:text="name"
/>
答案 1 :(得分:0)
可以使用此代码:
首先使用数组适配器加载微调器数据,然后为每个加载器设置一个OnItemSelectedListener ..最后将它们加载到textview
public class MainActivity extends AppCompatActivity {
private String[] arraySpinner;
private String[] arraySpinner2;
private String[] arraySpinner3;
private String[] arraySpinner4;
TextView tvItem1, tvItem2, tvItem3, tvItem4;
Spinner Sping1, Sping2, Sping3, Sping4;
String Ssanduche1, Ssanduche2, Ssanduche3, Ssanduche4;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.arraySpinner = new String[] {
"1", "2", "3", "4", "5"
};
this.arraySpinner2 = new String[] {
"6", "7", "8", "9", "10"
};
this.arraySpinner3 = new String[] {
"11", "12", "13", "14", "15"
};
this.arraySpinner4 = new String[] {
"1", "2", "3", "4", "5"
};
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout verpedido = (RelativeLayout) findViewById(R.id.verpedido);
View sanduche = getLayoutInflater().inflate(R.layout.secondary, null);
verpedido.addView(sanduche);
String item1, item2;
int price;
Sping1 = (Spinner) findViewById(R.id.Sing1);
Sping2 = (Spinner) findViewById(R.id.Sing2);
Sping3 = (Spinner) findViewById(R.id.Sing3);
Sping4 = (Spinner) findViewById(R.id.Sing4);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
Sping1.setAdapter(adapter);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner2);
Sping2.setAdapter(adapter2);
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner3);
Sping3.setAdapter(adapter3);
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner4);
Sping4.setAdapter(adapter4);
tvItem1 = (TextView) sanduche.findViewById(R.id.ting1);
tvItem2 = (TextView) sanduche.findViewById(R.id.ting2);
tvItem3 = (TextView) sanduche.findViewById(R.id.ting3);
tvItem4 = (TextView) sanduche.findViewById(R.id.ting4);
Ssanduche1 = (String) Sping1.getSelectedItem().toString();
Ssanduche2 = (String) Sping2.getSelectedItem().toString();
Ssanduche3 = (String) Sping3.getSelectedItem().toString();
Ssanduche4 = (String) Sping4.getSelectedItem().toString();
tvItem1.setText(Ssanduche1);
tvItem2.setText(Ssanduche2);
tvItem3.setText(Ssanduche3);
tvItem4.setText(Ssanduche4);
Sping1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Ssanduche1 = (String) Sping1.getSelectedItem().toString();
tvItem1.setText(Ssanduche1);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Sping2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Ssanduche2 = (String) Sping2.getSelectedItem().toString();
tvItem2.setText(Ssanduche2);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Sping3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Ssanduche3 = (String) Sping3.getSelectedItem().toString();
tvItem3.setText(Ssanduche3);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Sping4.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Ssanduche4 = (String) Sping4.getSelectedItem().toString();
tvItem4.setText(Ssanduche4);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
答案 2 :(得分:0)
首先将Array设置为ArrayAdapter,然后将ArrayAdapter设置为spinner。像这样:
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array); //set Array object as "array"
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
然后将选定的文本形式微调器设置为textView -
textView.setText(spinner.getSelectedItem().toString);
答案 3 :(得分:0)
在您的代码中(您提供的内容),我没有看到您如何填充您的微调器?您有4个微调器,您没有在它们的xml或java代码中为它们提供条目。如果你想在xml中这样做,解决方案是:
<Spinner
android:id="@+id/Sing1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignLeft="@+id/ting1"
android:layout_alignStart="@+id/ting1"
android:popupBackground="#CC413D"
android:layout_below="@+id/ting1"
android:entries="@array/array_name"/>
在strings.xml中,您必须创建一个包含一些条目的数组:
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>
如果您想使用java代码,请执行以下内容:
//创建一个新的字符串ArrayList
ArrayList<String> entriesList = new ArrayList<String>();
//向其中添加项目
entriesList.add("String 1");
entriesList.add("String 2");
entriesList.add("String 3");
entriesList.add("String 4");
//填充微调器
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.simple_spinner_item, entriesList);
dataAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
Sping1.setAdapter(dataAdapter);
我没有看到任何代码填充你的微调器,所以我粘贴了这个,也许你在其他地方有代码。
最后,根据选择了微调器中的项目来更新任何TextView,如下所示:
Sping1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selected = (String) parent.getItemAtPosition(position);
// update your TextView here
// textView.setText(selected);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
希望这很有帮助。欢呼声!!