Maartin1996 Maartin1996 - 1 month ago 5
Java Question

Saving data to database and retrieving it in a ListView

I'm making small to-do app, and I got stuck.

The idea is:

Save the data into the DB -> Retrieve it in a ListView.

At the moment, my app crashes right after I press the "BUILD!" Button, even though I think it should just say "SAVED!" (To make sure the data is saved into the db).

Some hints on how to display that data in ListView would be much appreciated.

P.S.: It's my first app, so don't be harsh

Code:

Mainactivity

package com.example.mrti.todo2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

TaskDbHelper taskDbHelper;
SQLiteDatabase SQLdb;
ArrayAdapter<String> itemsAdapter;
Context context;

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

ListView lvItems = (ListView) findViewById(R.id.listView);
ArrayList<String> items = new ArrayList<String>();

itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);


}

// ADDING YOUR FUTURE GOALS!
public void onBuildButton (View view){
EditText dreamText = (EditText) findViewById(R.id.dreamText);
String task = dreamText.getText().toString();

//db
taskDbHelper = new TaskDbHelper(context);
SQLdb = taskDbHelper.getWritableDatabase();
taskDbHelper.insertData(task, SQLdb);
Toast.makeText(getBaseContext(), "Saved!", Toast.LENGTH_LONG).show();
taskDbHelper.close();
}


}


TaskDBHelper.java

public class TaskDbHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "TASKS.db";
private static final int DB_VERSION = 1;
private static final String CREATE_QUERY =
"CREATE TABLE " + TaskContract.newTaskInfo.TABLE_NAME + " ( " +
TaskContract.newTaskInfo.TASK_NAME + " TEXT NOT NULL);";


public TaskDbHelper(Context context) {
super(context, DATABASE_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DB Operations", "Table created...");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TaskContract.newTaskInfo.TABLE_NAME);
onCreate(db);
}
// For adding new data to database
public void insertData(String task, SQLiteDatabase db){
ContentValues contentValues = new ContentValues();
contentValues.put(TaskContract.newTaskInfo.TASK_NAME, task);
db.insert(TaskContract.newTaskInfo.TABLE_NAME, null, contentValues);
Log.e("DB Operations", "1 record added...");

}
}


TaskContract.java

public class TaskContract {

public static abstract class newTaskInfo{
public static final String TASK_NAME = "task_name"; // Table field
public static final String TABLE_NAME = "task_info"; // Name of the table
}


XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
tools:context="com.example.mrti.todo2.MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#F55449"
android:id="@+id/belowTask"
android:layout_height="230dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dreamText"
android:hint="@string/build_your_dreams"
android:layout_gravity="center|center_vertical"
android:textColor="#FFFFFF"
android:textColorHint="#c5c5c5"
android:inputType="textCapSentences"
android:background="@android:color/transparent"
android:paddingTop="60dp"
android:paddingBottom="0dp"
android:layout_marginBottom="0dp"
android:layout_marginTop="20dp" />
<View
android:id="@+id/UnderLine"
android:layout_width="250dp"
android:layout_gravity="center|center_vertical"
android:layout_height="1dp"
android:layout_below="@+id/dreamText"
android:layout_centerHorizontal="true"
android:background="#FFCD2626"
android:paddingBottom="30dp"
android:layout_marginBottom="0dp"
android:layout_marginTop="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="@string/build"
android:layout_gravity="center"
android:background="#FFCD2626"
android:id="@+id/buildButton"
android:paddingTop="0dp"
android:layout_marginTop="65dp"
android:onClick="onBuildButton" />

<Button
android:layout_width="match_parent"
android:layout_height="30dp"
android:text="@string/build"
android:layout_gravity="center"
android:background="#FFCD2626"
android:id="@+id/button"
android:paddingTop="0dp"
android:layout_marginTop="65dp"
android:onClick="onBuildButton" />
</LinearLayout>

<ListView
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/belowTask"
android:id="@+id/listView" />

</RelativeLayout>

Answer

Your context variable is null and unnecessary.

Activities are contexts, so use that instead

taskDbHelper = new TaskDbHelper(MainActivity.this);
Comments