在我的布局xml中,我有一个微调器,在我的java中,我有一个带有联系人的数据库和一些值,所以我想知道如何从数据库中发送或放置这些值在微调器中,所以当我点击它时,它选择联系人,任何代码示例?
答案 0 :(得分:1)
首先从数据库中获取联系人详细信息。 您可以在http://codinglookseasy.blogspot.in/2012/08/sqlite-database.html找到数据库概念的示例代码。 接下来从上面的示例中,您可能已获得对象的ArrayList,并使用
在微调器中设置联系人姓名ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
此处的列表可能是数据库中ArrayList的数据。 (将要从数据库中显示的特定字段存储到此列表中)。 使用
获取所选项目spinner2.getSelectedItem()
选择项目并使用从数据库中获取的ArrayList获取数据
答案 1 :(得分:0)
试试
//Initialize the spinner view
Spinner spinnerContact = (Spinnner) findViewById(R.id.spinner);
//Initialise List Variable
List<String> contactsList = databaseObj.getContacts;
// Create and Implement Adapter for spinner
ArrayAdapter<String> contactAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, contactsList );
//set drop down view for spinner
contactAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//set adapter for spinner
spinnerContact.setAdapter(contactAdapter);
//select the item in spinner defaultly
spinnerContact.setSelection(position);
//get Selected Item Text from the spinner
spinnerContact.getSelectedItem();
答案 2 :(得分:0)
在主要的Activty中考虑你
public class MainActivity extends Activity {
private List<String> contactList;
private Spinner spinnerComp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerdemo);
spinnerComp= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getContacts());
adapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerComp.setAdapter(adapter);
spinnerComp.setSelection(0);
}
现在getContacts()包含以下内容:
// get Contact Names
private List<String> getContacts() {
// get Contacts
List<Contact> contacts = DBHelper.getContacts();
List<String> contactNames = null;
if (contacts.size() != 0) {
contactNames = new ArrayList<String>();
for (Contact contact : Contacts) {
String contactName = contact.getName();
contactNames.add(contactName);
}
}
return contactNames;
}
在DbHelper.java
public List<Contact> getAllContacts() {
String sql = "SELECT * FROM " + Table.CONTACT + " ORDER BY " +
Contact.Column.NAME + " ASC";
List<Contact> contacts = new ArrayList<Contact>();
Cursor cursor = database.rawQuery(sql, null);
while(cursor.moveToNext()) {
Contact contact = getContactFromCursor(cursor);
if(Contact != null) {
contacts.add(Contact);
}
}
cursor.close();
return contacts;
}
private Contact getContactFromCursor(Cursor cursor) {
long id = cursor.getLong(cursor.getColumnIndex(Contact.Column.ID));
String name = cursor.getString(cursor.getColumnIndex(Contact.Column.NAME));
long phoneNumber = cursor.getLong(cursor.getColumnIndex(Contact.Column.DATE_OF_BIRTH));
Contact Contact = new Contact(name, phoneNumber);
Contact.setId(id);
return Contact;
}