Rajesh Bharat Rajesh Bharat - 2 months ago 7
Android Question

I am creating an android app and connecting it with the database. Android programming[ the new boston tutorial no 49-54]

I am creating an android app and trying to connect it with the database. I am following TheNewBoston on Youtube for the code. I have successfully created the app.

But when I run it, on Insert Query, my app hangs up and doesn't respond.

Here is the code:

MyDBHandler.java

public class MyDBHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "newproducts.db";
public static final String TABLE_NAME = "products_table";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "product_name";


public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " +TABLE_NAME+ "("+
COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
COLUMN_NAME+ " TEXT "+
");";
db.execSQL(query);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
onCreate(db);
}
public void add(Products products){
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NAME, products.get_productname());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME,null,contentValues);
db.close();
}

public String dbToString(){
SQLiteDatabase db = getWritableDatabase();
String dbString = "";
String query = "SELECT * FROM "+TABLE_NAME+";";

Cursor cursor = db.rawQuery(query,null);
cursor.moveToFirst();

while(!cursor.isAfterLast()){
if(cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""))!=null){
dbString+= cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""));
dbString += "\n";
}
}
db.close();
return dbString;
}
}


MainActivity.Java

public class MainActivity extends AppCompatActivity {
EditText mkInput;
TextView mkText;
MyDBHandler dbHandler;
public int dbVersion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mkInput = (EditText)findViewById(R.id.mkInput);
mkText = (TextView)findViewById(R.id.textView);
dbVersion = MyDBHandler.DATABASE_VERSION;
dbHandler = new MyDBHandler(this, null, null, dbVersion);
printDatabase();
}

public void addClicked(View view){
Products products = new Products(mkInput.getText().toString());
dbHandler.add(products);
printDatabase();
}
public void deleteClicked(View view){
Toast.makeText(MainActivity.this,"This is a toast",Toast.LENGTH_LONG).show();
}

public void printDatabase(){
String dbString = dbHandler.dbToString();
mkText.setText(dbString);
mkInput.setText("");
}
}


Products.java

public class Products {
private int _id;
private String _productname;

public Products(String _productname) {
this._productname = _productname;
}

public void set_id(int _id) {
this._id = _id;
}

public void set_productname(String _productname) {
this._productname = _productname;
}

public String get_productname() {
return _productname;
}

public int get_id() {
return _id;
}
}


activity_main.xml

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mkInput"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:width="300dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/addButton"
android:layout_below="@+id/mkInput"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="48dp"
android:onClick="addClicked"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/deleteButton"
android:layout_alignBottom="@+id/addButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="deleteClicked"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Default Text"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />


Output Screen
This is the app interface

On Clicking AddButton, App hangs up

Answer

change your dbToString code to this

public String dbToString(){
    SQLiteDatabase db = getWritableDatabase();
    String dbString = "";
    String query = "SELECT * FROM "+TABLE_NAME+";";
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst()){

do{
    if(cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""))!=null){
        dbString+= cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""));
        dbString += "\n";
    }
}while(cursor.moveToNext());
}

db.close();
return dbString;
}

Basically app is going in self loop now.

Notice the difference of moveToNext(). It would move the cursor to next position while isAfterLast just tells whether the cursor is after the last result. see