Android动态填充Spinner

时间:2014-08-29 07:35:32

标签: android sql spinner android-arrayadapter

我正在尝试从我从数据库中检索的数据中填充Android微调器。这是我的SQL语句来检索数据:

private ArrayList<CategoryModel> catList = new ArrayList<CategoryModel>();
public ArrayList <CategoryModel> getAllCatName() {
    try {
        String sql = "SELECT categoryName FROM category";
        Cursor mCur = mDb.rawQuery(sql, null);
        Log.e(TAG, "Data Grab Success");
        if (mCur.getCount() != 0) {
            if (mCur.moveToFirst()) {
                do {
                    CategoryModel cm = new CategoryModel(); 
                    cm.setCategoryName((mCur.getString(mCur
                            .getColumnIndex("categoryName"))));

                    catList.add(cm);
                } while (mCur.moveToNext());
            }
        }
        return catList;
    } catch (SQLException mSQLException) {
        throw mSQLException;
    }
}

以下是我尝试将数据绑定到微调器的代码:

    spinnerCat = (Spinner) dialogView.findViewById(R.id.spinnerCat);

    ArrayList<CategoryModel> cat_list = cc.getAllCatName();
    ArrayAdapter<CategoryModel> adapterFrom = new ArrayAdapter<CategoryModel>(this,
            android.R.layout.simple_spinner_item, cat_list) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);

            // Get item at position
            CategoryModel catModel = getItem(position);
            // Set textview's value with the category name
            TextView textView = (TextView) view.findViewById(android.R.id.text1);
            textView.setText(catModel.getCategoryName());

            return view;
        }
    };

    adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCat.setAdapter(adapterFrom);

在我的实体课中:

public String categoryName;
public String getCategoryName() {
    return categoryName;
}

public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
}

然而,现在的问题是我检索的第一个项似乎工作正常。但是微调器中的以下项目似乎采用这种格式Entity.CategoryModel@61480167。我想知道为什么会这样。

提前致谢。

修改

DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
    mDbHelper.createDatabase();
    mDbHelper.open();
    CategoryController cc = new CategoryController(
            mDbHelper.open());

    Cursor cursor = cc.getAllCatName();
    if(cursor.getCount()>0){
           String[] from = new String[]{"categoryName"};
           // create an array of the display item we want to bind our data to
           int[] to = new int[]{android.R.id.text1};
           SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item,
             cursor, from, to);
           mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
           spinnerCat.setAdapter(mAdapter);
          }

我的SQL语句方法:

public Cursor getAllCatName() {
    try {
        String sql = "SELECT categoryName FROM category";
        Cursor mCur = mDb.rawQuery(sql, null);
        Log.e(TAG, "Data Grab Success");
        if (mCur.getCount() != 0) {
            if (mCur.moveToFirst()) {
                do {
                    CategoryModel cm = new CategoryModel(); 
                    cm.setCategoryName((mCur.getString(mCur
                            .getColumnIndex("categoryName"))));

                    catList.add(cm);
                } while (mCur.moveToNext());
            }
        }
        return mCur;
    } catch (SQLException mSQLException) {
        throw mSQLException;
    }
}

并且新的错误消息是列_id不存在。我真的不知道为什么会这样,因为我从未在任何地方声明任何_id。

2 个答案:

答案 0 :(得分:0)

您需要自定义适配器,因为您的数组(cat_list)包含自定义对象。

ArrayAdapter<CategoryModel> adapterFrom = new ArrayAdapter<CategoryModel>(this,
        android.R.layout.simple_spinner_item, cat_list) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        // Get item at position
        CategoryModel catModel = getItem(position);
        // Set textview's value with the category name
        TextView textView = (TextView) view.findViewById(android.R.id.text1);

        textView.setText(catModel.getCategoryName());

        return view;
    }
};

adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCat.setAdapter(adapterFrom);

答案 1 :(得分:0)

ArrayList<String> Temp = new ArrayList<String> ();
for (int i = 0; i < cat_list.size(); i++)
{
  Temp.add(cat_list.get(i).getCategoryName());
}

adapterFrom = new ArrayAdapter(this,android.R.layout.simple_spinner_item,                                           textview的id,Temp);   adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_ite米);
  spinnerCat.setAdapter(adapterFrom);

    spinnerCat.setOnItemSelectedListener
    (new OnItemSelectedListener() 
       {
        @Override
        public void onItemSelected(AdapterView<?> parent,
                View view, int position, long id)
        {
            Toast.makeText(getApplicationContext(), ""+position, Toast.LENGTH_LONG).show();
            Toast.makeText(getApplicationContext(), ""+Temp.get(position),  
            Toast.LENGTH_LONG).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            // TODO Auto-generated method stub

        }
    });