dimanche 14 juin 2015

Trouble in retrieving data from SQLite database(Android Studio)

I am not able to retrieve data from following code::

The APP is getting strucked at Fetching Block. The application is running upto Fetch Block but when it enters the Fetch Block its not responding and emulator is not responding after pressing "Submit" Button.

public void submitButtonClicked(View view) {


    eventName= (EditText) findViewById(R.id.eventName);
    eventDate= (EditText) findViewById(R.id.eventDate);
    sumbitButton= (Button) findViewById(R.id.submit);

    System.out.println("Taken data!");
    String ename= eventName.getText().toString();
    String edate= eventDate.getText().toString();
    String dbString;
    dbAdapter = new DBAdapter(this);
    dbAdapter.open();

    try {
        dbAdapter.createEvent(ename, edate);
    }
    catch (SQLException e) {
        Toast.makeText(MembersActivity.this, "Some problem occurred",
                Toast.LENGTH_LONG).show();

    }
    System.out.println("Written data to table!");
    setContentView(R.layout.event);
    System.out.println("With event file!");

    //Fetch Block starts here
    try {
        dbString = dbAdapter.databaseToString();
        System.out.println("Fetched data!");
        myText.setText(dbString);
        myText.setText(" ");

    }
    catch (Exception e){
        System.out.println("Problem in Fetching data!");
        e.printStackTrace();
    }

}

The function databaseToString is in below ::

//Print out the database as a string
public String databaseToString() {
    String dbString="";
    mDbHelper = new DBHelper(mCtx);
    SQLiteDatabase db = mDbHelper.getReadableDatabase();
    String query="select * from events where 1";

    //Cursor point to a location in your results
    Cursor c = db.rawQuery(query,null);

    //Move to the first row in your results
    c.moveToFirst();

    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex("eventname"))!= null){
            dbString+=c.getString(c.getColumnIndex("eventname"));
            //dbString+=c.getString(c.getColumnIndex("eventdate"));

            dbString+="\n";

        }
    }
    db.close();
    return dbString;
}

Aucun commentaire:

Enregistrer un commentaire