vendredi 6 mai 2016

Retrieve data SQLite from EditText and display in a new activity it in TextView

i want to retrieve data from the sqlite database from clicking the button OK then I want it to display in a TextView in other activity, like this image output

If possible, can anyone please help me with the code what have I done wrong?

database name certificate with structure

_id integer primary key
certificate_no text
product_name text
company text
valid_from text
valid_to text

DatabaseHelper.java

package com.mjusuf.halal;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper{

    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/mjusuf/databases/";

    private static String DB_NAME = "certificate";

    private SQLiteDatabase myDataBase; 

    private final Context myContext;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DatabaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }   

  /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    public void createDataBase() throws IOException{

        boolean dbExist = checkDataBase();

        if(dbExist){
            //do nothing - database already exist
        }else{

            //By calling this method and empty database will be created into the default system path
               //of your application so we are gonna be able to overwrite that database with our database.
            this.getReadableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }

    }

    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){

            //database does't exist yet.

        }

        if(checkDB != null){

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created empty database in the
     * system folder, from where it can be accessed and handled.
     * This is done by transfering bytestream.
     * */
    private void copyDataBase() throws IOException{

        //Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    public String[] getCertificateDetail() {

        final String TABLE_NAME = "certificate";

        String selectQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db  = this.getReadableDatabase();
        Cursor cursor      = db.rawQuery(selectQuery, null);
        String[] data      = null;

        if (cursor.moveToFirst()) {
            do {
               // get the data into array, or class variable
            } while (cursor.moveToNext());
        }
        cursor.close();
        return data;
    }

    public void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

            if(myDataBase != null)
                myDataBase.close();

            super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

        // Add your public helper methods to access and get content from the database.
       // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
       // to you to create adapters for your views.

}

Manual.java

package com.mjusuf.halal;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
public class Manual extends ActionBarActivity {
    EditText manual = (EditText) findViewById(R.id.form_manual);
    ImageButton reset = (ImageButton) findViewById(R.id.btn_reset);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manual);

        String input = manual.getText().toString();

        reset.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                manual.setText("");
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.manual, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_manual.xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:id="@+id/form_manual"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_marginTop="150dp"
        android:layout_centerHorizontal="true"
        android:textSize="24sp"
        android:gravity="center"
        android:background="@drawable/form_manual"
        android:ems="10"
        android:inputType="number" >
        <requestFocus />
    </EditText>

        <ImageButton
        android:id="@+id/btn_ok"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="50dp"
        android:layout_alignLeft="@+id/form_manual"
        android:layout_below="@+id/form_manual"
        android:background="@drawable/button_ok" />

    <ImageButton
        android:id="@+id/btn_reset"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginRight="30dp"
        android:layout_marginTop="50dp"
        android:layout_alignRight="@+id/form_manual"
        android:layout_below="@+id/form_manual"
        android:background="@drawable/button_reset" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/form_manual"
        android:layout_alignLeft="@+id/form_manual"
        android:layout_marginBottom="50dp"
        android:text="Certificate Number"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

OutputSuccess.java

package com.mjusuf.halal;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class OutputSuccess extends ActionBarActivity {

    TextView certificate_no = (TextView) findViewById(R.id.textView3);
    TextView valid_from = (TextView) findViewById(R.id.textView6);
    TextView valid_to = (TextView) findViewById(R.id.textView9);
    TextView company = (TextView) findViewById(R.id.textView12);
    TextView product_name = (TextView) findViewById(R.id.textView15);


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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.output_success, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_outputsuccess.xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.mjusuf.halal.OutputSuccess" >

    <TextView
        android:id="@+id/textView16"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tableLayout1"
        android:layout_alignLeft="@+id/tableLayout1"
        android:text="Certificate Information"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/cell_shape" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:padding="5dp"
                android:text="Certificate Number" />

            <TextView
                android:id="@+id/textView2"
                android:text=":" />

            <TextView
                android:id="@+id/textView3"
                android:padding="5dp"
                android:text="1234567890" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView4"
                android:padding="5dp"
                android:text="Valid From" />

            <TextView
                android:id="@+id/textView5"
                android:text=":" />

            <TextView
                android:id="@+id/textView6"
                android:padding="5dp"
                android:text="2016-01-01" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView7"
                android:padding="5dp"
                android:text="Valid To" />

            <TextView
                android:id="@+id/textView8"
                android:text=":" />

            <TextView
                android:id="@+id/textView9"
                android:padding="5dp"
                android:text="2018-01-01" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView10"
                android:padding="5dp"
                android:text="Company" />

            <TextView
                android:id="@+id/textView11"
                android:text=":" />

            <TextView
                android:id="@+id/textView12"
                android:padding="5dp"
                android:text="Internation Inch" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView13"
                android:padding="5dp"
                android:text="Product Name" />

            <TextView
                android:id="@+id/textView14"
                android:text=":" />

            <TextView
                android:id="@+id/textView15"
                android:padding="5dp"
                android:text="Noodle" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire