获取数据从数据库问题在Android中 [英] Fetching Data from Database Issue in Android

查看:118
本文介绍了获取数据从数据库问题在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个布局:


  1. 主要布局(与按钮档案)

  1. Main Layout(with a button "Profile")

档案布局(与TextView中显示来自数据库,并命名为编辑和返回主两按键数据)

Profile layout(With textview to show data from database and two button named as "Edit" and "Back to Main")

编辑布局(带的TextView和EditText上编辑,上面写着保存和返回档案用户,并再次将两个按钮的配置文件)

Edit layout(With textview and edittext to edit the profile of the user and two button again that says "Save" and "back to profile")

方法相关的按钮:
(按钮是一个明星,相关的方法是用两个星)

Methods Related to Buttons: (Button are with one star,related methods are with two stars)

*简介:** editm(查看W)

*Profile: **editm(View w)

*编辑:** calledit(查看W)

* **calledit(View w)

*返回主:** backmain(查看W)

*Back to main: **backmain(View w)

*的保存:的* savemethj(查看W)

*Save:*savemethj(View w)

*回简介:** pbackmeth(查看W)

*back to profile: **pbackmeth(View w)

  public class MainActivity extends Activity
   {
    TextView t1;
EditText e1,e2;
   databasehelper helper; 
       //Profile variables
      String ptrm;
pdatabasehelper phelper;
EditText ed1;
EditText ed2;
EditText ed3;
EditText ed4;
EditText ed5;
EditText ed6;
TextView ptn;
TextView kt0;
TextView kt1;
TextView kt2;
TextView kt3;TextView kt4;TextView kt5;TextView kt6;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     phelper=new pdatabasehelper(this);
}
     public void backmain(View w){ 
   setContentView(R.layout.activity_main);
      }
     public void editm(View w){ 
setContentView(R.layout.profilec);
          try{
                      pshow();
}
catch(Exception e){ 
    Toast.makeText(getBaseContext(), "pshow method", 5).show();
}



    }


public void pshow()
{
    onStartp();
    Log.d("7", "a");
    onstart();

    Log.d("10", "a");
    try{
        Log.d("55555", "entering to db");
       Cursor c=phelper.pshowrecord(ptrm);
       Log.d("11", "a");
       if (c!=null){

           Log.d("c1", "a");
           c.moveToFirst();

           Log.d("cmm1", "a");
        String name= c.getString(0);
           Log.d("cn1", "a");
                  kt1.setText(name);
                                  Log.d("c2", "a");
        kt2.setText(c.getString(1));
        Log.d("c3", "a");

        kt3.setText(c.getString(2));
        Log.d("12", "a");
        kt4.setText(c.getString(3));
        kt5.setText(c.getString(4));
        kt6.setText(c.getString(5));
        Log.d("13", "a");
        }
    else{
        Log.d("14", "a");
        Toast.makeText(this,"Add a Contact", 50).show(); 
    }
        }catch(Exception e){ 
      Toast.makeText(getBaseContext(), "SOme display error", 20).show();
    }
}


public void pbackmeth(View w){

    setContentView(R.layout.profilec);
    pshow();
}
public void calledit(View w)
{

        setContentView(R.layout.editc);


}

public void onStartp(){ 
    kt1=(TextView)findViewById(R.id.ptext3);
    kt2=(TextView)findViewById(R.id.ptext5);
    kt3=(TextView)findViewById(R.id.ptext7);
    kt4=(TextView)findViewById(R.id.ptext9);
    kt5=(TextView)findViewById(R.id.ptext11);
    kt6=(TextView)findViewById(R.id.ptext13);
}

public void onstart(){ 

     ed1=(EditText)findViewById(R.id.jname);
        ed2=(EditText)findViewById(R.id.jeditText1);
      ed3=(EditText)findViewById(R.id.jeditText2);
      ed4=(EditText)findViewById(R.id.jeditText3);
      ed5=(EditText)findViewById(R.id.jeditText4);
     ed6=(EditText)findViewById(R.id.jaeditText1);
      ptn=(TextView)findViewById(R.id.jprofile);

}

