android - SPINNER 中是否有任何方法可以选择一个项目并显示另一个相关的值?
问题描述
我有一个带有“Customer”和“CustomerId”列的表的 SQLite 数据库。
例如:客户:John Doe 客户 ID:84746464
我用“客户”中的数据为微调器充气。当 usr 选择 John Doe 时,我需要在微调器“84746464”中显示。
在android中有没有办法做到这一点?
谢谢!
解决方案
在android中有没有办法做到这一点?
是的,当然,这是一个在 Spinner 中同时显示 CustomerId 和 Customer 的示例:-
这利用了简化问题的 CursorAdapter。但是,它确实需要一个专门命名为_id的列。但是,在此示例中,即使该列在表中不存在,它也是由用于获取所有客户 ( getAllCustomers
) 的查询生成的。
选择时生成的 Spinner 看起来像使用库存布局simple_list_item_2:-
- 请注意,使用其他布局相对简单。
CursorAdapters (示例使用非常灵活的 SimpleCursorAdapter)的优点是它们被设计为与 Cursors 一起使用。诸如onSelectedItem之类的事件允许直接访问相应定位的源游标,并将_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>
数据库助手.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");
}
}
}
推荐阅读
- php - Etsy 文件上传(卷曲)错误:请求正文太大
- vhdl - VHDL 3 位二进制编码的十二进制(基数 12)到 4 位二进制编码的 senary(基数 6)转换器
- python - 如何平均张量中的非零条目?
- javascript - 如何在 echo php 中显示模态引导
- python - 如何将 Python 发布请求发送到 Dialogflow 到导入/导出代理
- aruco - ArUco 估计PoseSingleMarkers 中markerLength 的值是多少?
- c# - 将多个属性合并到一个属性中
- pandas - 如何在使用 Pandas.read_gpq 加载带有列表列的表后恢复结构?
- ms-access - 如何使用 Access VBA 和加密密码解压缩受密码保护的 zip 文件
- matlab - 价值观停在不应该出现的地方