android - 从 Spinner 中的 SQLite 加载数据

标签 android sqlite spinner

我正在尝试将我的员工姓名从 SQL 加载到我的微调器中,但这是我得到的:

enter image description here

如您所见,我的应用程序的目录正在被加载,但我不知道为什么。

我的 Activity :

public class AddAbsenceForm extends Activity implements OnItemSelectedListener {

Spinner spinner;
Button buttonAdd;

public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_absence_form);

    //spinner element
    spinner = (Spinner) findViewById(R.id.names_spinner);

    //spinner click listener
    spinner.setOnItemSelectedListener(this);
    //loading spinner data from database
    loadSpinnerData();

}

private void loadSpinnerData() {
    //database handler
    LysandrosDatabaseAdapter db = new LysandrosDatabaseAdapter(getApplicationContext());
    //spinner drop down elements
    List<DataBean> list = db.getAllDat();
    //creating adapter for spinner
    ArrayAdapter<DataBean > dataAdapter = new ArrayAdapter<DataBean>(this, android.R.layout.simple_spinner_dropdown_item, list);
    //drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    // On selecting a spinner item
    String list = parent.getItemAtPosition(position).toString();
    //showing selected spinner item
    Toast.makeText(parent.getContext(), "You selected: " + list, Toast.LENGTH_LONG).show();
}

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

}

}

数据库方法:

 public List <DataBean> getAllDat(){

    List<DataBean> list = new ArrayList<>();

    SQLiteDatabase db = helper.getReadableDatabase();

    String [] columns = {LysandrosHelper.UID, LysandrosHelper.NAME, LysandrosHelper.SURNAME, LysandrosHelper.DEPARTMENT, LysandrosHelper.WORKPLACE};
    Cursor cursor = db.query(LysandrosHelper.TABLE_NAME, columns, null, null, null, null, null);

    while (cursor.moveToNext()) {

        int index = cursor.getColumnIndex(LysandrosHelper.UID);
        int index2 = cursor.getColumnIndex(LysandrosHelper.NAME);
        int index3 = cursor.getColumnIndex(LysandrosHelper.SURNAME);
        int index4 = cursor.getColumnIndex(LysandrosHelper.DEPARTMENT);
        int index5 = cursor.getColumnIndex(LysandrosHelper.WORKPLACE);
        int cid = cursor.getInt(index);

        String persoName = cursor.getString(index2);
        String personSurname = cursor.getString(index3);
        String personDepartment = cursor.getString(index4);
        String personWorkplace = cursor.getString(index5);

        DataBean bean = new DataBean(cid, persoName, personSurname, personDepartment, personWorkplace);
        list.add(bean);
    }

    return list;
}

XML 中的微调器:

   <Spinner
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/names_spinner"
        android:text="@string/select_employee">
    </Spinner>

请有人告诉我我错过了什么

编辑:

我的 DataBean 类:

public class DataBean {

protected int id;
protected String name;
protected String surname;
protected String department;
protected String workplace;


public DataBean() {

}

public DataBean (int id, String name, String surname, String department, String workplace ) {
    this.id = id;
    this.name = name;
    this.surname = surname;
    this.department = department;
    this.workplace = workplace;
}

public DataBean (String name, String surname, String department, String workplace) {
    this.name = name;
    this.surname = surname;
    this.department = department;
    this.workplace = workplace;
}

public int getID() {
    return this.id;
}

public String getName() {
    return this. name;
}

public String getSurname() {
    return this.surname;
}

public String getDepartment() {
    return this.department;
}

public String getWorkplace() {
   return this.workplace;
}
}

编辑2:我的布局更改为这样.. enter image description here

最佳答案

如果您只想显示名称,请将您的 loadSpinnerData() 方法替换为此方法,否则请使用 Custome Adapter

private void loadSpinnerData() {
    //database handler
    LysandrosDatabaseAdapter db = new LysandrosDatabaseAdapter(getApplicationContext());
    //spinner drop down elements
    List<DataBean> list = db.getAllDat();
    //creating adapter for spinner
    String[] nameList=new String[list.size()];

    for(int i=0;i<list.size();i++){
        nameList[i]=list.get(i).getName(); //create array of name
    }
           ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, nameList);
    //drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);
}

关于android - 从 Spinner 中的 SQLite 加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501119/

相关文章:

c# - 分离时删除 Entity Framework Core 中的一比零或一相关数据 (sqlite)

android - 从 SQLite 数据库中删除重复项

android - 默认空微调器

android - 如何从 xml 动态地将数据添加到微调器中

android - 如何右对齐 Android 微调器

android - 使用切换按钮创建可扩展的 flutter

Android 从模拟器中的 Res/Raw 中逐行读取文本文件,OpenRawResource 上的 InvocationException

android - 设置 InfoWindowAdapter 仅显示单击第一个标记的信息

android - 如果互联网连接是错误的,它应该等待连接

c# - 查询多个表 SQLite Windows 10 UWP