public void savemethj(View w)
{ 

    onstart();
    Log.d("tag000", "before allgettext");

    long a;
    Log.d("tag01111", "afterlong");
    ptrm=ptn.getText().toString();
    try{ 
    phelper.deleterecord(ptrm);
    Log.d("tag02222", "be");
    String edt1=ed1.getText().toString();
    String edt2=ed2.getText().toString();
    Log.d("tag03333", "be11111");
    String edt3=ed3.getText().toString();
    String edt4=ed4.getText().toString();
    Log.d("tag0444444", "be2222");
    String edt5=ed5.getText().toString();
    String edt6=ed6.getText().toString();
    Log.d("tag11111", "after allgettext");

     phelper.deleterecord(ptrm);
    a=phelper.insertpro(ptrm,/*img*/edt1, edt2, edt3, edt4, edt5, edt6);
    Log.d("tag9999999", "after allgettext");

    if(a>=1){
        Toast.makeText(getBaseContext(),a+ "Record Successfully Saved", 30).show();
    }

    else{

        Toast.makeText(getBaseContext(),  "Not Saved", 30).show();

    }}
    catch(Exception e)
    { 

       Toast.makeText(getBaseContext(), "Errrrrrrrrrrr", Toast.LENGTH_SHORT).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.activity_main, menu);
    return true;
}

 }

数据库类:

   public class pdatabasehelper extends SQLiteOpenHelper
    {
final static String databasename="demonewexp";
final static int databaseversion=1;

public pdatabasehelper(Context ctx){
     super(ctx,databasename,null,databaseversion);

    }

@Override
public void onCreate(SQLiteDatabase db) {
    try{

         Log.d("tag4545","database");   
   db.execSQL("create table mypro(id text,name text,guardian text,number text,address text,medical text,another text)");

    }
    catch(SQLException e){e.printStackTrace();
    }

    }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("drop table if Exists mypro");
    onCreate(db);

}



 public long insertpro(String id,String e1,String e2,String e3,String e4,String e5,String e6)
 {
    SQLiteDatabase base=getWritableDatabase();
    ContentValues value=new ContentValues();
    value.put("id", id);
    value.put("name", e1);
    value.put("guardian", e2);
    value.put("number", e3);
    value.put("address", e4);
    value.put("medical", e5);
    value.put("another", e6);

    long a=base.insert("mypro",null,value);

     return a;
 }
 public Cursor pshowrecord(String id)
    {
     Log.d("d1", "a");
        Cursor cs = null;
        SQLiteDatabase base=getReadableDatabase();
        Log.d("d2", "a");
        try
        {
            Log.d("d3", "a");
         cs=base.query("mypro", new String[]{"name","guardian","number","address","medical","another"},"id=?", new String[]{String.valueOf(id)}, null, null, null);
         Log.d("d4", "a");
         return cs;
        }catch(Exception e){ 
            Log.d("d5", "a");
            return cs;
        }


    }


 public void deleterecord(String pe_id)
    {
        SQLiteDatabase base=getWritableDatabase();
        base.delete("mypro","id=?",new String[]{pe_id});
    }

          }

我开始我的应用一日一次,我点击配置文件按钮,也因此,它显示敬酒像一些显示错误然后我点击编辑按钮,并添加像名的数据,号码等单击保存按钮没有数据,它得到保存。然后我点击返回个人资料,我可以看到我的个人资料新增或数据回迁轮廓的布局。那么如果我点击返回主搬回主布局,如果我再次点击
配置文件按钮,然后还带我到配置布局,这是从数据库中提取,每次添加的数据。但在这里它来的问题,当我关闭我的应用程序
并启动它,我再次点击配置文件按钮,那么它没有显示任何东西,甚至数据库值数据库在那里,但是当我重新启动我的应用程序就无法提取,我有previously存储并显示像面包数据某些显示错误。我想签出错误,把一些登录,然后我看到日志文件时,我与应用程序的工作,然后它显示最后一个值:

