0

在我的布局 xml 中,我有一个微调器,在我的 java 中,我有一个包含联系人和一些值的数据库,所以我想知道如何从数据库中发送或放置这些值到微调器中,所以当我单击它时,它会选择联系方式,有代码示例吗?

4

3 回答 3

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 获取数据

于 2012-09-11T17:17:51.400 回答
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();
于 2012-09-11T17:25:55.357 回答
0

在主要活动中考虑你

 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;
}
于 2012-09-11T18:42:26.667 回答