It's already solved.
I want to add 3 data (date, location, and price) in my database. already solved some errors in the implementation of the database. but now it appears to me this error. I'm not very good at expressing myself on this topic, for the reason that I'm learning now and alone.
I have an error in line 41 (AddHistoria.addRegisto(new HistoricoCompras(data, local, preco));)
where this error appears ("addhistorico in AddHistorico cannot be aplied to (.com.example.a13731.ganhemais.historico de compras)")
in my activity
insert.
This is my insert activity (add):
package com.example.a13731.ganhemais;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
public class adiciona extends AppCompatActivity {
EditText edData,edLocal, edPreco;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adiciona);
Intent intent = getIntent();
final String value1 = intent.getStringExtra("key1");
final String value2 = intent.getStringExtra("key2");
edData = (EditText) findViewById(R.id.edit_data);
edLocal = (EditText) findViewById(R.id.edit_local);
edPreco = (EditText) findViewById(R.id.edit_preco);
}
public void inserirCompra(View v) {
AlertDialog.Builder dialogo = new AlertDialog.Builder(adiciona.this);
dialogo.setTitle("Aviso");
dialogo.setMessage("Inserir aluno?");
dialogo.setNegativeButton("Não", null);
dialogo.setPositiveButton("Sim", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String data = edData.getText().toString();
String local = edLocal.getText().toString();
String preco = edPreco.getText().toString();
AddHistoria.addRHistorico(new HistoricoCompras(data, local, preco));
AlertDialog.Builder d = new AlertDialog.Builder(adiciona.this);
d.setTitle("Aviso");
d.setMessage("Registo inserido!");
d.setNeutralButton("OK", null);
d.show();
edData.getText().clear();
edLocal.getText().clear();
edPreco.getText().clear();
}
});
}
My class (HistoricoCompras):
package com.example.a13731.ganhemais;
import java.io.Serializable;
/**
* Created by a13731 on 05/06/2017.
*/
public class HistoricoCompras implements Serializable {
private static String data;
private static String local;
private static String preco;
public HistoricoCompras(String data,String local,String preco)
{
this.data=data;
this.local=local;
this.preco=preco;
}
public void setdata(String data) {
this.data = data;
}
public void setlocal(String local) {
this.local = local;
}
public void setpreco(String preco) {
this.preco = preco;
}
public static String getData() {
return data;
}
public static String getLocal() {
return local;
}
public static String getPreco() {
return preco;
}
}
My database (DBhistorical):
package com.example.a13731.ganhemais; /**
* Created by a13731 on 05/06/2017.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBhistorico extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "GanheMais";
// table name
private static final String TABLE_historia = "Historia";
// Shops Table Columns names
private static final String KEY_Data = "data";
private static final String KEY_local = "local";
private static final String KEY_Preco = "preco";
public DBhistorico(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_historia_TABLE = "CREATE TABLE " + TABLE_historia + "(" + KEY_Data + " TEXT,"
+ KEY_local + " TEXT, " + KEY_Preco + "Text" + ")";
db.execSQL(CREATE_historia_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_historia);
// Creating tables again
onCreate(db);
}
// Adding new shop
public void addCompra(HistoricoCompras Historia) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_Data, HistoricoCompras.getData());
values.put(KEY_local, HistoricoCompras.getLocal());
values.put(KEY_Preco, HistoricoCompras.getPreco());
// Inserting Row
db.insert(TABLE_historia, null, values);
db.close(); // Closing database connection
}
}
isto cria uma array(ADDhISTORIA):
package com.example.a13731.ganhemais;
import java.util.ArrayList;
/**
* Created by a13731 on 05/06/2017.
*/
public final class AddHistoria {
private static ArrayList<Historico> aHistorico = new ArrayList<Historico>();
/*private addHistoria()
{
aHistorico = new ArrayList<Registo>();
}
*/
public static void addRHistorico(Historico r)
{ aHistorico.add(r); }
}