I Start my application 1st time,I clicked the "profile" button,there is no data therefore it shows toast like "some display error" then i click edit button and add the data like name,number etc click save button,it get saved. then i move back to profile layout by clicking "back to profile" and i can see my added profile or data. then if i move back to main layout by clicking "back to main" and if again i click "profile" button then it also take me to the profile layout with added data that was fetched everytime from database. But here it come the problem when i close my application and start it again i click "profile" button,then its not showing anything even database values were there in database but when i start my app again it is unable to fetch the data that i have previously stored and showing toast like "some display error". I thought to check out the errors and put some "Log" then i see log file when i was working with application then it is showing last value as:

    Log.d("cmm1", "a");

这之后我的应用程序终止,并在日志文件中生成任何日志。这意味着我得到的错误,而我正在同:

After that my application stopped and no logs generated in log file. That means i get error while i was working with:

     String name= c.getString(0);

我不知道这有什么code中的问题,或者可能是我有任何其他问题。请帮助我。我这么想吗?

I dont know what is the problem with this code or may be i have any other problem. Please help me out. Am i missing something?

K我已经编辑了code意图开始新的活动。我还有其他code太多,但剪切粘贴code,以方便查看和查找错误。这里是我的code现在:

k I have edited the code with intent to start new Activity. I have other code too but cut paste the code to make it easy to view and find error. Here is my code now:

(第1活性的按钮配置文件,单击按钮轮廓采取意图另一个活动)

(1st Activity with button Profile,clicking profile button take to another activity with intent)

MainActivity.class:

MainActivity.class:

    public class MainActivity extends Activity {
     @Override
       protected void onCreate(Bundle savedInstanceState)
    {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);phelper=new pdatabasehelper(this);
     }

 public void editm(View w){ 

      Intent pro=new Intent(MainActivity.this,profile.class);
      startActivity(new Intent(pro));

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

(显示轮廓)

profile.class:

profile.class:

    public class profile extends Activity
    {
pdatabasehelper phelper;
TextView ptn;
TextView kt0;
TextView kt1;
TextView kt2;
TextView kt3;TextView kt4;TextView kt5;TextView kt6;

      public void onCreate(Bundle savedInstanceState) 
   {
     super.onCreate(savedInstanceState);
    Toast.makeText(getBaseContext(), "Profile View", 5).show();
    setContentView(R.layout.profilec);
    Log.d("oncreate5", "5");
    ptn=(TextView)findViewById(R.id.jprofile);
    Log.d("oncreate5.666", "5.6");
    phelper=new pdatabasehelper(this);
    Log.d("oncreate5", "6");
    pshow();
    Log.d("oncreate5", "7");
    Toast.makeText(getBaseContext(), "helloo new", 10).show();

}

public void pshow(){
    Log.d("6", "a");
    //Toast.makeText(getBaseContext(), "helloo new", 10).show();
    onStartp();
    Log.d("7", "a");
    Cursor c=null;
    Log.d("8", "a");

    Log.d("9,10", "a");
    try{
        Log.d("55555", "entering to db");
    String ptrm = null;
    c=phelper.pshowrecord(ptrm);
    Log.d("11", "a");


    if(c!=null){
          Log.d("c1", "a");
           c.moveToFirst();

             Log.d("cmm1", "a");
        String name= c.getString(0);
            Log.d("cn1", "a");
            kt1.setText(name);
                Log.d("c2", "a");
        kt2.setText(c.getString(1));
        Log.d("c3", "a");

        kt3.setText(c.getString(2));
        Log.d("12", "a");
        kt4.setText(c.getString(3));
        kt5.setText(c.getString(4));
        kt6.setText(c.getString(5));
        Log.d("13", "a");
        c.close();
        Log.d("14", "a");

}
    else{

        Toast.makeText(this,"Add a Contact", 50).show(); 
    }
    }catch(Exception e){ 
        Toast.makeText(getBaseContext(), "SOme display error", 20).show();
    }
}




public void calledit(View w)
{
    Intent i=new Intent(this,editp.class);
    startActivity(i);
        //setContentView(R.layout.editc);


}


public void onStartp(){ 
    kt1=(TextView)findViewById(R.id.ptext3);
    kt2=(TextView)findViewById(R.id.ptext5);
    kt3=(TextView)findViewById(R.id.ptext7);
    kt4=(TextView)findViewById(R.id.ptext9);
    kt5=(TextView)findViewById(R.id.ptext11);
    kt6=(TextView)findViewById(R.id.ptext13);
}

}

(添加或编辑与保存按钮的配置文件)
editp.class

(To add or edit the profile with save button) editp.class

     public class editp extends Activity {

EditText ed1;
EditText ed2;
EditText ed3;
EditText ed4;
EditText ed5;
EditText ed6;
TextView ptn;

pdatabasehelper phelper;
public void onCreate(Bundle savedInstanceState)
{ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editc);
    phelper=new pdatabasehelper(this);

}

public void onstart(){ 

     ed1=(EditText)findViewById(R.id.jname);
        ed2=(EditText)findViewById(R.id.jeditText1);
      ed3=(EditText)findViewById(R.id.jeditText2);
      ed4=(EditText)findViewById(R.id.jeditText3);
      ed5=(EditText)findViewById(R.id.jeditText4);
     ed6=(EditText)findViewById(R.id.jaeditText1);
      ptn=(TextView)findViewById(R.id.jprofile);
}


public void savemethj(View w)
{ 

    onstart();
    Log.d("tag000", "before allgettext");

    long a;
    Log.d("tag01111", "afterlong");
    String ptrm = ptn.getText().toString();
    try{ 
    phelper.deleterecord(ptrm);
    Log.d("tag02222", "be");
    String edt1=ed1.getText().toString();
    String edt2=ed2.getText().toString();
    Log.d("tag03333", "be11111");
    String edt3=ed3.getText().toString();
    String edt4=ed4.getText().toString();
    Log.d("tag0444444", "be2222");
    String edt5=ed5.getText().toString();
    String edt6=ed6.getText().toString();
    Log.d("tag11111", "after allgettext");

     phelper.deleterecord(ptrm);
    a=phelper.insertpro(ptrm,edt1, edt2, edt3, edt4, edt5, edt6);

    if(a>=1){
        Toast.makeText(getBaseContext(),a+ "Record Successfully Saved", 30).show();
    }

    else{

        Toast.makeText(getBaseContext(),  "Not Saved", 30).show();

    }}
    catch(Exception e)
    { 

       Toast.makeText(getBaseContext(), "Errrrrrrrrrrr", Toast.LENGTH_SHORT).show();

    }


    }

  }

