【问题标题】:Load data from SQLite in Spinner在 Spinner 中从 SQLite 加载数据
【发布时间】:2018-08-29 15:14:28
【问题描述】:

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

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

我的活动:

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:我的布局更改为这个..

【问题讨论】:

  • custom adapter,因为你的list不是string array它的custom list
  • 你能再解释一下吗?我是新人
  • 请发布您的 DataBean 类

标签: android sqlite spinner


【解决方案1】:

如果您只想显示名称,请使用此方法替换您的 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);
}

【讨论】:

  • 我真的很接近,但请您进一步解释一下如何创建名称数组?
  • 等一下,我正在研究一些你的代码可能有用的教程。
  • 检查 nameList 是从您的 DataBean list 中提取的名称数组
  • 好吧,我也试试这个,然后回复你
  • 您的解决方案有效!但是我的布局搞砸了:/我会更新我的问题,请检查一下
【解决方案2】:

创建扩展 ArrayAdapter 的自定义适配器。

public class MyAdapter extends ArrayAdapter<DataBean>{

Context context; 
int layoutResourceId;    
ArrayList<DataBean> data;

public MyAdapter(Context context, int layoutResourceId, ArrayList<DataBean> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ViewHolder();
        holder.persoName = (TextView)row.findViewById(R.id.persoName);
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)row.getTag();
    }

    holder.persoName.setText(list.get(position).getName());

    return row;
}

static class ViewHolder
{
    TextView persoName;
}
}

这里是微调器列表项的 xml 文件。

spinner_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >

<TextView
    android:id="@+id/persoName"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:layout_weight="70"
    android:text="Name"
    android:textColor="#000000"
    android:textSize="22sp"
    android:textStyle="bold" />

</LinearLayout>

这里我只给出了一个数据字符串的例子。

然后像这样使用它。

private void loadSpinnerData() {
    //database handler
    LysandrosDatabaseAdapter db = new LysandrosDatabaseAdapter(getApplicationContext());
    //spinner drop down elements
    ArrayList<DataBean> list = db.getAllDat();
    //creating adapter for spinner
    MyAdapter<DataBean > dataAdapter = new MyAdapter<DataBean>(this, R.layout.spinner_list_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);
}

你可以参考这个link来检查如何使用自定义适配器。

【讨论】:

  • 感谢您的意见我会开始尝试这个
  • ty 也是为了将来会派上用场的代码
【解决方案3】:

您也可以简单地覆盖 DataBean 类的 toString 方法,并返回要在 Spinner 中显示的文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 2015-12-28
    • 2011-07-01
    相关资源
    最近更新 更多