问题
每当我为spinner编写代码时,它都不会显示所选项目。它只显示所选项目的祝酒词。
public class Retailormainhomepage extends Activity implements AdapterView.OnItemSelectedListener {
EditText mname;
TextView mcategory;
EditText mproduct;
Button mupload,mproducts;
String enteredRetailorname,enteredCategory,enteredProduct;
String enteredid,enteredRetailor_name,enteredCategory_id,enteredProduct_name;
String jsonresponce=null;
String UPLOAD_URL,PRODUCTS_URL;
ProgressDialog pd;
ImageView mimageback;
InputStream is = null;
private Spinner spinner;
String result = null;
List<String> list;
boolean connection = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_retailorhomepage);
mimageback = (ImageView)findViewById(R.id.imageback);
mname = (EditText)findViewById(R.id.rname);
mcategory = (TextView)findViewById(R.id.category);
mproduct = (EditText)findViewById(R.id.product);
mupload = (Button)findViewById(R.id.upload);
spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(Retailormainhomepage.this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
XML文件:
这是我同样要求的xml文件。我没有得到任何logcat错误,因为它没有在编辑文本中显示所选项目。
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="54dp"
android:layout_marginTop="22dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:weightSum="1">
<TextView
android:layout_width="125dp"
android:layout_height="40dp"
android:text="category"
android:id="@+id/category"
android:textColor="#000000"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:textSize="20dp"/>
<EditText
android:layout_width="173dp"
android:layout_height="40dp"
android:id="@+id/etcategory"
android:hint="1,2,..."
android:layout_marginLeft="20dp"
android:textColor="#000000"
android:layout_alignTop="@+id/textView2"
android:layout_alignParentRight="true"
android:textSize="15dp"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/category"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp" />
</LinearLayout>
答案 0 :(得分:0)
请尝试以下操作:
@Override public void onItemSelected(AdapterView parent, View view, int position, long id) { String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
//set as selected item.
spinner.setSelection(position)
}
有关详细信息,请访问documentation。
答案 1 :(得分:0)
试试这个。
@Override public void onItemSelected(AdapterView parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
spinner.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
看起来你需要使用微调器的setSelection方法设置值。
编辑:
对于多个选择项目,请查看以下参考链接。
https://trinitytuts.com/tips/multiselect-spinner-item-in-android/
https://asnehal.wordpress.com/2012/04/03/multi-select-drop-down-list-in-android/
答案 2 :(得分:0)
是的,我解决了我的问题,但仍有一点疑问,即一次无法选择多个项目。 有没有其他代码。