使用ArrayList和HashMap使用数据库中的数据填充Spinner

时间:2016-01-08 17:09:39

标签: android arraylist hashmap spinner

我需要帮助填充Spinner,使用ArrayList中的值。

通过从数据库表中读取值来填充ArrayList:

public ArrayList<HashMap<String, String>> getStudents() {
    //Open connection to read only
    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery =  "select * from " + TABLE_STUDENTS;

    //Student student = new Student();
    ArrayList<HashMap<String, String>> studentList = new ArrayList<HashMap<String, String>>();

    Cursor cursor = db.rawQuery(selectQuery, null);
    // looping through all rows and adding to list

    if (cursor.moveToFirst()) {
        do {
            HashMap<String, String> student = new HashMap<String, String>();
            student.put("id", cursor.getString(cursor.getColumnIndex(Student.KEY_ID)));
            student.put("name", cursor.getString(cursor.getColumnIndex(Student.KEY_name)));
            studentList.add(student);

        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();
    return studentList;

}

所以,我需要如何用选定的值填充Spinner。我尝试过这样的事情:

    mydb = new DBHelper(this);

    ArrayList<HashMap<String, String>> studentList =  mydb.getStudents();

            // and this is where I have the problem:
    ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, studentList);

请问您如何填充Spinner?提前谢谢!

4 个答案:

答案 0 :(得分:0)

您正在使用

#

为你的微调器创建适配器,注意 ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, studentList); 是android系统中的布局,它在一行上有一个TextView,所以你不能给出一个类型为android.R.layout.simple_spinner_item的列表作为数据,要修复它,您可以改为使用ArrayList<HashMap<String, String>>或使用扩展SimpleArrayAdapter的自定义适配器。

答案 1 :(得分:0)

您必须创建一个扩展ArrayAdapter的新类AdapterStudents。假设您的微调器行的布局包含两个TextView,即学生ID的txtViewId和Name的txtViewName,您可以使用以下代码:

public class AdapterStudent extends ArrayAdapter<String> {
    private Context context;
    private int layoutResourceId;
    private ArrayList<HashMap<String, String>> studentList;

    public AdapterStudent(Context context, int layoutResourceId, ArrayList<HashMap<String, String>> studentList) {
        super(context, layoutResourceId, arrayCountries);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.studentList = studentList;
    }

    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        // TODO Auto-generated method stub
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        //return super.getView(position, convertView, parent);

        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {

            LayoutInflater inflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();

            holder.txtViewId = (TextView) row.findViewById(R.id.txtViewId);
            holder.txtViewNam = (TextView) row.findViewById(R.id.txtViewName);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        holder.txtViewId.setText(studentList.get(i).get("id"));
        holder.txtViewName.setText(studentList.get(i).get("name"));

        return row;
    }

    static class ViewHolder {
        TextView txtViewId;
        TextView txtViewName;
    }


}

在包含微调器的主活动中,您需要调用

Spinner spinnerStudents = (Spinner) findViewById(R.id.spinnerlayoutname)
spinnerlayoutname.setAdapter(new AdapterStudent(getApplicationContext(), R.layout.spinnerrowlayoutid, studentList));

有了这个,你应该设置。已经在我的应用程序中运行的代码进行了调整,很高兴解释您是否需要对此进行更多说明。

答案 2 :(得分:0)

您可以实现从CursorAdapter扩展的适配器:

public class SpinnerAdapter extends CursorAdapter
{
    public SpinnerAdapter(Context context, Cursor cursor)
    {
        super(context, cursor, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent)
    {
        return LayoutInflater.from(context).inflate(R.layout.spinner_layout, parent, false);
    }

    @Override
    public void bindView(View rowView, Context context, Cursor cursor)
    {
        // Find fields to populate in inflated template
        TextView textView = (TextView) rowView.findViewById(R.id.spinner_layout_text_view); // This is your textview inside the layout that your spinner will use

        // Extract properties from cursor
        String studentName = cursor.getString(cursor.getColumnIndexOrThrow(Student.KEY_name));

        // Populate fields with extracted properties
        textView.setText(studentName);
    }
}

然后,初始化微调器(onCreate on Activity)

// Get a reference from your Spinner
Spinner spinner = (Spinner) findViewById(R.id.your_spinner);

// Get your cursor from a Db.
Cursor cursor = Get_Cursor_From_Db(); // Implement your logic and return a Cursor

// Create your adapter and tell it how is the cursor
SpinnerAdapter adapter = new SpinnerAdapter(this, cursor);

// Set the adapter to the Spinner.
spinner.setAdapter(adapter);

使用这种方式,您不必担心这个HashMap。

我希望这有帮助。

答案 3 :(得分:0)

为自定义适配器项创建自定义适配器类: 的 CustomAdapter.java

private class CustomAdapter extends ArrayAdapter {

    private Context context;
    ArrayList<HashMap<String, String>> studentList;

    public CustomAdapter(Context context, ArrayList<HashMap<String, String>> studentList) {

        super(context, textViewResourceId);
        this.context=context;
        this.studentList=studentList;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

       LayoutInflater inflater = getLayoutInflater();
       View row = inflater.inflate(R.layout.custom_spinner_item, parent,
                    false);
        // get your views here and set values to them

        TextView textview = (TextView) row.findViewById(R.id.textview);

        HashMap<String, String> rowItem = studentList.get(position);

        String value = (String) rowItem.get(key);

        textview.setText(value);

        return row;
    }


    public View getDropDownView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.custom_spinner_item, parent,
                        false);

        TextView textview = (TextView) row.findViewById(R.id.textview);

        HashMap<String, String> rowItem = studentList.get(position);

        String value = (String) rowItem.get(key);

        textview.setText(value);

        return row;
    }
}

还创建一个xml文件。

<强> custom_spinner_item.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    />

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textview"
    />

</LinearLayout>

然后使用这样的东西:

ArrayList<HashMap<String, String>> studentList =  mydb.getStudents();

        // and this is where I have the problem:
CustomAdapter adapter = new CustomAdapter(this, studentList);
spinner.setAdapter(adapter);