如何在不按钮从活动呼叫数据到另一个只显示数据库中的数据 [英] how to call data from activity to another without the button only display the data inside the database

查看:165
本文介绍了如何在不按钮从活动呼叫数据到另一个只显示数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话给我CompanyDetail到另一个活动只显示DATAS ..我想隐藏CompanytDetail按钮到另一个活动时,我显示出来。这是我的code为CompanyDetail ..我的第二次​​活动将不显示按钮作为CompanyDetail在ListViewItem的用户点击..我怎么能code呢?即时通讯新的到Android工作室..

 公共类CompanyDetail扩展活动实现View.OnClickListener {按钮cbtnsave,cbtndelete,cbtnclose;
的EditText etcomname,etcomstand,etcomrep;
的EditText etcomcont,etcommail;
私人INT _Company_Id = 0;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_company_detail);    cbtnsave =(按钮)findViewById(R.id.cbtnSave);
    cbtndelete =(按钮)findViewById(R.id.cbtnDelete);
    cbtnclose =(按钮)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;
    意向intentc = getIntent();
    _Company_Id = intentc.getIntExtra(COMPANY_ID,0);
    CompanyCrud ccrud =新CompanyCrud(本);
    公司公司=新的公司();
    公司= ccrud.getCompanyById(_Company_Id);    etcomname.setText(company.cname);
    etcomstand.setText(company.cstanding);
    etcomrep.setText(company.cre preS);
    etcomcont.setText(company.ccontact);
    etcommail.setText(company.cemail);}公共无效的onClick(查看视图){    如果(查看== findViewById(R.id.cbtnSave)){
        CompanyCrud ccrud =新CompanyCrud(本);
        公司公司=新的公司();        company.cname = etcomname.getText()的toString()。
        。company.cstanding = etcomstand.getText()的toString();
        。company.cre preS = etcomrep.getText()的toString();
        。company.ccontact = etcomcont.getText()的toString();
        company.cemail = etcommail.getText()的toString()。        如果(_Company_Id == 0){
            _Company_Id = ccrud.insert(公司);            Toast.makeText(这一点,新公司创立Toast.LENGTH_SHORT).show();
        }其他{
            ccrud.update(公司);
            Toast.makeText(这一点,本公司更新了,Toast.LENGTH_SHORT).show();
        }    }否则如果(查看== findViewById(R.id.cbtnDelete)){
        CompanyCrud ccrud =新CompanyCrud(本);
        ccrud.delete(_Company_Id);        Toast.makeText(这一点,公司已删除,Toast.LENGTH_SHORT).show();
        完();
    }否则如果(查看== findViewById(R.id.cbtnClose)){
        完();
    }}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.menu_company_detail,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。操作栏会
    //自动处理上点击主页/向上按钮,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT ID = item.getItemId();    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }    返回super.onOptionsItemSelected(项目);
}
 }

