Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用ArrayList和HashMap使用数据库中的数据填充微调器_Android_Arraylist_Hashmap_Spinner - Fatal编程技术网

Android 使用ArrayList和HashMap使用数据库中的数据填充微调器

Android 使用ArrayList和HashMap使用数据库中的数据填充微调器,android,arraylist,hashmap,spinner,Android,Arraylist,Hashmap,Spinner,我需要使用ArrayList中的值填充微调器的帮助 ArrayList通过从数据库表中读取值来填充: public ArrayList<HashMap<String, String>> getStudents() { //Open connection to read only SQLiteDatabase db = this.getReadableDatabase(); String selectQuery = "select * from "

我需要使用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;

}
public ArrayList getStudents(){
//打开只读连接
SQLiteDatabase db=this.getReadableDatabase();
String selectQuery=“select*from”+表\u学生;
//学生=新生();
ArrayList studentList=新建ArrayList();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
HashMap student=新HashMap();
put(“id”,cursor.getString(cursor.getColumnIndex(student.KEY_id));
put(“name”,cursor.getString(cursor.getColumnIndex(student.KEY_name));
学生列表。添加(学生);
}while(cursor.moveToNext());
}
cursor.close();
db.close();
返回学生名单;
}
所以,我需要如何用选定的值填充微调器。我试过这样的方法:

    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);
mydb=newdbhelper(此);
ArrayList studentList=mydb.getStudents();
//这就是我的问题所在:
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项,学生列表);
你能帮我怎么填充旋转器吗?提前谢谢你

您正在使用

 ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, studentList);
ArrayAdapter adapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器项目,studentList);

要为您的微调器创建适配器,请注意,
android.R.layout.simple\u spinner\u item
是android系统中的一个布局,它在一行上有一个文本视图,因此您不能将类型为
ArrayList
的列表作为数据提供,以修复它,您可以使用
SimpleArrayAdapter
,也可以通过扩展
BaseAdapter
使用自定义适配器。您必须创建一个新的类适配器Students来扩展ArrayAdapter。假设微调器行的布局包含两个文本视图,即txtViewId表示学生Id,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;
    }


}

有了这个,你就应该准备好了。我已经根据我的应用程序中的运行代码改编了以上内容,如果您需要更多的说明,欢迎解释

您可以实现从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)

使用这种方式,您不需要担心这个HashMap


我希望这有帮助。

为自定义适配器项创建自定义适配器类: 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;
    }
}
私有类CustomAdapter扩展了ArrayAdapter{
私人语境;
ArrayList学生名单;
公共CustomAdapter(上下文上下文,ArrayList studentList){
super(上下文,textViewResourceId);
this.context=context;
this.studentList=studentList;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气机=getLayoutInflater();
视图行=充气机。充气(R.layout.custom\u微调器\u项,父项,
假);
//在此处获取视图并为其设置值
TextView TextView=(TextView)row.findViewById(R.id.TextView);
HashMap rowItem=studentList.get(位置);
字符串值=(字符串)rowItem.get(键);
textview.setText(值);
返回行;
}
公共视图getDropDownView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气机=getLayoutInflater();
视图行=充气机。充气(R.layout.custom\u微调器\u项,父项,
假);
TextView TextView=(TextView)row.findViewById(R.id.TextView);
HashMap rowItem=studentList.get(位置);
字符串值=(字符串)rowItem.get(键);
textview.setText(值);
返回行;
}
}
还要创建一个xml文件

自定义微调器\u 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);
ArrayList studentList=mydb.getStudents();
//这就是我的问题所在:
CustomAdapter=新的CustomAdapter(此,学生列表);
旋转器。设置适配器(适配器);

您现在有什么问题?
<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);