简体   繁体   中英

How to Fetch Database from button's click event to another activity for show inserted record?

I create database and table on button's click event. I also perform insert function on button's click event after database and table creation. I want to inserted record show in next activity. In this activity i take Listview for showing data. I can't use SQLiteOpenHelper class for database. I create database using OpenOrCreateDatabase() method. So please tell me what i do for this???

From what I understand, you want the same button, which sets up your database and inserts data into it, to be take you to another activity and display all the inserted data?

You can do this by setting up an intent on the click of that button which will take you to the required activity. And in that activity's onCreate() you should call the function which gets your data.

Something like this:

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

    userdatabase viewinfo = new userdatabase(this);
    viewinfo.open();
    String data = viewinfo.getData();
    viewinfo.close();

    TextView txtv = (TextView) findViewById(R.id.viewuserdata);
    txtv.setText(String.valueOf(data)); 

}

here getData() is a method which retrieves the required data and is defined in the same class in which you define and create your database.

In my case, getData() looks like this:

public String getData() {                                                                   //Retrieve complete data from database
    String[] columns = new String[]{KEY_ROWID, USER_NAME,USER_HP,USER_LEVEL,USER_ID};
    Cursor c = userDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(USER_NAME);
    int ihp = c.getColumnIndex(USER_HP);
    int ilevel = c.getColumnIndex(USER_LEVEL);
    int iId = c.getColumnIndex(USER_ID);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getInt(ihp) + " " + c.getInt(ilevel);
    }

    return result;
}

EDIT: Just saw that you need to insert data into ListView. I have no experience with that, but maybe this can help.

Create your own application class

public class MyApplication extends Application {

 private  ArrayList myData;

public ArrayList getMyData(){
 return myData;

}
public void putMyData(ArrayList myData){
 this.myData = myData;

}

Mention this in your AndroidManifest.xml file like

<application android:icon="@drawable/icon" android:label="@string/app_name"  android:largeHeap="true" android:name="MyApplication">

Read the data in your first activity and, in your first Activity do like this

MyApplication myApplication=(MyApplication)getApplication();
myApplication.putMyData(myData);

in second activity

MyApplication myApplication=(MyApplication)getApplication();
myData =  myApplication.gettMyData();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM