如何给微调器的位置零一个提示值?

时间:2014-08-22 02:57:47

标签: android android-sqlite android-spinner

数据库然后将数据传输到一个微调器,我想将位置0留空,这样我就可以向微调器添加一个没有值的项目使它看起来像一个提示符。我整天都在努力。失败后失败

MainActivity

public class MainActivity extends Activity {

Button AddBtn;
EditText et;
EditText cal;
Spinner spn;
SQLController SQLcon;
ProgressDialog PD;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AddBtn = (Button) findViewById(R.id.addbtn_id);
    et = (EditText) findViewById(R.id.et_id);
    cal = (EditText) findViewById(R.id.et_cal);
    spn = (Spinner) findViewById(R.id.spinner_id);

    spn.setOnItemSelectedListener(new OnItemSelectedListenerWrapper(
            new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> parent,
                        View view, int pos, long id) {

                    SQLcon.open();

                    Cursor c = SQLcon.readData();
                    if (c.moveToPosition(pos)) {

                        String name = c.getString(c
                                .getColumnIndex(DBhelper.MEMBER_NAME));
                        String calories = c.getString(c
                                .getColumnIndex(DBhelper.KEY_CALORIES));
                        et.setText(name);
                        cal.setText(calories);

                    }

                    SQLcon.close();
                    // closing database

                }

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

                }

            }));
    SQLcon = new SQLController(this);
    // opening database

    SQLcon.open();

    loadtospinner();

    AddBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            new MyAsync().execute();

        }
    });
}

public void loadtospinner() {

    ArrayList<String> al = new ArrayList<String>();

    Cursor c = SQLcon.readData();
    c.moveToFirst();
    while (!c.isAfterLast()) {

        String name = c.getString(c.getColumnIndex(DBhelper.MEMBER_NAME));
        String calories = c.getString(c
                .getColumnIndex(DBhelper.KEY_CALORIES));

        al.add(name + ", Calories: " + calories);

        c.moveToNext();
    }

    ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_spinner_item,
            al);

    spn.setAdapter(aa1);

    // closing database
    SQLcon.close();

}

private class MyAsync extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        PD = new ProgressDialog(MainActivity.this);
        PD.setTitle("Please Wait..");
        PD.setMessage("Loading...");
        PD.setCancelable(false);
        PD.show();

    }

    @Override
    protected Void doInBackground(Void... params) {
        String name = et.getText().toString();
        String calories = cal.getText().toString();
        // opening database
        SQLcon.open();
        // insert data into table

        SQLcon.insertData(name, calories);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        loadtospinner();
        PD.dismiss();

    }
}
}

数据库

public class SQLController {
private DBhelper dbhelper;
private Context ourcontext;
private SQLiteDatabase database;

public SQLController(Context c) {
    ourcontext = c;
}

public SQLController open() throws SQLException {
    dbhelper = new DBhelper(ourcontext);
    database = dbhelper.getWritableDatabase();
    return this;

}

public void close() {
    dbhelper.close();
}

public void insertData(String name, String calories) {
    ContentValues cv = new ContentValues();
    cv.put(DBhelper.MEMBER_NAME, name);

    cv.put(DBhelper.KEY_CALORIES, calories);
    database.insert(DBhelper.TABLE_MEMBER, null, cv);
}

public Cursor readData() {
    String[] allColumns = new String[] { DBhelper.MEMBER_ID,
            DBhelper.MEMBER_NAME, DBhelper.KEY_CALORIES };
    Cursor c = database.query(DBhelper.TABLE_MEMBER, allColumns, null,
            null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}
}

辅助

public class DBhelper extends SQLiteOpenHelper {

// TABLE INFORMATTION
public static final String TABLE_MEMBER = "member";
public static final String MEMBER_ID = "_id";
public static final String MEMBER_NAME = "name";
public static final String KEY_CALORIES = "calories";

// DATABASE INFORMATION
static final String DB_NAME = "MEMBER.DB";
static final int DB_VERSION = 2;

// TABLE CREATION STATEMENT
private static final String CREATE_TABLE = "create table " + TABLE_MEMBER
        + "(" + MEMBER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + MEMBER_NAME + " TEXT NOT NULL," + KEY_CALORIES
        + " INT NOT NULL);";

public DBhelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
    onCreate(db);
}
}

2 个答案:

答案 0 :(得分:2)

您在al ArrayList中从db获取数据。然后你可以做以下

al.add(0, "YOUR MESSAGE");    

它在0th索引处添加了您的MESSAGE字符串。

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

之后将列表传递给arrayadapter

ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_spinner_item,
            al);

    spn.setAdapter(aa1);

请检查ArrayList

修改

这是代码

public void loadtospinner() {

    ArrayList<String> al = new ArrayList<String>();

    Cursor c = SQLcon.readData();
    c.moveToFirst();
    while (!c.isAfterLast()) {

        String name = c.getString(c.getColumnIndex(DBhelper.MEMBER_NAME));
        String calories = c.getString(c
                .getColumnIndex(DBhelper.KEY_CALORIES));

        al.add(name + ", Calories: " + calories);

        c.moveToNext();
    }

    al.add(0, "YOUR MESSAGE");    // do this after while loop and that's it. 


    ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
            getApplicationContext(), android.R.layout.simple_spinner_item,
            al);

    spn.setAdapter(aa1);

    // closing database
    SQLcon.close();

}

答案 1 :(得分:1)

使用以下内容:

Spinner spinner = (Spinner) findViewById(R.id.yourSpinner);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray); 
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);

<强>更新

您的 spinnerArray 应具有数据库中的值,第一个值为提示

例如:

List<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("YOU MESSAGE");

然后您可以使用 for loop 将值添加到 spinnerArray ,如下所示:

for(//loop until you need values to be added)

将值添加到spinnerArray中:

spinnerArray.add(value1);
spinnerArray.add(value1);
spinnerArray.add(value1);
//so on.

最后在创建 spinnerArrayAdapter 时将其作为最后一个参数传递。这应该可以解决你的问题。

<强> UPDATE1:

如果您不想在0号位置获得项目,可以执行以下操作:

if(spinner.getSelectedItem().equals("YOUR MESSAGE"){//may be you want to ignorecase using equalsIgnoreCase() method
//display message that you haven't selected anything
}else{
//do anything you want
}

填充虚拟数据:

将DBhelper类中的 onCreate()更改为:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE);

    ContentValues cv = new ContentValues();
        cv.put(DBhelper.MEMBER_NAME, "DUMMY NAME");

        cv.put(DBhelper.KEY_CALORIES, "DUMMY CALORIES");
        database.insert(DBhelper.TABLE_MEMBER, null, cv);
}