我的MainActivity中出现错误。产品中的 产品()无法应用于(java.lang.String) 这是代码
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import apps.fsc.foodtrackertuesday.R;
// This program exists solely to generate a database and play with it.
public class MainActivity extends AppCompatActivity {
EditText userEntryText;
TextView resultsText;
myDBManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Grab references to UI elements
userEntryText = findViewById(R.id.userEntryText);
resultsText = findViewById(R.id.resultsText);
// dbManager reference
dbManager = new myDBManager(this, null, null, 1);
// Let's see what's in the database.
printDatabase();
}
public void printDatabase()
{
// Query the database using the dbManager's dbToString() method.
String dbString = dbManager.dbToString();
// Drop the results onto the UI
resultsText.setText(dbString);
// Set the UI up for fresh input
userEntryText.setText("");
}
public void addFRUITButtonClick(View view)
{
// Create a new product based on what's in the UI
Products products = new Products(userEntryText.getText().toString());
// Send that product object to the dbManager so the item can be added.
dbManager.addFRUITItem(products);
// See what's in the database
printDatabase();
}
public void delFRUITButtonClick(View view)
{
// Grab the UI contents and dump it into a string
String inputText = userEntryText.getText().toString();
// Send that string to the dbManager's delItem() method.
dbManager.delFRUITItem(inputText);
// See what's in the database
printDatabase();
}
}
这是MyDBManager
package apps.fsc.foodtrackertuesday;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class myDBManager extends SQLiteOpenHelper{
// Define some global database stuff
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "database";
// Define a table and some columns
private static final String TABLE_NAME = "products";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_PRODUCT_FRUIT = "productfruit";
private static final String COLUMN_PRODUCT_DAIRY = "productdairy";
private static final String COLUMN_PRODUCT_GRAINS = "productgrains";
private static final String COLUMN_PRODUCT_VEGETABLES = "productvegetables";
private static final String COLUMN_PRODUCT_PROTEIN = "productprotein";
public myDBManager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
//super(context, name, factory, version); // original super.
super(context, DATABASE_NAME, factory , DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// put together a sql string to create the table
String query = "CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCT_FRUIT + " VARCHAR(255) " +
COLUMN_PRODUCT_DAIRY + " VARCHAR(255) " +
COLUMN_PRODUCT_GRAINS + " VARCHAR(255) " +
COLUMN_PRODUCT_VEGETABLES + " VARCHAR(255) " +
COLUMN_PRODUCT_PROTEIN + " VARCHAR(255) " +
");";
// execute the sql command
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// delete the existing database
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// call onCreate
onCreate(db);
}
// Add item to database
public void addFRUITItem(Products _productFRUIT){
// get reference to the database
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
// Associate column name with value
values.put(COLUMN_PRODUCT_FRUIT, _productFRUIT.get_productFRUIT());
// insert value into table at the appropriate location
db.insert(TABLE_NAME, null, values);
// done writing
db.close();
}
public void addDAIRYItem(Products product_dairy){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCT_DAIRY, product_dairy.get_productFRUIT());
db.insert(TABLE_NAME, null, values);
db.close();
}
public void addVEGETABLESItem(Products product_vegetables){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCT_VEGETABLES, product_vegetables.get_productFRUIT());
db.insert(TABLE_NAME, null, values);
db.close();
}
public void addGRAINSItem(Products product_grain){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCT_GRAINS, product_grain.get_productFRUIT());
db.insert(TABLE_NAME, null, values);
db.close();
}
public void addPROTEINItem(Products product_protein){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCT_PROTEIN, product_protein.get_productFRUIT());
db.insert(TABLE_NAME, null, values);
db.close();
}
// Delete an item from the database
public void delDAIRYItem(String product_dairy){
// Get database reference
SQLiteDatabase db = getWritableDatabase();
// build the sql query
String query = "DELETE FROM " + TABLE_NAME +
" WHERE " + COLUMN_PRODUCT_DAIRY +
"=\"" + product_dairy + "\";";
//execute the query
db.execSQL(query);
}
public void delFRUITItem(String product_fruit){
// Get database reference
SQLiteDatabase db = getWritableDatabase();
// build the sql query
String query = "DELETE FROM " + TABLE_NAME +
" WHERE " + COLUMN_PRODUCT_FRUIT +
"=\"" + product_fruit + "\";";
//execute the query
db.execSQL(query);
}
public void delGRAINStem(String product_grains){
// Get database reference
SQLiteDatabase db = getWritableDatabase();
// build the sql query
String query = "DELETE FROM " + TABLE_NAME +
" WHERE " + COLUMN_PRODUCT_GRAINS +
"=\"" + product_grains + "\";";
//execute the query
db.execSQL(query);
}
public void delVEGETABLESItem(String product_vegetables){
// Get database reference
SQLiteDatabase db = getWritableDatabase();
// build the sql query
String query = "DELETE FROM " + TABLE_NAME +
" WHERE " + COLUMN_PRODUCT_VEGETABLES +
"=\"" + product_vegetables + "\";";
//execute the query
db.execSQL(query);
}
public void delPROTEINItem(String product_protein){
// Get database reference
SQLiteDatabase db = getWritableDatabase();
// build the sql query
String query = "DELETE FROM " + TABLE_NAME +
" WHERE " + COLUMN_PRODUCT_PROTEIN +
"=\"" + product_protein + "\";";
//execute the query
db.execSQL(query);
}
public String dbToString()
{
// Get a reference to the database
SQLiteDatabase db = getWritableDatabase();
String dbString = "";
// Build a sql query
String query = "SELECT * FROM " + TABLE_NAME + " WHERE 1";
// Point the cursor at the result of the executed query
Cursor c = db.rawQuery(query, null);
// Start at the beginning
c.moveToFirst();
// Iterate through the rows until the end is reached
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex(COLUMN_ID))!=null){
// dbString += c.getString(c.getColumnIndex(COLUMN_ID));
dbString += " ";
dbString += c.getString(c.getColumnIndex(COLUMN_PRODUCT_FRUIT));
dbString += "\n";
}
// point to the next row
c.moveToNext();
}
// close the database
db.close();
// send back the data
return dbString;
}
}
这是我的产品表
package apps.fsc.foodtrackertuesday;
公共类产品 {
private int _id;
private String _productFRUIT;
private String _productDAIRY;
private String _productGRAINS;
private String _productPROTEIN;
private String _productVEGETABLES;
public void _productFRUIT(String _productFRUIT)
{
this._productFRUIT = _productFRUIT;
}
public void _productDAIRY(String _productDAIRY)
{
this._productDAIRY = _productDAIRY;
}
public void _productGRAINS(String _productGRAINS)
{
this._productGRAINS = _productGRAINS;
}
public void _productPROTEIN(String _productPROTEIN)
{
this._productPROTEIN = _productPROTEIN;
}
public void _productVEGETABLES(String _productVEGETABLES)
{
this._productVEGETABLES = _productVEGETABLES;
}
//Getters
public int get_id()
{
return _id;
}
public String get_productFRUIT()
{
return _productFRUIT;
}
public String get_productGRAINS()
{
return _productGRAINS;
}
public String get_productVEGETABLES()
{
return _productVEGETABLES;
}
public String get_productPROTEIN()
{
return _productPROTEIN;
}
public String get_productDAIRY()
{
return _productDAIRY;
}
//Setters
public void set_id(int _id) {
this._id = _id;
}
public void set_productFRUIT(String _productFRUIT)
{
this._productFRUIT = _productFRUIT;
}
public void set_productDAIRY(String _productDAIRY)
{
this._productDAIRY = _productDAIRY;
}
public void set_productGRAINS(String _productGRAINS)
{
this._productGRAINS = _productGRAINS;
}
public void set_productVEGETABLES(String _productVEGETABLES)
{
this._productVEGETABLES = _productVEGETABLES;
}
public void set_productPROTEIN(String _productPROTEIN)
{
this._productPROTEIN = _productPROTEIN;
}
}
为什么会发生这种情况?如何解决这个问题?
一点背景:
它是一个应用程序,用于存储在杂货店中可以获得的东西
它被细分为食物组。
答案 0 :(得分:1)
在产品表
上public Products(String productName) {
this._productFRUIT = productName;
}