现在这是我的code为CompanyDetail的操作界面污物

 公共类CompanyCrud {私人DBHelper dbHelper;公共CompanyCrud(上下文的背景下){dbHelper =新DBHelper(上下文);}公众诠释插入(公司公司){
    SQLiteDatabase分贝= dbHelper.getWritableDatabase();
    ContentValues​​值=新ContentValues​​();
    values​​.put(Company.KEY_cname,company.cname);
    values​​.put(Company.KEY_cstanding,company.cstanding);
    values​​.put(Company.KEY_cre preS,company.cre preS);
    values​​.put(Company.KEY_ccontact,company.ccontact);
    values​​.put(Company.KEY_cemail,company.cemail);    长COMPANY_ID = db.insert(Company.TABLE,空,价值);
    db.close();
    回报(INT)COMPANY_ID;}公共无效删除(INT COMPANY_ID){    SQLiteDatabase分贝= dbHelper.getWritableDatabase();
    db.delete(Company.TABLE,Company.KEY_ID +=?,新的String [] {将String.valueOf(COMPANY_ID)});
    db.close();
}公共无效更新(公司公司){
    SQLiteDatabase分贝= dbHelper.getWritableDatabase();
    ContentValues​​值=新ContentValues​​();
    values​​.put(Company.KEY_cname,company.cname);
    values​​.put(Company.KEY_cstanding,company.cstanding);
    values​​.put(Company.KEY_cre preS,company.cre preS);
    values​​.put(Company.KEY_ccontact,company.ccontact);
    values​​.put(Company.KEY_cemail,company.cemail);    db.update(Company.TABLE,价值观,Company.KEY_ID +=?,新的String [] {将String.valueOf(company.company_ID)});
    db.close();}公众的ArrayList<&HashMap的LT;字符串,字符串>> getCompanytList(){
    SQLiteDatabase分贝= dbHelper.getReadableDatabase();    串selectQuery =SELECT+
            Company.KEY_ID +,+
            Company.KEY_cname +,+
            Company.KEY_cre preS +,+
            Company.KEY_ccontact +,+
            Company.KEY_cemail +
            FROM+ Company.TABLE;    ArrayList的<&HashMap的LT;字符串,字符串>> companyList =新的ArrayList<&HashMap的LT;字符串,字符串>>();    光标光标= db.rawQuery(selectQuery,NULL);    如果(cursor.moveToFirst()){        做{
            HashMap的<字符串,字符串>公司=新的HashMap<字符串,字符串>();
            company.put(CID,cursor.getString(cursor.getColumnIndex(Company.KEY_ID)));
            company.put(CNAME,cursor.getString(cursor.getColumnIndex(Company.KEY_cname)));
            companyList.add(公司);
        }而(cursor.moveToNext());
    } cursor.close();
    db.close();
    返回companyList;}上市公司getCompanyById(INT CID){
    SQLiteDatabase分贝= dbHelper.getReadableDatabase();    串selectQuery =SELECT+
            Company.KEY_ID +,+
            Company.KEY_cname +,+
            Company.KEY_cre preS +,+
            Company.KEY_ccontact +,+
            Company.KEY_cemail +
            FROM+ Company.TABLE
            +WHERE+
            Company.KEY_ID +=;    INT ictrc = 0;
    公司公司=新的公司();    光标光标= db.rawQuery(selectQuery,新的String [] {将String.valueOf(CID)});    如果(cursor.moveToFirst()){
        做{
            company.company_ID = cursor.getInt(cursor.getColumnIndex(Company.KEY_ID));
            company.cstanding = cursor.getString(cursor.getColumnIndex(Company.KEY_cstanding));
            company.cre preS = cursor.getString(cursor.getColumnIndex(Company.KEY_cre preS));
            company.ccontact = cursor.getString(cursor.getColumnIndex(Company.KEY_ccontact));
            company.cemail = cursor.getString(cursor.getColumnIndex(Company.KEY_cemail));        }而(cursor.moveToNext());    } cursor.close();
    db.close();
    返回公司;}
 }

这就是类文件,我想显示的数据和我有什么就可以了,因为我不知道我会怎么做..我用包... .. putextras getextras寻找答案..我DNT知道如何做到这一点,我要去把它..

 公共类DisplayData扩展ActionBarActivity {@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_display_data);
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.menu_display_data,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。操作栏会
    //自动处理上点击主页/向上按钮,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT ID = item.getItemId();    // noinspection SimplifiableIfStatement
    如果(ID == R.id.action_settings){
        返回true;
    }    返回super.onOptionsItemSelected(项目);
}
 }


解决方案

卸载并重新安装应用

这是因为,一旦你的应用程序部署在模拟器或测试装置,在那之后,如果您对数据库的结构的任何变化,它不会反映出来,你会得到一个错误。当你重新安装,数据库创建为一个新的,因此没有错误.. !!!

更新

@DerGolem上面提到

的宝贵意见

  

如果你修改,因为它的原始创建数据库,然后就
  确保你有一个onUpgrade()方法。和您设置的
  DATABASE_VERSION常数较大的数字。


感谢@DerGolem的指出,因为我真的不知道有关事实。

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);
}
 }

解决方案

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..

这篇关于如何在不按钮从活动呼叫数据到另一个只显示数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