无法将Location设置为String

时间:2014-05-21 03:26:17

标签: android database sqlite

嗨我不知道为什么我的一个列表上出现错误。它表示我的“位置”不能用字符串。此外,我硬编码我的位置和地址,因为我不需要用户键入任何地址(我把它放在一个微调器的形式)。所以我尝试将我的“位置”和“地址”硬编码到我的SQLite数据库中。我将其他Spinners存储到数据库中没有问题。所以请帮帮我!

将对DBAdapter:

public class DBAdapter {

    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "powerfood2014";
    private static final int DATABASE_VERSION = 2;

public static final String KEY_RESTID = "locationId";
    public static final String KEY_LOCATION = "location";
    private static final String LOCATION_TABLE = "location";

private static final String CREATE_LOCATION =
            "create table if not exists location (locationId integer primary key autoincrement, " + "location VARCHAR);";

private final Context context;

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

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

  @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            db.execSQL("DROP TABLE IF EXISTS " + CREATE_LOCATION);
            onCreate(db);
        }
    }
//---opens the database---
    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---
    public void close()
    {
        DBHelper.close();
    }
 public long insertLocation(String location, String address) {

        ContentValues initialValues = new ContentValues();

        initialValues.put(KEY_LOCATION, "Somerset 313 Powerfood");
        db.insert(LOCATION_TABLE, null, initialValues);

        initialValues.put(KEY_LOCATION, "Vivo City Powerfood");
        db.insert(LOCATION_TABLE, null, initialValues);

        //insert row
        long locationId = db.insert(LOCATION_TABLE, null, initialValues);

        return locationId;
    }
}

这是我的位置类:

public class Location extends Activity {
Spinner spinner1;
    private Button btnSubmit;
    DBAdapter db = new DBAdapter(this);

public void buttonOnClickL(View v) {

        Log.d("test", "adding");
        //get data from form
        Spinner location = (Spinner)findViewById(R.id.spinner1);

        db.open();
        long id = db.insertLocation(location.getSelectedItem().toString());
        db.close();


        Toast.makeText(Location.this, "Customer successfully registered", Toast.LENGTH_LONG).show();

        startActivity(new Intent(getApplicationContext(), HomePage.class));


    }
}

logcat的:

    error: method insertLocation in class DBAdapter cannot be applied to given types;
    required: String,String
    found: String
    reason: actual and formal argument lists differ in length

错误:类Adapter中的方法setOnItemSelectedListener无法应用于给定的类型;     必需:OnItemSelectedListener     发现:位置     reason:实际参数位置无法通过方法调用转换转换为OnItemSelectedListener     其中T是一个类型变量:     T扩展了AdapterView类中声明的Adapter

1 个答案:

答案 0 :(得分:1)

与日志一样:

  

错误:类DBAdapter中的方法insertLocation无法应用于   给定类型;

因为insertLocation方法接受两个String参数而不是一个,并且在从Activity调用insertLocation方法时只传递一个参数。

    String str_location=location.getSelectedItem().toString();
    long id = db.insertLocation(str_location,<PASS_SECOND_PARAM_HERE>);