我想调用我的CompanyDetail只显示另一个活动的数据..我想在显示它时将CompanytDetail中的按钮隐藏到另一个活动。这是我的CompanyDetail的代码..我的第二个活动将显示没有按钮的CompanyDetail,因为用户点击listviewItem ..我怎么能编码它?我是Android工作室的新手..
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);
}
}
现在这是我的公司详细信息Crud操作的代码
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;
}
}
这是我想要显示数据的类文件,我什么都没有,因为我不知道我要做什么..我寻找答案使用捆绑... putextras .. getextras ..我dnt知道该怎么做以及我要把它放在哪里..
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);
}
}
答案 0 :(得分:0)
这是因为,一旦您的应用程序部署在模拟器或测试设备中,之后如果您对数据库的结构进行任何更改,它就不会被反映出来并且您会收到错误。当你重新安装它时,数据库被创建为一个新的,因此没有错误.. !!!
<强>更新强>
@DerGolem上面提到了一个有价值的评论
如果您修改了数据库,因为它是原始创建的,那么就是 确定你有一个onUpgrade()方法。并且你设置了 DATABASE_VERSION常数为更高的数字。
感谢@DerGolem指出,因为我真的没有意识到这个事实......