我的记录都保存在数据库中,我可以看到他们,但我不能看一下,我有救首次配置文件的记录,即使我打电话pshow()获取了从数据库中的记录。我想每次我来来分析,我可以看到这些记录主页或布局上无论是从编辑页面返回移动或单击配置文件按钮。
请大家帮我找错误。和借口登录命令,我已经把看到的错误。而且我在这里得到错误:

My records are saved in database i can see them,But i am unable to look at those records in the profile that i have saved first time even i called pshow() to fetch out the record from database. I want everytime i come to profile i can see those records either moving back from edit page or clicking "profile" button on main page or layout. please help me to find error. and excuse the Log commands that i have put to see the errors. and moreover i am getting error here:

  String name= c.getString(0);

由于上次登录我看到在日志文件中运行程序时是:

because last Log i see while running program in log file is:

    Log.d("cmm1", "a");

像一些显示错误与土司在屏幕上显示该错误消息后。

After that error message displayed on the screen with toast like "some display error".

推荐答案

您有很多工作要做,但真正考虑这个。

相反的:

You have a lot of work to do really but consider this.
Instead of:

public void editm(View w){ 
setContentView(R.layout.profilec);
    try{
        pshow();
    }
    catch(Exception e){ 
        Toast.makeText(getBaseContext(), "pshow method", 5).show();
    }
}

您想要的:

Intent edit_profile_intent = new Intent();
edit_profile_intent.setClass(getApplicationContext(), EditProfileActivity.class);
startActivity(edit_profile_intent);

所以,你需要做的几件事情:创建这个新的活动课,在你的清单声明它,编辑您的布局,包括适当的意见,等等。

So you would need to do several things: Create this new activity class, declare it in your manifest, Edit your layout to include the appropriate views, etc.

让您的固定活动堆栈中,然后担心你的数据有问题...

Lets fix your activity stack and then worry about your data problems...

这篇关于获取数据从数据库问题在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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