gpt4 book ai didi

java - 如何获取微调器上所选项目的 ID?

转载 作者:太空宇宙 更新时间:2023-11-04 10:50:49 26 4
gpt4 key购买 nike

我创建了一个 Activity ,其中用户需要从微调器中选择一个名称,在文本框中输入一条消息,然后单击一个按钮才能发送消息。微调器中有 2 个字符串(名字和姓氏)。数据库中的每个姓名还有一个唯一的 ID。 如何从数据库中获取姓名的 ID?

示例数据:ID:265958998、Fname:Leo、Lname:Cruz

下面是我的代码

发送按钮:

            btnsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String USERID=getIntent().toString();

db.open();
*// db.insertMessage(USERID, );*
db.close();

et10.setText("");
Toast.makeText(getApplicationContext(),
"Message Sent", Toast.LENGTH_SHORT).show();}}
});

将消息插入数据库:

   void insertMessage(String sender, String recipient) {
SQLiteDatabase db = DBHelper.getWritableDatabase();

ContentValues initialValues = new ContentValues();
initialValues.put(KEY_SENDER, sender);
initialValues.put(KEY_RECIPIENT, recipient);
db.insert(TABLE_MESSAGE, null, initialValues);


db.close(); // Closing database connection
}

旋转查询:

           public List<String> getAllRegisteredInfoAcc(){
List<String> labels = new ArrayList<String>();

// Select All Query
String selectQuery = "SELECT tableinfo.*, tableacc.*" +
"FROM tableinfo JOIN tableacc ON tableinfo.userid = tableacc.userid WHERE tableacc.userstatus=? ";
Cursor cursor = db.rawQuery(selectQuery , new String[]{ "REGISTERED"});

SQLiteDatabase db = DBHelper.getReadableDatabase();
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(4)+""+cursor.getString(3));
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
db.close();
return labels;
}

CallSpinner:

        spn1();

}
public void spn1() {
// database handler
DBAdapter db = new DBAdapter(getApplicationContext());
List<String> lables = db.getAllRegisteredInfoAcc();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);

dataAdapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
spnr1.setAdapter(dataAdapter);
}

最佳答案

您应该为微调器创建一个适配器,将 SQLite 中的数据加载到对象数组中,然后将数组中的对象映射到通过 toString() 函数反射(reflect)到微调器中。

首先为对象创建一个类并添加 toString() 函数

public class MyUser {
private int id = 0;
private String name = "";
private String lastName ="";

public MyUser(){}


public String toString()
{
return(name + " " + lastName);
}

}

然后将 sqlite 中的数据加载到该对象的数组中。

在旋转器中

users.addAll(myDataSource.getUsers());

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, users);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnr1.setPrompt("Select your User");
spnr1.setAdapter(
new NothingSelectedSpinnerAdapter(
adapter,
R.layout.row_state_register_nothing_selected,
// R.layout.contact_spinner_nothing_selected_dropdown, // Optional
this));

所以在 onItemClickListener() 中:

spnr1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (position > 0) { //because of the prompt
userID = users.get(position - 1).getUserId();
// DO WHAT YOU WANT
}
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});

关于java - 如何获取微调器上所选项目的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47887992/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com