Вы находитесь на странице: 1из 10

public class MainActivity extends AppCompatActivity {

Button despreButton;
Button adaugareButton;
Button reviziiButton;

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

despreButton= (Button) findViewById(R.id.btnDespre);


adaugareButton = (Button) findViewById(R.id.btnAdaugare);
reviziiButton = (Button) findViewById(R.id.btnRevizii);

Button.OnClickListener reviziiListener = new View.OnClickListener(){

@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this,ReviziiActivity.class);
startActivity(intent);
}
};

Button.OnClickListener despreListener = new View.OnClickListener(){

@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Mircea Nicoleta 26 Ian 2016
12:57",Toast.LENGTH_LONG).show();
}
};

Button.OnClickListener adaugareListener = new View.OnClickListener(){

@Override
public void onClick(View view) {
Intent intent =new Intent (MainActivity.this,AdaugareActivity.class);
startActivity(intent);
}
};

despreButton.setOnClickListener(despreListener);
adaugareButton.setOnClickListener(adaugareListener);
reviziiButton.setOnClickListener(reviziiListener);
}

=====================================================================================
=====
public class AdaugareActivity extends AppCompatActivity {

Button adaugaRevizie;
public static List<Revizie> listaRevizii;
EditText etNumarAuto;
EditText etNumarKm;
EditText etCost;
EditText etData;
Spinner spinner;
String numarAuto;
int numarKm;
int cost;
Date data;
String tipString;
DatabaseHandler db;

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

db = new DatabaseHandler(this);
adaugaRevizie = (Button) findViewById(R.id.btnAdauga);
listaRevizii = new ArrayList<Revizie>();
etNumarAuto = (EditText) findViewById(R.id.etNumarAuto);
etNumarKm = (EditText) findViewById(R.id.etNumarKm);
etCost = (EditText) findViewById(R.id.etCost);
etData = (EditText) findViewById(R.id.etData);
spinner = (Spinner) findViewById(R.id.spinner);

Button.OnClickListener adaugaListener = new View.OnClickListener(){

@Override
public void onClick(View view) {

numarAuto = etNumarAuto.getText().toString();
numarKm = Integer.parseInt(etNumarKm.getText().toString());

SimpleDateFormat format = new SimpleDateFormat("dd-mm-yyyy");


try {
data = format.parse(etData.getText().toString());
} catch (ParseException e) {
e.printStackTrace();
}
cost = Integer.parseInt(etCost.getText().toString());
if (tipString != null) {
Revizie revizie = new Revizie(numarAuto, numarKm, tipString, data, cost);
listaRevizii.add(revizie);
Log.d("Insert: ", "Inserting ..");
db.addRevizie(revizie);
Toast.makeText(AdaugareActivity.this,"Revizia a fost adaugata
!",Toast.LENGTH_SHORT).show();
etNumarAuto.getText().clear();
etNumarKm.getText().clear();
etData.getText().clear();
etCost.getText().clear();
spinner.setSelection(0);

}
else
{
Toast.makeText(AdaugareActivity.this,"Selectati tipul reviziei
!",Toast.LENGTH_SHORT).show();
}

};
Spinner.OnItemSelectedListener onItemSelectedListener = new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView editText = (TextView) view;
tipString = editText.getText().toString();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
};

String[] array = getResources().getStringArray(R.array.spinner);


SpinnerAdapter spinnerAdapter = new ArrayAdapter(this,
R.layout.support_simple_spinner_dropdown_item, array);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(onItemSelectedListener);
adaugaRevizie.setOnClickListener(adaugaListener);

}
}

=====================================================================================
=====

public class ReviziiActivity extends AppCompatActivity {

ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_revizii);
listView = (ListView) findViewById(R.id.listaRevizii);
listView.setAdapter(new MyCustomAdapter(this, R.layout.list_item,AdaugareActivity.listaRevizii));

ListView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Revizie revizie = (Revizie) adapterView.getItemAtPosition(i);
String numarAuto = revizie.getNumarAuto();
int numarKm = revizie.getNumarKm();
int cost = revizie.getCost();
String tip = revizie.getTip();
Date data = revizie.getData();

Intent intent = new Intent(ReviziiActivity.this,ModifyActivity.class);


intent.putExtra("numarAuto",numarAuto);
intent.putExtra("numarKm", numarKm);
intent.putExtra("cost",cost);
intent.putExtra("data", data);
intent.putExtra("tip",tip);
startActivity(intent);

}
};
listView.setOnItemClickListener(onItemClickListener);
}

