dimanche 26 juin 2016

Android SQLite database doesnot Update and Delete

This is mainactivity which provides the user Input

public class Welcome extends AppCompatActivity{
private DBMANAGER_person dbmanager_person;
private ListView listView;
private SimpleCursorAdapter adapter;

final String [] from = new String[]{MyDB.COLUMN_ID, MyDB.COLUMN_NAME, MyDB.COLUMN_AGE, MyDB.COLUMN_HEIGHT , MyDB.COLUMN_WEIGHT};
final int [] to = new int[]{R.id.nameTV,R.id.ageTV,R.id.heightTV,R.id.weightTV};



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

    dbmanager_person = new DBMANAGER_person(this);
    dbmanager_person.open();

    Cursor cursor = dbmanager_person.fetch();

    listView = (ListView) findViewById(R.id.list_view);
    listView.setEmptyView(findViewById(R.id.emptyTV));

    adapter = new SimpleCursorAdapter(this, R.layout.person, cursor, from, to,0);
    adapter.notifyDataSetChanged();

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView idTextView = (TextView)view.findViewById(R.id.idTV);
            TextView nameTextView = (TextView) view.findViewById(R.id.nameTV);
            TextView ageTextView = (TextView) view.findViewById(R.id.ageTV);
            TextView heightTextView = (TextView) view.findViewById(R.id.heightTV);
            TextView weightTextView = (TextView) view.findViewById(R.id.weightTV);

            String iD = idTextView.getText().toString();
            String name = nameTextView.getText().toString();
            String age = ageTextView.getText().toString();
            String height = heightTextView.getText().toString();
            String weight = weightTextView.getText().toString();

            Intent intent = new Intent(getApplicationContext(), Modiffy_person_Details.class);
            intent.putExtra("_id",iD);
            intent.putExtra("name", name);
            intent.putExtra("age", age);
            intent.putExtra("height", height);
            intent.putExtra("weight", weight);

            startActivity(intent);


        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.add_person,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if(id == R.id.add){
        Intent intent = new Intent(this,Add_people.class);
        startActivity(intent);

    }else if (id == R.id.logout){
        Intent backToHome = new Intent(this,MainActivity.class);
        startActivity(backToHome);
    }

    return super.onOptionsItemSelected(item);
}

}

This is Add_prople class

public class Add_people extends AppCompatActivity implements View.OnClickListener { private EditText nameEditText; private EditText ageEditText; private EditText heightEditText; private EditText weightEditText;

private Button save;
private DBMANAGER_person dbmanager_person;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTitle("Add People");

    setContentView(R.layout.activity_add_people);

    nameEditText = (EditText)findViewById(R.id.nameET);
    ageEditText = (EditText)findViewById(R.id.ageET);
    heightEditText = (EditText)findViewById(R.id.heightET);
    weightEditText = (EditText)findViewById(R.id.weightET);

    save = (Button)findViewById(R.id.saveBtn);

    dbmanager_person = new DBMANAGER_person(this);
    dbmanager_person.open();
    save.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.saveBtn:
            final String name = nameEditText.getText().toString();
            final String age = ageEditText.getText().toString();
            final String height = heightEditText.getText().toString();
            final String weight = weightEditText.getText().toString();

            dbmanager_person.insert(name,age,height,weight);

            Intent main = new Intent(this,Welcome.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(main);
            break;

    }

}

}

This is DBMANAGER_person class where the CRUD operation works

This is the Modiffy_person_Details class where user can Update and Delete their information

public class DBMANAGER_person {
private SQLiteDatabase database;
private MyDB myDB;
private Context context;

public DBMANAGER_person(Context context) {
    this.context = context;
}

public DBMANAGER_person open() throws SQLiteException{
    myDB = new MyDB(context);
    database = myDB.getWritableDatabase();
    return this;
}

public void close(){
    myDB.close();
}
public void insert(String name,String age,String height,String weight){
    ContentValues contentValues = new ContentValues();
    contentValues.put(MyDB.COLUMN_NAME,name);
    contentValues.put(MyDB.COLUMN_AGE,age);
    contentValues.put(MyDB.COLUMN_HEIGHT,height);
    contentValues.put(MyDB.COLUMN_WEIGHT,weight);
    database.insert(MyDB.TABLE_NAME,null,contentValues);
}

public Cursor fetch(){
    String[] columns  = new String[]{MyDB.COLUMN_ID, MyDB.COLUMN_NAME, MyDB.COLUMN_AGE , MyDB.COLUMN_HEIGHT, MyDB.COLUMN_WEIGHT};
    Cursor cursor = database.rawQuery( "select rowid _id,* from "+MyDB.TABLE_NAME, null);
    cursor.moveToFirst();
    return cursor;
}

public int update(long id, String name, String age, String height, String weight){
    ContentValues contentValues = new ContentValues();
    contentValues.put(MyDB.COLUMN_NAME,name);
    contentValues.put(MyDB.COLUMN_AGE,age);
    contentValues.put(MyDB.COLUMN_HEIGHT,height);
    contentValues.put(MyDB.COLUMN_WEIGHT,weight);
    int i = database.update(MyDB.TABLE_NAME,contentValues,MyDB.COLUMN_ID + " = "+id,null);
    return i;
}

public void delete(long id){
    database.delete(MyDB.TABLE_NAME, MyDB.COLUMN_ID + "="+ id,null);
}

}

public class Modiffy_person_Details extends AppCompatActivity implements View.OnClickListener{ private EditText nameField; private EditText ageField; private EditText heightField; private EditText weightField;

private Button update;
private Button delete;

private long _id;
private DBMANAGER_person dbmanager_person;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Modify Record");
    setContentView(R.layout.activity_modiffy_person__details);

    dbmanager_person = new DBMANAGER_person(this);
    dbmanager_person.open();

    nameField = (EditText)findViewById(R.id.nameEditText);
    ageField = (EditText)findViewById(R.id.ageEditText);
    heightField = (EditText)findViewById(R.id.heightEditText);
    weightField = (EditText)findViewById(R.id.weightEditText);

    update = (Button)findViewById(R.id.update_btn);
    delete = (Button)findViewById(R.id.delete_btn);

    Intent intent = getIntent();
    String ID = intent.getStringExtra("_id");
    String name = intent.getStringExtra("name");
    String age = intent.getStringExtra("age");
    String height = intent.getStringExtra("height");
    String weight = intent.getStringExtra("weight");

    String check_ID = ID;
    if(!check_ID.equals("")) {

        _id = Long.parseLong(ID);
    }/*else{
        Toast.makeText(getApplicationContext(),"There is no id",Toast.LENGTH_LONG).show();
    }*/



        nameField.setText(name);
        ageField.setText(age);
        heightField.setText(height);
        weightField.setText(weight);





    update.setOnClickListener(this);
    delete.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.update_btn:
            String na = nameField.getText().toString();
            String ag = ageField.getText().toString();
            String hei = heightField.getText().toString();
            String wei = weightField.getText().toString();

            dbmanager_person.update( _id , na , ag,hei,wei);
            this.returnHome();
            break;
        case R.id.delete_btn:
            dbmanager_person.delete(_id);
            this.returnHome();
            break;
    }

}

public void returnHome(){
    Intent home_intent = new Intent(getApplicationContext(),Welcome.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(home_intent);
}

}

My problem is whenever i want to update or delete my information it does not work.

Aucun commentaire:

Enregistrer un commentaire