dimanche 27 septembre 2015

Display Data from SQLite in Listview

I'm not that good in programming and I need your help guys. I only able to display the data in textview. It only shows the last data in database SQLite.

This is my databasehelper

public class DatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.example.jathniel.myapplication/databases/";
private static String DB_NAME = "example.sqlite";
private SQLiteDatabase myDataBase;
private Context myContext = null;

public DatabaseHelper(Context context) {
    super(context, DB_NAME, (SQLiteDatabase.CursorFactory) null, 1);
    this.myContext = context;
}

public void createDataBase() throws IOException {
    boolean dbExist = this.checkDataBase();
    if(!dbExist) {
        this.getReadableDatabase();

        try {
            this.copyDataBase();
        } catch (IOException var3) {
            throw new Error("Error");
        }
    }

}

public void copyDataBase() throws IOException {
    InputStream myInput = this.myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    FileOutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];

    int length;
    while((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public boolean checkDataBase() {
    SQLiteDatabase checkDB = null;

    try {
        String e = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(e, (SQLiteDatabase.CursorFactory)null, 0);
    } catch (SQLiteException var3) {
        ;
    }

    if(checkDB != null) {
        checkDB.close();
    }

    return checkDB != null;
}

public void openDataBase() throws SQLException {
    String myPath = DB_PATH + DB_NAME;
    this.myDataBase = SQLiteDatabase.openDatabase(myPath, (SQLiteDatabase.CursorFactory)null, 0);
}

public synchronized void close() {
    if(this.myDataBase != null) {
        this.myDataBase.close();
    }

    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}


public void createDatabase() {
}

this is the dataDB

public class DataDB {
DatabaseHelper con;
String name,lastname;

public DataDB() {
}

public String getNameDB(Context context) throws SQLException {
    this.con = new DatabaseHelper(context);

    try {
        this.con.createDataBase();
    } catch (IOException var4) {
        ;
    }

    if (!this.con.checkDataBase()) {
        return "ERROR";
    } else {
        this.con.openDataBase();
        SQLiteDatabase db = this.con.getWritableDatabase();

        for (Cursor cursor = db.rawQuery("SELECT name FROM exampleTable", (String[]) null); cursor.moveToNext(); this.name = cursor.getString(0)) {

        }

        this.con.close();
        return this.name;


    }

}
public String getLNameDB(Context context) throws SQLException {
    this.con = new DatabaseHelper(context);

    try {
        this.con.createDataBase();
    } catch (IOException var4) {
        ;
    }

    if(!this.con.checkDataBase()) {
        return "ERROR";
    } else {
        this.con.openDataBase();
        SQLiteDatabase db = this.con.getWritableDatabase();

        for (Cursor cursor = db.rawQuery("SELECT lastname FROM exampleTable", (String[]) null); cursor.moveToNext(); this.lastname = cursor.getString(0)) {

        }


        this.con.close();
        return this.lastname;
    }
    }

I have a main activity composed of textview.

public class MainActivity extends Activity {
DataDB data = new DataDB();
TextView txtName,txtlastName;

public MainActivity() {
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtName = (TextView) findViewById(R.id.txtName);
    txtlastName = (TextView) findViewById(R.id.txtlastName);



     try {
           txtName.setText(data.getNameDB(this));
       } catch (SQLException e) {
           e.printStackTrace();
     }
       try {
          txtlastName.setText(data.getLNameDB(this));
       } catch (SQLException e) {
           e.printStackTrace();
       }
}

Aucun commentaire:

Enregistrer un commentaire