public class MyCustomAdapter extends ArrayAdapter<Revizie> {


int layout;

public MyCustomAdapter(Context context, int resource, List<Revizie> revizii) {


super(context, resource, revizii);
layout = resource;
}
public class ViewHolder {
TextView numarMasina;
TextView numarKM;
TextView tip;
TextView data;
TextView cost;
}
@Override
@NonNull
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout,parent,false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.numarMasina = (TextView) convertView.findViewById(R.id.numarAutoLv);
viewHolder.numarKM = (TextView) convertView.findViewById(R.id.numarKmLV);
viewHolder.tip = (TextView) convertView.findViewById(R.id.tipLV);
viewHolder.data = (TextView) convertView.findViewById(R.id.dataLV);
viewHolder.cost = (TextView) convertView.findViewById(R.id.costLV);
convertView.setTag(viewHolder);
mainViewHolder = (ViewHolder) convertView.getTag();
String numarMasinaString = getItem(position).getNumarAuto();
String numarKmString = "" + getItem(position).getNumarKm();
String tipString = getItem(position).getTip();
Date data = getItem(position).getData();
String cost = "" + getItem(position).getCost();
mainViewHolder.numarMasina.setText("Numar masina : " + numarMasinaString);
mainViewHolder.numarKM.setText("Numar km : " + numarKmString);
mainViewHolder.tip.setText("Tip revizie : " + tipString);
mainViewHolder.data.setText("Data revizie : " + data.toString());
mainViewHolder.cost.setText("Cost revizie : " + cost);
}
return convertView;
}

}
}
=====================================================================================
=====

public class ModifyActivity extends Activity {

// public static List<Revizie> listaRevizii;


Button modificaRevizie;
EditText numarAuto;
EditText numar
Km;
Spinner tip;
EditText data;
EditText cost
String numarAutoString;
int numarKmInt;
String tipString;
Date dataUtil;
int costInt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_modify);
// listaRevizii = new ArrayList<>();
modificaRevizie = (Button) findViewById(R.id.modificaRevizie);
numarAuto = (EditText) findViewById(R.id.numarAuto);
numarKm = (EditText) findViewById(R.id.numarKm);
tip = (Spinner) findViewById(R.id.tip);
data = (EditText) findViewById(R.id.data);
cost = (EditText) findViewById(R.id.cost);
Intent intent=getIntent();
String numarMasinaIntent=intent.getStringExtra("numarAuto");
String numarKmIntent=intent.getStringExtra("numarKm");
String dataIntent=intent.getStringExtra("data");
String tipIntent=intent.getStringExtra("tip");
String costIntent=intent.getStringExtra("cost");
numarAuto.setText(numarMasinaIntent);
numarKm.setText(numarKmIntent);
data.setText(dataIntent);
cost.setText(costIntent);

Button.OnClickListener onClickListener = new View.OnClickListener() {


@Override
public void onClick(View view) {
numarAutoString = numarAuto.getText().toString();
numarKmInt = Integer.parseInt(numarKm.getText().toString());
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyy");
try {
dataUtil = format.parse(data.getText().toString());
} catch (ParseException e) {
e.printStackTrace();
}
costInt = Integer.parseInt(cost.getText().toString());
if (tipString != null) {
Revizie revizie = new Revizie(numarAutoString, numarKmInt, tipString, dataUtil, costInt);
for(int i=0;i<AdaugareActivity.listaRevizii.size();i++)
{
if(AdaugareActivity.listaRevizii.get(i).getNumarAuto().equalsIgnoreCase(numarAutoString))
{
AdaugareActivity.listaRevizii.set(i,revizie);
Toast.makeText(ModifyActivity.this,"Revizia a fost modificata
!",Toast.LENGTH_SHORT).show();
}
}

}
else
{
Toast.makeText(ModifyActivity.this,"Selectati tipul reviziei !",Toast.LENGTH_SHORT).show();
}
}
};

Spinner.OnItemSelectedListener onItemSelectedListener = new


AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView editText = (TextView) view;
tipString = editText.getText().toString();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
};

