我有3个微调器
1)Array Spinner
2)XML Spinner和
3)SQLite Cursor Spinner
当我添加我的第3个Spinner时,页面开始表现得很奇怪。旋转器1& 2开始出现在微调器3区域。 Cursor中的值存在于Spinner 3中。当我选择任何值时,TOAST
可以正常工作但值不会被选中。
布局文件:spinner_demo_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.training.sqlitelistviewdemo.SpinnerDemo">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Country"
android:textColor="#000000"
android:textSize="20sp" />
<Spinner
android:id="@+id/spCountries"
style="@android:style/Widget.Holo.Light.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/country_arrays"
android:prompt="@string/country_prompt"
android:spinnerMode="dialog">
</Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Business Type"
android:textColor="#000000"
android:textSize="20sp" />
<Spinner
android:id="@+id/spBussinessType"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/business_prompt">
</Spinner>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Select Shop Name"
android:textColor="#000000"
android:textSize="20sp" />
<Spinner
android:id="@+id/sp_Shops"
style="@android:style/Widget.Holo.Light.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/select_shops"
android:spinnerMode="dialog">
</Spinner>
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Submit"
android:textSize="15sp" />
我的Java文件:SpinnerDemo.java
package com.training.sqlitelistviewdemo;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class SpinnerDemo extends AppCompatActivity implements View.OnClickListener {
// uicontrols
Spinner spCountries;
Spinner spBusinessType;
Button btnsubmit;
//class members
String businessType[] = { "Automobile", "Food", "Computers", "Education", "Personal", "Travel" };
ArrayAdapter<String> adapterBusinessType;
// local members
String sbusinesstype,scountry;
// SQLite Parameters Start
private DatabaseManager dbManager;
private TextView tv_Text;
private Spinner sp;
private SimpleCursorAdapter adapter;
// final String[] from = new String[] { DatabaseHelper._ID, DatabaseHelper.SHOPNAME, DatabaseHelper.SHOPADDRESS };
final String[] from = new String[] {DatabaseHelper.SHOPNAME};
//final int[] to = new int[] { R.id.id, R.id.shopname, R.id.shopaddress };
final int[] to = new int[] {android.R.id.text1};
// SQLite Parameters End
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_demo_main);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
spCountries = (Spinner) findViewById(R.id.spCountries);
spBusinessType = (Spinner) findViewById(R.id.spBussinessType);
btnsubmit=(Button)findViewById(R.id.submit);
btnsubmit.setOnClickListener(this);
// Initialize and set Adapter
adapterBusinessType = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, businessType);
adapterBusinessType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spBusinessType.setAdapter(adapterBusinessType);
// Country Item Selected Listener
spCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
scountry = adapter.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(getApplicationContext(),
"Selected Country : " + scountry, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// Business Type Item Selected Listener
spBusinessType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
sbusinesstype = adapter.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(getApplicationContext(),
"Bussiness Type : " + sbusinesstype, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// SQLite Spinner Code Starts
dbManager = new DatabaseManager(this);
dbManager.open();
Cursor cursor = dbManager.fetch_Shop();
sp = (Spinner) findViewById(R.id.sp_Shops);
// sp.setEmptyView(findViewById(R.id.empty));
adapter = new SimpleCursorAdapter(this, R.layout.spinner_demo_main, cursor, from, to, 0);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
//sp.setSelection(1);
//adapter.notifyDataSetChanged();
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
sp.setSelection(position);
Cursor shopCur=(Cursor)sp.getSelectedItem();
String shopNamecol=shopCur.getString(shopCur.getColumnIndex(DatabaseHelper.SHOPNAME));
String Rowid = shopCur.getString(shopCur.getColumnIndex(DatabaseHelper._ID));
Toast.makeText(getApplicationContext(), "You Selected - " + shopNamecol + " - " + Rowid + " " + position, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
// SQLite Spinner Code Ends
}
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You have selected " + scountry + " and " + sbusinesstype,Toast.LENGTH_LONG).show();
}
}
任何帮助都会非常感激。
答案 0 :(得分:0)
要解决此问题,请在java文件中声明三个微调器并在java文件中初始化它们
$('#myModal').on('hidden.bs.modal', function (e) {
player.pause();
})
现在添加setOnItemSelectedListener并处理使用switch case。
对我来说很好。
答案 1 :(得分:0)
发现问题..Phew
更新了以下内容
adapter = new SimpleCursorAdapter(this, R.layout.spinner_demo_main, cursor, from, to, 0);
spinner_demo_main - 这是我的Activity类 - 我们应该给予android.R.layout.simple_spinner_item
如下
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor, from, to, 0);