将数据从sqlite数据库和集合加载到微调器

时间:2014-06-09 09:11:21

标签: android sqlite spinner

我想做什么 ::我正在尝试从Spinner数据库

中通过cursor获取的数据中填充sqlite

我做了什么 ::我可以从sqlite中获取数据并将其存储在ArrayList<HashMap<String,String>>

问题 ::如何将此集合中的数据加载到我的微调器


代码 ::

     Spinner CitySpinner;

    private HashMap<String, String> objHashCityName;

    private ArrayList<HashMap<String, String>> objListCityName=null;

private void setDataForCity() {
        DatabaseHandler mHelper;
        SQLiteDatabase db = null;
        Cursor mCursor = null;
        try {
            mHelper = new DatabaseHandler(getActivity());
            db = mHelper.getReadableDatabase();
            mCursor = db.rawQuery("select city_name from "
                    + city_mas.TABLE_NAME_CITY_MAS, null);
            if(mCursor.moveToFirst()){
                do{
                    objHashCityName = new HashMap<String, String>();
                    objHashCityName.put("city_name", mCursor.getString(0));
                    //objHashBufType to array list (One row i each iteration)
                    objListCityName.add(objHashCityName);
                }while(mCursor.moveToNext());
                Log.d("", "");
            }
        } catch (Exception e) {
            e.printStackTrace();

        }finally{
            if(db!=null){
                if(db.isOpen()) db.close();
            }
            if(mCursor!=null){
                if(!mCursor.isClosed())mCursor.close();
            }
        }

2 个答案:

答案 0 :(得分:0)

enter image description here

图片来源:Androidhive

public class AndroidSpinnerFromSQLiteActivity extends Activity implements
        OnItemSelectedListener {

    // Spinner element
    Spinner spinner;

    // Add button
    Button btnAdd;

    // Input text
    EditText inputLabel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        // add button
        btnAdd = (Button) findViewById(R.id.btn_add);

        // new label input field
        inputLabel = (EditText) findViewById(R.id.input_label);

        // Spinner click listener
        spinner.setOnItemSelectedListener(this);

        // Loading spinner data from database
        loadSpinnerData();

        /**
         * Add new label button click listener
         * */
        btnAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String label = inputLabel.getText().toString();

                if (label.trim().length() > 0) {
                    // database handler
                    DatabaseHandler db = new DatabaseHandler(
                            getApplicationContext());

                    // inserting new label into database
                    db.insertLabel(label);

                    // making input filed text to blank
                    inputLabel.setText("");

                    // Hiding the keyboard
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);

                    // loading spinner with newly added data
                    loadSpinnerData();
                } else {
                    Toast.makeText(getApplicationContext(), "Please enter label name",
                            Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    /**
     * Function to load the spinner data from SQLite database
     * */
    private void loadSpinnerData() {
        // database handler
        DatabaseHandler db = new DatabaseHandler(getApplicationContext());

        // Spinner Drop down elements
        List<String> lables = db.getAllLabels();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, lables);

        // 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 label = parent.getItemAtPosition(position).toString();

        // Showing selected spinner item
        Toast.makeText(parent.getContext(), "You selected: " + label,
                Toast.LENGTH_LONG).show();

    }

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

    }
}

完整教程可从androidhive

获取

答案 1 :(得分:0)

这就是我解决这个问题的方法 ::

private void setDataForCity() {
        DatabaseHandler mHelper;
        SQLiteDatabase db = null;
        Cursor mCursor = null;
        List<String> objCitySpinner;

        try {
            objCitySpinner=new ArrayList<String>();
            mHelper = new DatabaseHandler(getActivity());
            db = mHelper.getReadableDatabase();
            mCursor = db.rawQuery("select city_name from "
                    + city_mas.TABLE_NAME_CITY_MAS, null);

            //First city in the spinner must always should be current city
            objCitySpinner.add(AppController.currentCity);

            if(mCursor.moveToFirst()){
                do{
                    objCitySpinner.add(mCursor.getString(0));
                    //objHashBufType to list (One row i each iteration)
                }while(mCursor.moveToNext());
                Log.d("", "");
            }
            //I am using ArrayAdapter to populate the data to the spinner
            ArrayAdapter<String> spinCityAdapt = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_dropdown_item, objCitySpinner);
            CitySpinner.setAdapter(spinCityAdapt);
        } catch (Exception e) {
            e.printStackTrace();

        }finally{
            if(db!=null){
                if(db.isOpen()) db.close();
            }
            if(mCursor!=null){
                if(!mCursor.isClosed())mCursor.close();
            }
        }
    }