String[] array = getResources().getStringArray(R.array.spinner);


SpinnerAdapter spinnerAdapter = new ArrayAdapter(this,
R.layout.support_simple_spinner_dropdown_item, array);
tip.setAdapter(spinnerAdapter);
tip.setOnItemSelectedListener(onItemSelectedListener);
modificaRevizie.setOnClickListener(onClickListener);

}
}
=====================================================================================
=====
public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables


// Database Version
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "reviziiManager";

// Contacts table name


private static final String TABLE_REVIZII = "revizii";

// Contacts Table Columns names


private static final String NUMAR_AUTO = "numarAuto";
private static final String NUMAR_KM = "numarKm";
private static final String COST = "cost";
private static final String DATA = "data";
private static final String TIP = "tip";

public DatabaseHandler(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_REVIZII_TABLE = "CREATE TABLE " + TABLE_REVIZII + "("
+ NUMAR_AUTO + " TEXT PRIMARY KEY, " + NUMAR_KM + " INTEGER, "
+ COST + " INTEGER, " + DATA + " TEXT, " + TIP + " TEXT" +")";
db.execSQL(CREATE_REVIZII_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_REVIZII);

onCreate(db);
}

// Adding new contact


void addRevizie(Revizie revizie) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();


values.put(NUMAR_AUTO, revizie.getNumarAuto()); // Contact Name
values.put(NUMAR_KM, revizie.getNumarKm()); // Contact Phone
values.put(COST, revizie.getCost());
values.put(DATA, String.valueOf(revizie.getData()));
values.put(TIP, revizie.getTip());
// Inserting Row
db.insert(TABLE_REVIZII, null, values);
db.close(); // Closing database connection
}
public int getReviziiCount() {
String countQuery = "SELECT * FROM " + TABLE_REVIZII;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
// Getting single contact
// Contact getContact(int id) {
// SQLiteDatabase db = this.getReadableDatabase();
//
// Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
// KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
// new String[] { String.valueOf(id) }, null, null, null, null);
// if (cursor != null)
// cursor.moveToFirst();
//
// Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
// cursor.getString(1), cursor.getString(2));
// // return contact
// return contact;
// }

// // Getting All Contacts


// public List<Contact> getAllContacts() {
// List<Contact> contactList = new ArrayList<Contact>();
// // Select All Query
// String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
//
// SQLiteDatabase db = this.getWritableDatabase();
// Cursor cursor = db.rawQuery(selectQuery, null);
//
// // looping through all rows and adding to list
// if (cursor.moveToFirst()) {
// do {
// Contact contact = new Contact();
// contact.setID(Integer.parseInt(cursor.getString(0)));
// contact.setName(cursor.getString(1));
// contact.setPhoneNumber(cursor.getString(2));
// // Adding contact to list
// contactList.add(contact);
// } while (cursor.moveToNext());
// }
//
// // return contact list
// return contactList;
// }

// Updating single contact


// public int updateContact(Contact contact) {
// SQLiteDatabase db = this.getWritableDatabase();
//
// ContentValues values = new ContentValues();
// values.put(KEY_NAME, contact.getName());
// values.put(KEY_PH_NO, contact.getPhoneNumber());
//
// // updating row
// return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
// new String[] { String.valueOf(contact.getID()) });
// }
//
// // Deleting single contact
// public void deleteContact(Contact contact) {
// SQLiteDatabase db = this.getWritableDatabase();
// db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
// new String[] { String.valueOf(contact.getID()) });
// db.close();
// }
//
//
// // Getting contacts Count