mercredi 1 juillet 2015

Deleting records which are generated dynamically in android

I am working on a app which stores record. Until now i have successfully added the data into data base and retrieve it from data base.

Now my problem is deleting the records. I know the query of deleting a record but in which the record is being is displayed is AlertDialog.

Below is my code.

public class MainActivity extends ActionBarActivity {

EditText topic;
EditText description;
Button add;
SQLiteDatabase db;

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

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    topic = (EditText)findViewById(R.id.addTopic);
    description = (EditText)findViewById(R.id.addDescription);
    add = (Button)findViewById(R.id.addBtn);

   db = openOrCreateDatabase("RecordMaster",Context.MODE_PRIVATE,null);
    db.execSQL("CREATE TABLE IF NOT EXISTS records(topic VARCHAR,description VARCHAR)");

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(v==add)
            {
                if(topic.getText().toString().trim().length()==0 || description.getText().toString().trim().length()==0)
                {
                    Toast.makeText(getApplicationContext(), "Please Enter Topic & Description",Toast.LENGTH_LONG).show();
                    return;
                }
            }
            db.execSQL(" INSERT INTO records VALUES('"+topic.getText()+"','"+description.getText()+"') ");
            Toast.makeText(getApplicationContext(), "Record Added Successfully", Toast.LENGTH_LONG).show();
            topic.setText("");
            description.setText("");
        }
    });


}

public void showMessage(String title,String message)
{
    Builder builder=new Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    if (id == R.id.exit) {
         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         intent.putExtra("Do You Wanna EXIT ?", true);
         startActivity(intent);
         finish();
         Toast.makeText(getApplicationContext(), "Thanks For Using My Application", Toast.LENGTH_LONG).show();
    }

    if(id == R.id.display_record){

        db = openOrCreateDatabase("RecordMaster",Context.MODE_PRIVATE,null);

     Cursor c = db.rawQuery("SELECT * FROM records",null);

        if(c.getCount()==0)
        {
            Toast.makeText(getApplicationContext(), "No records available", Toast.LENGTH_LONG).show();
        }

        StringBuffer buffer = new StringBuffer();
        while(c.moveToNext())
        {
            buffer.append("Topic: -"+c.getString(0)+"\n");
            buffer.append("Description: -"+c.getString(1)+"\n\n");
        }

        showMessage("Records",buffer.toString());

        db.close();
    }

    if(R.id.delete_record)
    {
        //what code i have to write to delete the records
    }
    return super.onOptionsItemSelected(item);
}

I am referring http://ift.tt/1ozHQjl. In this it is done on button click where we have to enter roll_no but i want that when i click on my menu item(delete) it should show all my records with check box so i can check one or multiple records to delete.

Aucun commentaire:

Enregistrer un commentaire