在 SPINNER 中有什么方法可以 select 一个项目并显示与之相关的另一个值?
Is there any way in a SPINNER to select an item and show another value related to?
我有一个带有 table 和 "Customer" 和 "CustomerId" 列的 SQLite 数据库。
例如:客户:John Doe
客户编号:84746464
我用 "Customer" 中的数据给微调器充气。当用户选择 John Doe 时,我需要在微调器“84746464”中显示。
在 android 中有办法做到这一点吗?
谢谢!
Is there anuway to do this in android?
是的,当然,这是一个在 Spinner 中同时显示 CustomerId 和 Customer 的示例:-
这利用了 CursorAdapter 简化了事情。然而,它确实需要一个专门命名为 _id 的列。但是,在此示例中,即使 table 中不存在该列,它也是由用于获取所有客户 (getAllCustomers
) 的查询生成的。
选择时生成的 Spinner 看起来像使用库存布局 simple_list_item_2 :-
- 注意使用其他布局相对简单。
CursorAdapters 的优势(该示例使用非常灵活的 SimpleCursorAdapter) 是它们被设计为与游标一起使用。 onSelectedItem 等事件允许直接访问相应定位的源 Cursor _id 的值传递给此类事件 (请参阅 onSelectedItem
示例中的代码,因为这说明了这一点).
- 由于 _id 列应该是 rowid 列的别名,因此它将始终唯一标识所选行 (再次在
onSelectedItem
中演示).
该示例包含这样一个侦听器,它既会吐司消息,也会将消息写入日志,例如(依次选择所有三个):-
2019-12-04 07:50:30.008 7679-7679/? D/SELECTEDITEMINFO: You selected Customer >Mary CustomerID > 12345678
The position in the List was 0
The id passed to onItemSelected was 12345678
2019-12-04 07:50:33.828 7679-7679/a.so59159856spinner D/SELECTEDITEMINFO: You selected Customer >Agnes CustomerID > 22222222
The position in the List was 1
The id passed to onItemSelected was 22222222
2019-12-04 07:50:38.666 7679-7679/a.so59159856spinner D/SELECTEDITEMINFO: You selected Customer >Fred CustomerID > 84746464
The position in the List was 2
The id passed to onItemSelected was 84746464
- 请注意,当设置 Spinner 时,将触发 onSelectedItem。
例子
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/myspinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String CUSTOMER_TABLE = "customer";
public static final String CUSTOMER_COL_CUSTOMERID = "CustomerId";
public static final String CUSTOMER_COL_CUSTOMER = "Customer";
SQLiteDatabase mDB;
public DatabaseHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE IF NOT EXISTS " + CUSTOMER_TABLE +
"(" +
CUSTOMER_COL_CUSTOMERID + " INTEGER PRIMARY KEY," +
CUSTOMER_COL_CUSTOMER + " TEXT" +
")"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insertCustomer(String customerId,String customer) {
ContentValues cv = new ContentValues();
cv.put(CUSTOMER_COL_CUSTOMER,customer);
cv.put(CUSTOMER_COL_CUSTOMERID,customerId);
return mDB.insert(CUSTOMER_TABLE,null,cv);
}
public Cursor getAllCustomers() {
String[] columns = new String[]
{
CUSTOMER_COL_CUSTOMERID,
CUSTOMER_COL_CUSTOMER,
CUSTOMER_COL_CUSTOMERID + " AS " + BaseColumns._ID
};
return mDB.query(CUSTOMER_TABLE,columns,null,null,null,null,null);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
Spinner mSpinner;
DatabaseHelper mDBHelper;
Cursor mAllCustomers;
SimpleCursorAdapter mSpinnerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = this.findViewById(R.id.myspinner);
mDBHelper = new DatabaseHelper(this);
addSomeTestingCustomers();
manageSpinner();
}
@Override
protected void onDestroy() {
super.onDestroy();
mAllCustomers.close(); /* Close the Cursor when done with it */
}
@Override
protected void onResume() {
super.onResume();
manageSpinner(); /* Refresh the Spinner in case data has changed */
}
/* Manage the Spinner */
private void manageSpinner() {
mAllCustomers = mDBHelper.getAllCustomers();
if (mSpinnerAdapter == null) {
String[] columns_to_display = new String[]{
DatabaseHelper.CUSTOMER_COL_CUSTOMERID,
DatabaseHelper.CUSTOMER_COL_CUSTOMER
};
int[] views_in_which_data_is_displayed = new int[]{
android.R.id.text1,
android.R.id.text2
};
mSpinnerAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
mAllCustomers,
columns_to_display,
views_in_which_data_is_displayed,
0);
mSpinner.setAdapter(mSpinnerAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selected_info =
"You selected Customer >" +
mAllCustomers.getString(mAllCustomers.getColumnIndex(DatabaseHelper.CUSTOMER_COL_CUSTOMER)) +
" CustomerID > " +
mAllCustomers.getString(mAllCustomers.getColumnIndex(DatabaseHelper.CUSTOMER_COL_CUSTOMERID)) +
"\n\tThe position in the List was " + position +
"\n\tThe id passed to onItemSelected was " + id
;
Log.d("SELECTEDITEMINFO",selected_info);
Toast.makeText(
view.getContext(),
selected_info,
Toast.LENGTH_SHORT
).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} else {
/* If the Spinner has been setup then refresh the List */
/* This would be called whenever the Database has been (or might have been) changed */
/* typically you would override the activities onResume method to call this */
mSpinnerAdapter.swapCursor(mAllCustomers);
}
}
/* Add some testing data to the database if none already exists */
private void addSomeTestingCustomers() {
if (DatabaseUtils.queryNumEntries(mDBHelper.getWritableDatabase(),DatabaseHelper.CUSTOMER_TABLE) < 1) {
mDBHelper.insertCustomer("84746464","Fred");
mDBHelper.insertCustomer("12345678","Mary");
mDBHelper.insertCustomer("22222222","Agnes");
}
}
}
这个例子很完美,只有一件事,我需要在微调器项目 selected 时只显示客户 ID,而不是两者,这可能吗?
如果我在 select 一个项目时使用上面的代码,微调器会显示 CUSTOMER 和 CUSTOMER ID。
我有一个带有 table 和 "Customer" 和 "CustomerId" 列的 SQLite 数据库。
例如:客户:John Doe 客户编号:84746464
我用 "Customer" 中的数据给微调器充气。当用户选择 John Doe 时,我需要在微调器“84746464”中显示。
在 android 中有办法做到这一点吗?
谢谢!
Is there anuway to do this in android?
是的,当然,这是一个在 Spinner 中同时显示 CustomerId 和 Customer 的示例:-
这利用了 CursorAdapter 简化了事情。然而,它确实需要一个专门命名为 _id 的列。但是,在此示例中,即使 table 中不存在该列,它也是由用于获取所有客户 (getAllCustomers
) 的查询生成的。
选择时生成的 Spinner 看起来像使用库存布局 simple_list_item_2 :-
- 注意使用其他布局相对简单。
CursorAdapters 的优势(该示例使用非常灵活的 SimpleCursorAdapter) 是它们被设计为与游标一起使用。 onSelectedItem 等事件允许直接访问相应定位的源 Cursor _id 的值传递给此类事件 (请参阅 onSelectedItem
示例中的代码,因为这说明了这一点).
- 由于 _id 列应该是 rowid 列的别名,因此它将始终唯一标识所选行 (再次在
onSelectedItem
中演示).
该示例包含这样一个侦听器,它既会吐司消息,也会将消息写入日志,例如(依次选择所有三个):-
2019-12-04 07:50:30.008 7679-7679/? D/SELECTEDITEMINFO: You selected Customer >Mary CustomerID > 12345678
The position in the List was 0
The id passed to onItemSelected was 12345678
2019-12-04 07:50:33.828 7679-7679/a.so59159856spinner D/SELECTEDITEMINFO: You selected Customer >Agnes CustomerID > 22222222
The position in the List was 1
The id passed to onItemSelected was 22222222
2019-12-04 07:50:38.666 7679-7679/a.so59159856spinner D/SELECTEDITEMINFO: You selected Customer >Fred CustomerID > 84746464
The position in the List was 2
The id passed to onItemSelected was 84746464
- 请注意,当设置 Spinner 时,将触发 onSelectedItem。
例子
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/myspinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String CUSTOMER_TABLE = "customer";
public static final String CUSTOMER_COL_CUSTOMERID = "CustomerId";
public static final String CUSTOMER_COL_CUSTOMER = "Customer";
SQLiteDatabase mDB;
public DatabaseHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE IF NOT EXISTS " + CUSTOMER_TABLE +
"(" +
CUSTOMER_COL_CUSTOMERID + " INTEGER PRIMARY KEY," +
CUSTOMER_COL_CUSTOMER + " TEXT" +
")"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insertCustomer(String customerId,String customer) {
ContentValues cv = new ContentValues();
cv.put(CUSTOMER_COL_CUSTOMER,customer);
cv.put(CUSTOMER_COL_CUSTOMERID,customerId);
return mDB.insert(CUSTOMER_TABLE,null,cv);
}
public Cursor getAllCustomers() {
String[] columns = new String[]
{
CUSTOMER_COL_CUSTOMERID,
CUSTOMER_COL_CUSTOMER,
CUSTOMER_COL_CUSTOMERID + " AS " + BaseColumns._ID
};
return mDB.query(CUSTOMER_TABLE,columns,null,null,null,null,null);
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
Spinner mSpinner;
DatabaseHelper mDBHelper;
Cursor mAllCustomers;
SimpleCursorAdapter mSpinnerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = this.findViewById(R.id.myspinner);
mDBHelper = new DatabaseHelper(this);
addSomeTestingCustomers();
manageSpinner();
}
@Override
protected void onDestroy() {
super.onDestroy();
mAllCustomers.close(); /* Close the Cursor when done with it */
}
@Override
protected void onResume() {
super.onResume();
manageSpinner(); /* Refresh the Spinner in case data has changed */
}
/* Manage the Spinner */
private void manageSpinner() {
mAllCustomers = mDBHelper.getAllCustomers();
if (mSpinnerAdapter == null) {
String[] columns_to_display = new String[]{
DatabaseHelper.CUSTOMER_COL_CUSTOMERID,
DatabaseHelper.CUSTOMER_COL_CUSTOMER
};
int[] views_in_which_data_is_displayed = new int[]{
android.R.id.text1,
android.R.id.text2
};
mSpinnerAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
mAllCustomers,
columns_to_display,
views_in_which_data_is_displayed,
0);
mSpinner.setAdapter(mSpinnerAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selected_info =
"You selected Customer >" +
mAllCustomers.getString(mAllCustomers.getColumnIndex(DatabaseHelper.CUSTOMER_COL_CUSTOMER)) +
" CustomerID > " +
mAllCustomers.getString(mAllCustomers.getColumnIndex(DatabaseHelper.CUSTOMER_COL_CUSTOMERID)) +
"\n\tThe position in the List was " + position +
"\n\tThe id passed to onItemSelected was " + id
;
Log.d("SELECTEDITEMINFO",selected_info);
Toast.makeText(
view.getContext(),
selected_info,
Toast.LENGTH_SHORT
).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
} else {
/* If the Spinner has been setup then refresh the List */
/* This would be called whenever the Database has been (or might have been) changed */
/* typically you would override the activities onResume method to call this */
mSpinnerAdapter.swapCursor(mAllCustomers);
}
}
/* Add some testing data to the database if none already exists */
private void addSomeTestingCustomers() {
if (DatabaseUtils.queryNumEntries(mDBHelper.getWritableDatabase(),DatabaseHelper.CUSTOMER_TABLE) < 1) {
mDBHelper.insertCustomer("84746464","Fred");
mDBHelper.insertCustomer("12345678","Mary");
mDBHelper.insertCustomer("22222222","Agnes");
}
}
}
这个例子很完美,只有一件事,我需要在微调器项目 selected 时只显示客户 ID,而不是两者,这可能吗?
如果我在 select 一个项目时使用上面的代码,微调器会显示 CUSTOMER 和 CUSTOMER ID。