-3

i want to call my CompanyDetail to display only the datas on another activity.. i want to hide the buttons from CompanytDetail to another activity when i display it. this is my code for CompanyDetail.. my second activity will display CompanyDetail without button as the user click on the listviewItem .. how can i code it? im new to android studio..

public class CompanyDetail extends Activity implements View.OnClickListener {

Button cbtnsave, cbtndelete, cbtnclose;
EditText etcomname, etcomstand, etcomrep;
EditText etcomcont, etcommail;
private int _Company_Id=0;

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

    cbtnsave = (Button) findViewById(R.id.cbtnSave);
    cbtndelete = (Button) findViewById(R.id.cbtnDelete);
    cbtnclose = (Button) findViewById(R.id.cbtnClose);

    etcomname = (EditText) findViewById(R.id.eTcomname);
    etcomstand = (EditText) findViewById(R.id.eTcomstand);
    etcomrep = (EditText) findViewById(R.id.eTcomrep);
    etcomcont = (EditText) findViewById(R.id.eTcomcont);
    etcommail = (EditText) findViewById(R.id.eTcommail);

    _Company_Id=0;
    Intent intentc = getIntent();
    _Company_Id = intentc.getIntExtra("company_Id", 0);
    CompanyCrud ccrud= new CompanyCrud(this);
    Company company = new Company();
    company = ccrud.getCompanyById(_Company_Id);

    etcomname.setText(company.cname);
    etcomstand.setText(company.cstanding);
    etcomrep.setText(company.crepres);
    etcomcont.setText(company.ccontact);
    etcommail.setText(company.cemail);

}

public void onClick(View view) {

    if (view == findViewById(R.id.cbtnSave)){
        CompanyCrud ccrud = new CompanyCrud(this);
        Company company= new Company();

        company.cname = etcomname.getText().toString();
        company.cstanding = etcomstand.getText().toString();
        company.crepres = etcomrep.getText().toString();
        company.ccontact=etcomcont.getText().toString();
        company.cemail=etcommail.getText().toString();

        if (_Company_Id==0){
            _Company_Id=ccrud.insert(company);

            Toast.makeText(this, "New Company Created", Toast.LENGTH_SHORT).show();
        } else {
            ccrud.update(company);
            Toast.makeText(this, "Company Updated", Toast.LENGTH_SHORT).show();
        }

    }else if (view == findViewById(R.id.cbtnDelete)){
        CompanyCrud ccrud = new CompanyCrud(this);
        ccrud.delete(_Company_Id);

        Toast.makeText(this, "Company Deleted", Toast.LENGTH_SHORT).show();
        finish();
    }else if ( view == findViewById(R.id.cbtnClose)){
        finish();
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_company_detail, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
 }

now this is my code for Crud operation of CompanyDetail

 public class CompanyCrud {

private DBHelper dbHelper;

public CompanyCrud(Context context) { dbHelper = new DBHelper(context);}

public int insert(Company company){
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Company.KEY_cname, company.cname);
    values.put(Company.KEY_cstanding, company.cstanding);
    values.put(Company.KEY_crepres, company.crepres);
    values.put(Company.KEY_ccontact, company.ccontact);
    values.put(Company.KEY_cemail, company.cemail);

    long company_Id = db.insert(Company.TABLE, null, values);
    db.close();
    return  (int) company_Id;

}

public void delete(int company_Id) {

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    db.delete(Company.TABLE,Company.KEY_ID + "=?", new String[]{ String.valueOf(company_Id) });
    db.close();
}

public void update(Company company) {
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Company.KEY_cname, company.cname);
    values.put(Company.KEY_cstanding, company.cstanding);
    values.put(Company.KEY_crepres, company.crepres);
    values.put(Company.KEY_ccontact, company.ccontact);
    values.put(Company.KEY_cemail, company.cemail);

    db.update(Company.TABLE, values, Company.KEY_ID + "= ?", new String[]{String.valueOf(company.company_ID)});
    db.close();

}

public ArrayList<HashMap<String, String>> getCompanytList () {
    SQLiteDatabase db = dbHelper.getReadableDatabase();

    String selectQuery = "SELECT " +
            Company.KEY_ID + "," +
            Company.KEY_cname + "," +
            Company.KEY_crepres + "," +
            Company.KEY_ccontact + "," +
            Company.KEY_cemail +
            " FROM " + Company.TABLE;

    ArrayList<HashMap<String, String>>companyList =  new ArrayList<HashMap<String, String>>();

    Cursor cursor= db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()){

        do{
            HashMap<String , String > company = new HashMap<String, String >();
            company.put("cid", cursor.getString(cursor.getColumnIndex(Company.KEY_ID)));
            company.put("cname", cursor.getString(cursor.getColumnIndex(Company.KEY_cname)));
            companyList.add(company);
        } while (cursor.moveToNext());
    }cursor.close();
    db.close();
    return companyList;

}

public Company getCompanyById(int cid) {
    SQLiteDatabase db = dbHelper.getReadableDatabase();

    String selectQuery = "SELECT " +
            Company.KEY_ID + "," +
            Company.KEY_cname + "," +
            Company.KEY_crepres + "," +
            Company.KEY_ccontact + "," +
            Company.KEY_cemail +
            " FROM " + Company.TABLE
            + " WHERE " +
            Company.KEY_ID + "=?";

    int ictrc =0;
    Company company = new Company();

    Cursor cursor = db.rawQuery(selectQuery, new String[]{ String.valueOf(cid)});

    if (cursor.moveToFirst()){
        do {
            company.company_ID=cursor.getInt(cursor.getColumnIndex(Company.KEY_ID));
            company.cstanding=cursor.getString(cursor.getColumnIndex(Company.KEY_cstanding));
            company.crepres=cursor.getString(cursor.getColumnIndex(Company.KEY_crepres));
            company.ccontact= cursor.getString(cursor.getColumnIndex(Company.KEY_ccontact));
            company.cemail = cursor.getString(cursor.getColumnIndex(Company.KEY_cemail));

        }while (cursor.moveToNext());

    } cursor.close();
    db.close();
    return company;

}
 }

and this is the class file where i want to display the data and i have nothing on it cause i dont know what i am going to do.. i look for answer using bundle... putextras.. getextras.. i dnt know how to do it and where am i going to put it..

 public class DisplayData extends ActionBarActivity {

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_display_data, 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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
 }
Jeraline
  • 39
  • 9

1 Answers1

0

Uninstall and reinstall your app

This is because, once your app is deployed in the emulator or testing device, and after that if you make any changes to the structure of the database, it wont be reflected and you get an error. when you reinstall it, the database is created as a new one and thus no error..!!!

UPDATE

@DerGolem mentioned a valuable comment above

If you modified the database since it's original creation, then be sure you have an onUpgrade() method. And that you set the DATABASE_VERSION constant to a higher number.

Thanks @DerGolem for pointing out that as I was really unaware about that fact..

Lal
  • 14,726
  • 4
  • 45
  • 70