how to show my contacts database value in my spinn

2019-06-09 10:16发布

问题:

In my layout xml I have a spinner and in my java I have a database with contacts and some values on it so I want to know how I send or put these values from the database in the spinner so when I click it, it selects the contact, any example of code?

回答1:

Get the contacts details from the database first. You can find a sample code for database concept at http://codinglookseasy.blogspot.in/2012/08/sqlite-database.html . Next from the above sample you may have got ArrayList of objects and set the contact names in the spinner using a

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);

Where list here may be the data from your ArrayList from the database. (store a specific field that you want to display from the database into this list). Get the item selected using

spinner2.getSelectedItem() 

which gives the item selected and get the data using from the ArrayList that you got from the database



回答2:

Just try

//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();


回答3:

Consider you in main 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);
}

Now getContacts() containsas follows :

// 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;
}

In 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;
}