在我的布局XML我有一个微调,在我的java我有与它接触,有的值的数据库,所以我想知道我是如何发送或把这些值从微调的数据库,所以当我点击它时,它选择接触,代码的任何示例?
Answer 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的使用数据
Answer 2:
你试一试
//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();
Answer 3:
考虑一下你的主要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()containsas如下:
// 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;
}
文章来源: how to show my contacts database value in my spinner?