I'm trying to figure a way of allowing users NOT to be able to add the same ID twice as if I wanted to delete one of the users and he shared the ID with another it would delete both.
So far I have found no help doing so does anyone have an idea?
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText IdText;
private EditText NameText;
private EditText AgeText;
private EditText WeightText;
private EditText HeightText;
private EditText ReachText;
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IdText = (EditText) findViewById(R.id.IdText);
NameText = (EditText) findViewById(R.id.NameText);
HeightText = (EditText) findViewById(R.id.HeightText);
AgeText = (EditText) findViewById(R.id.AgeText);
WeightText = (EditText) findViewById(R.id.WeightText);
ReachText = (EditText) findViewById(R.id.ReachText);
button = (Button) findViewById(R.id.button);
dbHandler = new MyDBHandler(this);
AddData();
}
public void AddData() {
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if ((IdText.getText().toString()).isEmpty()) {
IdText.setError("Please fill out your name");
return;
} else if ((NameText.getText().toString()).isEmpty()) {
NameText.setError("Please fill out your ID");
return;
} else if ((AgeText.getText().toString()).isEmpty()) {
AgeText.setError("Please fill out your Age");
return;
} else if ((HeightText.getText().toString()).isEmpty()) {
HeightText.setError("Please fill out your Height in centimeters");
return;
} else if ((WeightText.getText().toString()).isEmpty()) {
WeightText.setError("Please fill out your weight in kilos");
return;
} else if ((ReachText.getText().toString()).isEmpty()) {
ReachText.setError("Please fill out your reach in inches");
return;
} else {
boolean isInserted = dbHandler.insertData(IdText.getText().toString(),
NameText.getText().toString(),
AgeText.getText().toString(),
HeightText.getText().toString(),
WeightText.getText().toString(),
ReachText.getText().toString());
if (isInserted == true)
Toast.makeText(MainActivity.this, "Fighter added", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not inserted", Toast.LENGTH_LONG).show();
}
}
}
);
}
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Fighters.db";
public static final String TABLE_PRODUCTS = "Fighter";
public static final String COLUMN_ID = "ID1";
public static final String COLUMN_NAME = "Name";
public static final String COLUMN_AGE = "Age";
public static final String COLUMN_WEIGHT = "Weight";
public static final String COLUMN_HEIGHT = "Height";
public static final String COLUMN_REACH = "Reach";
SQLiteDatabase db;
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_PRODUCTS + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,ID1 TEXT,Name TEXT,Age INTEGER,Weight INTEGER,Height TEXT,Reach TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_PRODUCTS);
this.onCreate(db);
}
//Add new row to the database
public boolean insertData(String id1, String name, String age, String weight, String height, String reach) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_ID, id1);
contentValues.put(COLUMN_NAME, name);
contentValues.put(COLUMN_AGE, age);
contentValues.put(COLUMN_WEIGHT, weight);
contentValues.put(COLUMN_HEIGHT, height);
contentValues.put(COLUMN_REACH, reach);
long result = db.insert(TABLE_PRODUCTS, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_PRODUCTS, null);
return res;
}
public boolean updateData(String id1,String name, String age, String weight, String height, String reach) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_ID, id1);
contentValues.put(COLUMN_NAME, name);
contentValues.put(COLUMN_AGE, age);
contentValues.put(COLUMN_WEIGHT, weight);
contentValues.put(COLUMN_HEIGHT, height);
contentValues.put(COLUMN_REACH, reach);
db.update(TABLE_PRODUCTS, contentValues, "ID1 = ?", new String[] {id1 });
return true;
}
public Integer deleteData (String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_PRODUCTS, "ID1 = ?", new String[] {id});
}