mardi 19 mai 2015

Read data from the database and displays a random textview

I wanted to get some information from the database and randomly Textview I display. If I read three of the table, what should I do? Related questions, but I did not notice much. Please help me to get it. Thank. Database class:

    package com.database.dbtest;

public class Database extends SQLiteOpenHelper {

    public final String path="data/data/com.database.dbtest/databases";
    public final String Name="dbtest";
    public SQLiteDatabase mydb;
    private final Context mycontext;

    public Database(Context context) {
        super(context, "dbtest", null, 1);
        mycontext=context;

    }

    public void database(){

        boolean checkdb=checkdb();

        if(checkdb){

        }else{

            this.getReadableDatabase();
            try{
            copydatabase();
            }catch(IOException e){

            }
        }

    }

    public void open(){

        mydb=SQLiteDatabase.openDatabase(path+Name,null,SQLiteDatabase.OPEN_READWRITE);

    }

    public void close(){
        mydb.close();
    }
    public boolean checkdb(){
        SQLiteDatabase db=null;
        try{
        db=SQLiteDatabase.openDatabase(path+Name,null,SQLiteDatabase.OPEN_READONLY);
        }
        catch(SQLException e)
        {

        }

        return db !=null ? true:false;
    }

    public void copydatabase() throws IOException{
        OutputStream myOutput=new FileOutputStream(path+Name);
        byte[] buffer=new byte[1024];
        int length;

        InputStream myInput=mycontext.getAssets().open("dbtest");
        while ((length=myInput.read(buffer)) > 0){
            myOutput.write(buffer,0,length);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();

    }

    public String Display(int row,int fild){
        Cursor cu=mydb.query("users", null, null, null, null, null, null);
        cu.moveToPosition(row);
        String name=cu.getString(fild);
        return name;
    }



}

main.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.database.dbtest.Main" >

    <TextView
        android:id="@+id/txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="72dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/txt1"
        android:layout_marginRight="36dp"
        android:text="Button" />

</RelativeLayout>




Aucun commentaire:

Enregistrer un commentaire