Why is my loop only outputting the last result? - java

I'm building an Android Studio app in which data can be added to a SQLite database. When I want to output the data to a page it only shows the last result in the database (it overrides). How can i prevent this from happening and output all the data?
Here's my code:
public void viewAll() {
Cursor res = myDb.getAllData();
if (res.getCount() == 0) {
// show error
System.out.println("No Data Found");
return;
}
while (res.moveToNext()) {
System.out.println("Id: " + res.getString(0) + "\n");
System.out.println("Title: " + res.getString(1) + "\n");
System.out.println("Description: " + res.getString(2) + "\n");
System.out.println("Location: " + res.getString(4) + "\n\n");
TextView textViewID = (TextView) findViewById(R.id.textViewID);
textViewID.setText("Id: " + res.getString(0) + "\n");
TextView textViewName = (TextView) findViewById(R.id.textViewName);
textViewName.setText("Name: " + res.getString(1) + "\n");
TextView textViewDescription = (TextView) findViewById(R.id.textViewDescription);
textViewDescription.setText("Description: " + res.getString(2) + "\n");
TextView textViewLocation = (TextView) findViewById(R.id.textViewLocation);
textViewLocation.setText("Location: " + res.getString(4) + "\n");
}
// show all the data
}
The expected results were all the data from the database but it only output the last entry.
EDIT
MyActivity.java
package com.example.triptracker;
import android.content.Intent;
import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MomentsActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_moments:
Intent intent = new Intent(MomentsActivity.this, MomentsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
break;
case R.id.navigation_addmoment:
Intent intent2 = new Intent(MomentsActivity.this, AddMomentActivity.class);
intent2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent2);
break;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDb = new DatabaseHelper(this);
setContentView(R.layout.activity_moments);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
mList = this.findViewById(R.id.my_listview);
manageListView();
}
DatabaseHelper myDb;
EditText editTitle, editDescription, editLocation;
Button btnAddData;
Button btnViewAll;
SimpleCursorAdapter mSCA; //Adapts/Handles the data for the listview
ListView mList;
Cursor mCsr;
int[] item_layout_ids_for_list = new int[]{
R.id.textview_id,
R.id.textview_name,
R.id.textview_description,
R.id.textview_location
};
String[] columns_to_list = new String[]{
DatabaseHelper.COL_1,
DatabaseHelper.COL_2,
DatabaseHelper.COL_3,
DatabaseHelper.COL_4
};
private void manageListView() {
mCsr = myDb.getAllData();
if (mSCA == null) {
// Builds the Adapter for the List
mSCA = new SimpleCursorAdapter(
this,
R.layout.mylistview_item, mCsr,
columns_to_list,
item_layout_ids_for_list,
0
);
mList.setAdapter(mSCA); // Ties the Adapter to the ListView
} else {
mSCA.swapCursor(mCsr); // Refresh the List
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mCsr.close();
}
#Override
protected void onResume() {
super.onResume();
manageListView();
}
}
DatabaseHelper.java
package com.example.triptracker;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "triptracker.db";
public static final String TABLE_NAME = "trip_table";
public static final String COL_1 = "trip_id";
public static final String COL_2 = "trip_title";
public static final String COL_3 = "trip_description";
public static final String COL_4 = "trip_image";
public static final String COL_5 = "trip_location";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (" + COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_2 + " TEXT, " + COL_3 + " TEXT, " + COL_4 + " TEXT, " + COL_5 + " TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}
// TODO: Add image functionality.
public boolean insertData(String title, String description, String location) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, title);
contentValues.put(COL_3, description);
// contentValues.put(COL_4, image);
contentValues.put(COL_5, location);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
}
It's outputting these errors:
2019-05-09 14:23:47.959 21253-21253/com.example.triptracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.triptracker, PID: 21253
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.triptracker/com.example.triptracker.MomentsActivity}: java.lang.IllegalArgumentException: column '_id' does not exist. Available columns: [trip_id, trip_title, trip_description, trip_image, trip_location]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist. Available columns: [trip_id, trip_title, trip_description, trip_image, trip_location]
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:340)
at android.widget.CursorAdapter.init(CursorAdapter.java:180)
at android.widget.CursorAdapter.<init>(CursorAdapter.java:157)
at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:96)
at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
at com.example.triptracker.MomentsActivity.manageListView(MomentsActivity.java:81)
at com.example.triptracker.MomentsActivity.onCreate(MomentsActivity.java:52)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
2019-05-09 14:23:47.983 21253-21253/com.example.triptracker I/Process: Sending signal. PID: 21253 SIG: 9

The reason is that for each iteration of the loop you set the values of the TextViews to the values for that iteration, as such you will only likely see the last row.
You could use :-
public void viewAll() {
Cursor res = myDb.getAllData();
if (res.getCount() == 0) {
// show error
System.out.println("No Data Found");
return;
}
TextView textViewID = (TextView) findViewById(R.id.textViewID);
textViewID.setText("");
TextView textViewName = (TextView) findViewById(R.id.textViewName);
textViewName.setText("");
TextView textViewDescription = (TextView) findViewById(R.id.textViewDescription);
textViewDescription.setText("");
TextView textViewLocation = (TextView) findViewById(R.id.textViewLocation);
textViewLocation.setText("");
while (res.moveToNext()) {
System.out.println("Id: " + res.getString(0) + "\n");
System.out.println("Title: " + res.getString(1) + "\n");
System.out.println("Description: " + res.getString(2) + "\n");
System.out.println("Location: " + res.getString(4) + "\n\n");
textViewId.setText(textViewID.getText().toString()+res.getString(0) + "\n");
textViewName.setText(textViewName.getText().toString()+"Name: " + res.getString(1) + "\n");
textViewDescription.setText(textViewDescription.getText().toString() + "Description: " + res.getString(2) + "\n");
textViewLocation.setText(textViewLocation.getText().toString() + "Location: " + res.getString(4) + "\n");
}
}
Or even better as
- String concatenation in a loop is inefficient
- This also only appends a new line character after the first.
:-
public void viewAll() {
Cursor res = myDb.getAllData();
if (res.getCount() == 0) {
// show error
System.out.println("No Data Found");
return;
}
StringBuilder idsb = new StringBuilder();
StringBuilder namesb = new StringBuilder();
StringBuilder descsb = new StringBuilder();
StringBuilder locsb = new StringBuilder();
String endofline = "";
TextView textViewID = (TextView) findViewById(R.id.textViewID);
TextView textViewName = (TextView) findViewById(R.id.textViewName);
TextView textViewDescription = (TextView) findViewById(R.id.textViewDescription);
TextView textViewLocation = (TextView) findViewById(R.id.textViewLocation);
while (res.moveToNext()) {
System.out.println("Id: " + res.getString(0) + "\n");
System.out.println("Title: " + res.getString(1) + "\n");
System.out.println("Description: " + res.getString(2) + "\n");
System.out.println("Location: " + res.getString(4) + "\n\n");
idsb.append("Id: ").append(res.getString(0)).append(endofline);
namesb.append("Name: ").append(res.getString(1)).append(endofline);
descsb.append("Description: ").append(res.getString(2)).append(endofline);
locsb.append("Location: ").append(res.getString(4)).append(endofline);
if (endofline.length() == 0) {
endofline = "\n";
}
}
textViewId.setText(idsb.toString());
textViewName.setText(namesb.toString());
textViewDescription.setText(descsb.toString());
textViewLocation.setText(locsb.toString());
}
However, you probably want to use a ListView or a RecyclerView.
Additional - Use a ListView
Important Note
This example uses a CursorAdapter which make life very easy BUT they require that a column named specifically _id exists (id will not do). You may have to change you table so that the column is named _id (you can use the constant BaseColumns._ID as is used in the DBHelper below). There are other ways such as createing an _id column in the SELECT (query) e.g. SELECT rowid AS _id,* FROM the_table.
1 In the activity's layout add (replace the textviews for id/name/description/location) a ListView making sure you give it an id e.g. :-
<ListView
android:id="#+id/my_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
2 Create a new layout mylistview_item.xml, this will have 4 textviews e.g.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textview_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
This will be the layout used for each row (item) in the listview
3 Add the following class variables :-
SimpleCursorAdapter mSCA; //Adapts/Handles the data for the listview
ListView mList;
Cursor mCsr;
4 Add the following int array as a class variable :-
int[] item_layout_ids_for_list = new int[]{
R.id.textview_id,
R.id.textview_name,
R.id.textview_description,
R.id.textview_location
};
note that these are the id's of the TextView in the layout mylistview_item.xml and will be used by the adapter to map the data from the columns to the TextViews.
5 Similarily add a String array for the column names from which the data is to be retrieved; BUT NOTE you will/may have to change the column names (the following uses CONSTANTS defined in the Database Helper (it is suggested to define such constants and always use the constants rather then hard code the names)) e.g. :-
String[] columns_to_list = new String[]{
DBHelper.COL_MYTABLE_ID,
DBHelper.COl_MYTABLE_NAME,
DBHelper.COL_MYTABLE_DESCRIPTION,
DBHelper.COl_MYTABLE_LOCATION
};
6 Add the following method to the activity :-
private void manageListView() {
mCsr = myDb.getAllData();
if (mSCA == null) {
// Builds the Adapter for the List
mSCA = new SimpleCursorAdapter(
this,
R.layout.mylistview_item, mCsr,
columns_to_list,
item_layout_ids_for_list,
0
);
mList.setAdapter(mSCA); // Ties the Adapter to the ListView
} else {
mSCA.swapCursor(mCsr); // Refresh the List
}
}
This as the method says manages the ListView. It gets the data from the db into a Cursor, constructs (instantiates) the SimpleCursorAdapter, if it hasn't been instantiated and then it ties the adapter to the ListView. If the adapter has been instantiated it tells the adapter that there is a new (changed) cursor (so the list is refreshed).
7
Add the following lines to the activity's onCreate method :-
mList = this.findViewById(R.id.my_listview);
myDb = new DBHelper(this); //<<<<<<<<<< SHOULD ALREADY HAVE SOMETHING LIKE THIS LEAVE AS IT IS
manageListView();
8 Override the onResume and onDestroy methods of your activity using :-
#Override
protected void onDestroy() {
super.onDestroy();
mCsr.close();
}
#Override
protected void onResume() {
super.onResume();
manageListView();
}
This isn't essential as yet but good practice.
onDestroy closes the when the activity is destroyed Cursor (Cursors should be closed when finished with)
less important for the main activity
onResume is overidden to call the manageListView() method (yet to be added) and will result in the listview showing the latest data upon return from another activity.
The activity I used to test this is :-
public class MainActivity extends AppCompatActivity {
DBHelper myDb;
SimpleCursorAdapter mSCA; //Adapts/Handles the data for the listview
ListView mList;
Cursor mCsr;
String[] columns_to_list = new String[]{
DBHelper.COL_MYTABLE_ID,
DBHelper.COl_MYTABLE_NAME,
DBHelper.COL_MYTABLE_DESCRIPTION,
DBHelper.COl_MYTABLE_LOCATION
};
int[] item_layout_ids_for_list = new int[]{
R.id.textview_id,
R.id.textview_name,
R.id.textview_description,
R.id.textview_location
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mList = this.findViewById(R.id.my_listview);
myDb = new DBHelper(this);
forTestingAddSomeData();
manageListView();
}
private void manageListView() {
mCsr = myDb.getAllData();
if (mSCA == null) {
// Builds the Adapter for the List
mSCA = new SimpleCursorAdapter(
this,
R.layout.mylistview_item, mCsr,
columns_to_list,
item_layout_ids_for_list,
0
);
mList.setAdapter(mSCA); // Ties the Adapter to the ListView
} else {
mSCA.swapCursor(mCsr); // Refresh the List
}
}
private void forTestingAddSomeData() {
if(DatabaseUtils.queryNumEntries(myDb.getWritableDatabase(),DBHelper.TABLE_MYTABLE) < 1) {
myDb.add("Test001","This is a test","Home");
myDb.add("Test002","For Testing","Garage");
myDb.add("Test003","Test using this","Loft");
myDb.add("Test004","Yet again for testing","Cupboard");
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mCsr.close();
}
#Override
protected void onResume() {
super.onResume();
manageListView();
}
}
Note forTestingAddSomeData and the call to the method are just to add some testing data. Adding and using this method (so I could display some data) was omitted from the step by step guide.
In case you have problems or want to use the full code then the DBHelper class is :-
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydb";
public static final int DBVERSION = 1;
public static final String TABLE_MYTABLE = "mytable";
public static final String COL_MYTABLE_ID = BaseColumns._ID;
public static final String COl_MYTABLE_NAME = "name";
public static final String COL_MYTABLE_DESCRIPTION = "description";
public static final String COl_MYTABLE_LOCATION = "location";
SQLiteDatabase mDB;
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_MYTABLE +
"(" +
COL_MYTABLE_ID + " INTEGER PRIMARY KEY," +
COl_MYTABLE_NAME + " TEXT, " +
COL_MYTABLE_DESCRIPTION + " TEXT, " +
COl_MYTABLE_LOCATION + " TEXT" +
")"
);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long add(String name, String description, String location) {
ContentValues cv = new ContentValues();
cv.put(COl_MYTABLE_NAME,name);
cv.put(COL_MYTABLE_DESCRIPTION,description);
cv.put(COl_MYTABLE_LOCATION,location);
return mDB.insert(TABLE_MYTABLE,null,cv);
}
public Cursor getAllData() {
return mDB.query(TABLE_MYTABLE,null,null,null,null,null,null);
}
}
Result
The result of running the above is :-

Related

How to Retrieve Multiple data when clicked on listview?

I want all edit text content that I have saved in SQL to be displayed on the edit bills activity when I click on the list item, but I am only able to retrieve the name and display it again in the intent. Rather than saving another data it should update the record with the new data if I save the existing record another time in the editbills_activity.
Here is my DBAdapter.java
package com.example.dhruv.bills;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_AMOUNT = "amount";
public static final String KEY_DUEDATE = "duedate";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "billsdb";
private static final String DATABASE_TABLE = "bills";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE =
"create table if not exists assignments (id integer primary key autoincrement, "
+ "name VARCHAR not null, amount VARCHAR, duedate date );";
// Replaces DATABASE_CREATE using the one source definition
private static final String TABLE_CREATE =
"CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY, " + // AUTOINCREMENT NOT REQD
KEY_NAME + " DATE NOT NULL, " +
KEY_AMOUNT + " VARCHAR ," +
KEY_DUEDATE + " DATE " +
")";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(TABLE_CREATE); // NO need to encapsulate in try clause
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts"); //????????
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a record into the database---
public long insertRecord(String name, String amount, String duedate)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_AMOUNT, amount);
initialValues.put(KEY_DUEDATE, duedate);
//return db.insert(DATABASE_TABLE, null, initialValues);
// Will return NULL POINTER EXCEPTION as db isn't set
// Replaces commented out line
return DBHelper.getWritableDatabase().insert(DATABASE_TABLE,
null,
initialValues
);
}
//---deletes a particular record---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the records--- SEE FOLLOWING METHOD
public Cursor getAllRecords()
{SQLiteDatabase db = DBHelper.getWritableDatabase();
String query ="SELECT * FROM " + DATABASE_TABLE;
Cursor data = db.rawQuery(query,null);
return data;
}
//As per getAllRecords but using query convenience method
public Cursor getAllAsCursor() {
return DBHelper.getWritableDatabase().query(
DATABASE_TABLE,
null,null,null,null,null,null
);
}
public Cursor getItemID(String name) {
SQLiteDatabase db = DBHelper.getWritableDatabase();
String query = "SELECT " + KEY_ROWID + " FROM " + DATABASE_TABLE +
" WHERE " + KEY_NAME + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
//---retrieves a particular record--- THIS WILL NOT WORK - NO SUCH TABLE
/* public Cursor getRecord()
{String query1 ="SELECT * FROM" + KEY_TITLE;
Cursor mCursor = db.rawQuery(query1,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}*/
// Retrieve a row (single) according to id
public Cursor getRecordById(long id) {
return DBHelper.getWritableDatabase().query(
DATABASE_TABLE,
null,
KEY_ROWID + "=?",
new String[]{String.valueOf(id)},
null,null,null
);
}
//---updates a record---
/* public boolean updateRecord(long rowId, String name, String amount, String duedate)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_AMOUNT, amount);
args.put(KEY_DUEDATE, duedate);
String whereclause = KEY_ROWID + "=?";
String[] whereargs = new String[]{String.valueOf(rowId)};
// Will return NULL POINTER EXCEPTION as db isn't set
//return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
// Replaces commented out line
return DBHelper.getWritableDatabase().update(DATABASE_TABLE,
args,
whereclause,
whereargs
) > 0;
}*/
}
Here is my Bills.java
(MainActivity)
Problem: Bills.java has the list view that shows the intent whenever the item in list view is clicked, but it does not put the amount and date or update the record. Instead it saves another record.
Solution: I want to retrieve it and display all (name ,amount,duedate) and instead of saving another record it should update it.
package com.example.dhruv.bills;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class bills extends AppCompatActivity {
DBAdapter dbAdapter;
ListView mrecycleview;
private static final String TAG ="assignments";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bills);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mrecycleview =(ListView) findViewById(R.id.mRecycleView);
dbAdapter = new DBAdapter(this);
// mlistview();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),Editbills.class);
startActivity(i);
}
});
mlistview();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_bills, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void mlistview(){
Log.d(TAG,"mlistview:Display data in listview");
Cursor mCursor = dbAdapter.getAllRecords();
ArrayList<String> listData = new ArrayList<>();
while (mCursor.moveToNext()){
listData.add(mCursor.getString(1));
}
ListAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,listData);
mrecycleview.setAdapter(adapter);
mrecycleview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
Log.d(TAG, "onItemClick: You Clicked on " + name);
Cursor data = dbAdapter.getItemID(name); //get the id associated with that name
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
Log.d(TAG, "onItemClick: The ID is: " + itemID);
Intent editScreenIntent = new Intent(bills.this, Editbills.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
startActivity(editScreenIntent);
}
else{
}
}
});
}
}
here is my editbills.java code
package com.example.dhruv.bills;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class Editbills extends AppCompatActivity {
Button button;
private static final String Tag= "assignments";
DBAdapter db = new DBAdapter(this);
private String selectedName;
private int selectedID;
DBAdapter dbAdapter;
private EditText editText,editText2,editText3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editbills);
button=(Button)findViewById(R.id.button);
editText =(EditText)findViewById(R.id.editText);
editText2=(EditText)findViewById(R.id.editText2);
editText3 =(EditText)findViewById(R.id.editText3);
//get the intent extra from the ListDataActivity
Intent receivedIntent = getIntent();
//now get the itemID we passed as an extra
selectedID = receivedIntent.getIntExtra("id",-1); //NOTE: -1 is just the default value
//now get the name we passed as an extra
selectedName = receivedIntent.getStringExtra("name");
//set the text to show the current selected name
editText.setText(selectedName);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("test", "adding");
db.open();
long id = db.insertRecord(editText.getText().toString(), editText2.getText().toString(), editText3.getText().toString());
db.close();
Toast.makeText(Editbills.this," Added", Toast.LENGTH_LONG).show();
Intent q = new Intent(getApplicationContext(),bills.class);
startActivity(q);
}
});
}
}
There are two parts/questions.
First to retrieve the data, so that it can be displayed in the editbills activity, you can utilise the row's id that is passed to the activity in conjunction with the getRecordById method.
I'd suggest that adding a method to the editbills class will simplyfy matters.
private void populateDisplay(int id) {
Cursor csr = dbAdapter.getRecordById(id);
if (csr.moveToFirst()) {
editText.setText(csr.getString(csr.getColumnIndex(DBAdapter.KEY_NAME)));
editText2.setText(csr.getString(csr.getColumnIndex(DBAdapter.KEY_AMOUNT)));
editText3.setText(csr.getString(csr.getColumnIndex(DBAdapter.KEY_DUEDATE)));
}
csr.close();
}
You could invoke the above method after retrieving the id from the Intent using :-
populateDisplay(selectedID);
To update based upon the content's of the EditTexts un-comment the updateRecord method in DBAdapter.java and the change :-
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("test", "adding");
db.open();
long id = db.insertRecord(editText.getText().toString(), editText2.getText().toString(), editText3.getText().toString());
db.close();
Toast.makeText(Editbills.this," Added", Toast.LENGTH_LONG).show();
Intent q = new Intent(getApplicationContext(),bills.class);
startActivity(q);
}
});
to :-
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (dbAdapter.updateRecord(
selectedId,
editText.getText().toString(),
editText2.getText().toString(),
editText3.getText().toString())
) {
Log.d("TEST","Row successfully updated.");
Toast.makeText(getApplicationContext(),"Row Updated.",Toast.LENGTH_LONG).show();
populateDisplay(selectedId);
} else {
Toast.makeText(getApplicationContext(),"Row not Updated",Toast.LENGTH_LONG).show();
}
Intent q = new Intent(getApplicationContext(),bills.class);
startActivity(q);
}
});
You will additionally have to add a line initialise the dbAdapter i.e. dbAdapter = new DBAdapter(this); i.d suggest adding this line immediately after the line setContentView(R.layout.activity_editbills);
Note the above code is in-principle and has not been thoroughly tested so it may contains errors.
Edit complete working example :-
The following code is basically an implementation of the above, except modified to utilise a Bill class. Rather than an ArrayList<String> as the source for the Adapter it has ArrayList<Bill>.
Thus all the data (id, name, amount and duedate) is available.
You should notice that I've overridden the toString method to return a String that combines the name, amount and duedate. The ArrayAdapter uses the object's toString method to populate the view.
Saying that the critical value is the rowid or an alias of rowid as this can be used to identify a row even if the other data is identical (as would happen when running this example more than once due to the addSomeData method). Hence, only the id is extracted and passed to the Editbills activity when an item in the list is long clicked (changed from just clicked to reduce the potential for accidental use).
Bill.java
public class Bill {
private long id;
private String name;
private String amount;
private String duedate;
public Bill(long id, String name, String amount, String duedate) {
this.id = id;
this.name = name;
this.amount = amount;
this.duedate = duedate;
}
/*
Note if this Constructor used then setId should be used
*/
public Bill(String name, String amount, String duedate) {
new Bill(0,name,amount,duedate);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getDuedate() {
return duedate;
}
public void setDuedate(String duedate) {
this.duedate = duedate;
}
/*
As ArrayAdapter uses toString method change this
to return all items
*/
#Override
public String toString() {
return name + " " + amount + " " + duedate;
}
}
MainActivity.java
This is the equivalent to your Bills.java without a lot of the bloat such as FAB. :-
public class MainActivity extends AppCompatActivity {
public static final String ID_INTENTEXTRA = DBAdapter.KEY_ROWID + "_INTENTEXTRA"; //<<<< ADDED
DBAdapter mDBAdapter;
Cursor mCsr;
ListView mrecycleview;
ArrayAdapter adapter; //<<<< NOT ListAdapter
Context context; // ADDED
private static final String TAG ="assignments";
ArrayList<Bill> mBillList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
mrecycleview = this.findViewById(R.id.mRecycleView);
mDBAdapter = new DBAdapter(this);
addSomeData();
adapter = new ArrayAdapter<Bill>(
this,
android.R.layout.simple_list_item_1,
mBillList
);
mrecycleview.setAdapter(adapter);
mrecycleview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Bill b = (Bill) adapter.getItem(position);
Intent i = new Intent(context,Editbills.class);
i.putExtra(ID_INTENTEXTRA,b.getId()); //Get ID
startActivity(i);
return true;
}
});
rebuildBillList();
}
//<<<< ADDED Method to refresh the ListView resumed
#Override
protected void onResume() {
super.onResume();
rebuildBillList();
}
// Add some data (note will add 2 rows each time it is run)
private void addSomeData() {
mDBAdapter.insertRecord("Bill1","2018-10-02","English");
mDBAdapter.insertRecord("Bill2","2018-09-03","Mathematics");
mDBAdapter.insertRecord("Bill3","2018-11-04", "Geography");
}
public void rebuildBillList() {
mBillList.clear();
mCsr = mDBAdapter.getAllAsCursor();
while (mCsr.moveToNext()) {
mBillList.add(new Bill(
mCsr.getLong(mCsr.getColumnIndex(DBAdapter.KEY_ROWID)),
mCsr.getString(mCsr.getColumnIndex(DBAdapter.KEY_NAME)),
mCsr.getString(mCsr.getColumnIndex(DBAdapter.KEY_AMOUNT)),
mCsr.getString(mCsr.getColumnIndex(DBAdapter.KEY_DUEDATE))
)
);
}
adapter.notifyDataSetChanged();
}
}
Note the above adds 3 rows each time it is run.
Editbills.java
public class Editbills extends AppCompatActivity {
Button button;
private static final String Tag = "assignments";
DBAdapter db = new DBAdapter(this);
private String selectedName;
private long selectedID;
DBAdapter dbAdapter;
private EditText editText,editText2,editText3;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editbills);
context = this;
dbAdapter = new DBAdapter(this); //<<<< ADDED
button=(Button)findViewById(R.id.button);
editText =(EditText)findViewById(R.id.edittext);
editText2=(EditText)findViewById(R.id.edittext2);
editText3 =(EditText)findViewById(R.id.edittext3);
//get the intent extra from the ListDataActivity
Intent receivedIntent = getIntent();
//now get the itemID we passed as an extra
selectedID = receivedIntent.getLongExtra(MainActivity.ID_INTENTEXTRA,-1L); //NOTE: -1 is just the default value
populateDisplay(selectedID);
//now get the name we passed as an extra
//selectedName = receivedIntent.getStringExtra("name");
//set the text to show the current selected name
//editText.setText(selectedName); <<<< commented out
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*
Log.d("test", "adding");
db.open();
long id = db.insertRecord(editText.getText().toString(), editText2.getText().toString(), editText3.getText().toString());
db.close();
Toast.makeText(Editbills.this," Added", Toast.LENGTH_LONG).show();
*/
/*
* <<<< Not the way as it ends/closes the existing activity
Intent q = new Intent(getApplicationContext(),MainActivity.class);
startActivity(q);
*/
String toast = "Updated.";
if (dbAdapter.updateRecord(selectedID,
editText.getText().toString(),
editText2.getText().toString(),
editText3.getText().toString())) {
} else {
toast = "Not Updated.";
}
Toast.makeText(context,toast,Toast.LENGTH_LONG).show();
finish(); //<<<< ends/closes this activity and returns to calling activity
}
});
}
private void populateDisplay(long id) {
Cursor csr = dbAdapter.getRecordById(id);
if (csr.moveToFirst()) {
editText.setText(csr.getString(csr.getColumnIndex(DBAdapter.KEY_NAME)));
editText2.setText(csr.getString(csr.getColumnIndex(DBAdapter.KEY_AMOUNT)));
editText3.setText(csr.getString(csr.getColumnIndex(DBAdapter.KEY_DUEDATE)));
}
csr.close();
}
}
Basically as per the original answer.
DBAdapter.java
public class DBAdapter {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_AMOUNT = "amount";
public static final String KEY_DUEDATE = "duedate";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "billsdb";
private static final String DATABASE_TABLE = "bills";
private static final int DATABASE_VERSION = 2;
// Replaces DATABASE_CREATE using the one source definition
private static final String TABLE_CREATE =
"CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY, " + // AUTOINCREMENT NOT REQD
KEY_NAME + " DATE NOT NULL, " +
KEY_AMOUNT + " VARCHAR ," +
KEY_DUEDATE + " DATE " +
")";
private Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(TABLE_CREATE); // NO need to encapsulate in try clause
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts"); //????????
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
//---opens the database--- NOT NEEDED
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database--- NOT NEEDED
public void close()
{
DBHelper.close();
}
//---insert a record into the database---
public long insertRecord(String name, String amount, String duedate)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_AMOUNT, amount);
initialValues.put(KEY_DUEDATE, duedate);
return DBHelper.getWritableDatabase().insert(DATABASE_TABLE,
null,
initialValues
);
}
//---deletes a particular record---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the records--- SEE FOLLOWING METHOD
public Cursor getAllRecords()
{SQLiteDatabase db = DBHelper.getWritableDatabase();
String query ="SELECT * FROM " + DATABASE_TABLE;
Cursor data = db.rawQuery(query,null);
return data;
}
//As per getAllRecords but using query convenience method
public Cursor getAllAsCursor() {
return DBHelper.getWritableDatabase().query(
DATABASE_TABLE,
null,null,null,null,null,null
);
}
public Cursor getItemID(String name) {
SQLiteDatabase db = DBHelper.getWritableDatabase();
String query = "SELECT " + KEY_ROWID + " FROM " + DATABASE_TABLE +
" WHERE " + KEY_NAME + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
// Retrieve a row (single) according to id
public Cursor getRecordById(long id) {
return DBHelper.getWritableDatabase().query(
DATABASE_TABLE,
null,
KEY_ROWID + "=?",
new String[]{String.valueOf(id)},
null,null,null
);
}
//---updates a record---
public boolean updateRecord(long rowId, String name, String amount, String duedate) {
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_AMOUNT, amount);
args.put(KEY_DUEDATE, duedate);
String whereclause = KEY_ROWID + "=?";
String[] whereargs = new String[]{String.valueOf(rowId)};
return DBHelper.getWritableDatabase().update(DATABASE_TABLE,
args,
whereclause,
whereargs
) > 0;
}
}
Basically as was except that upDateRecord has been un-commented, again as per the original answer.
Results
1st Run - MainActivity (Bills) :-
Long Click Bill2 (note changed from Click as Long Click is less prone to accidental use)
Change data (Update button not clicked)
Update Button Clicked (Toast was Updated.)
Note use of finish() to return to the invoking activity and then the use of onResume to refresh the list according to the updated data (possibly your next question).
using startActivity will simply result in issues.
Note Values are date and course rather then amount and duedate because I didn't change the values in the addSomeData method (easily done).
the actual data itself is irrelevant to the process.

Program crashes the moment I start typing in the search bar

When I run the program, everything looks fine but the moment I type a letter in the search bar, the program crashes immediately.It mostly shows Exceptions at searchBar() function in LibraryDbAdapter class and at onQueryTextChange(String newText) in SearchViewActivity class.I am using ListView too.
LibraryDbAdapter.java
package com.example.waheed.library;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Waheed on 6/30/17.
*/
public class LibraryDbAdapter {
public static final String KEY_ROWID = "rowid";
//public static final String KEY_LIBRARY = "library";
public static final String KEY_BOOKNAME = "bookname";
public static final String KEY_BOOKAUTHOR = "bookauthor";
public static final String KEY_STORENAME = "storename";
public static final String KEY_STOREADDRESS = "storeaddress";
public static final String KEY_STORELAT = "storelat";
public static final String KEY_STORELONG = "storelong";
public static final String KEY_SEARCH = "searchData";
private static final String TAG = "LibraryDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "LibraryData";
private static final String FTS_VIRTUAL_TABLE = "LibraryInfo";
private static final int DATABASE_VERSION = 1;
//Create a FTS3 Virtual Table for fast searches
private static final String DATABASE_CREATE =
"CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + " USING fts3(" +
// KEY_LIBRARY + "," +
KEY_BOOKNAME + "," +
KEY_BOOKAUTHOR + "," +
KEY_STORENAME + "," +
KEY_STOREADDRESS + "," +
KEY_STORELAT + "," +
KEY_STORELONG + "," +
KEY_SEARCH + "," +
" UNIQUE (" + KEY_BOOKNAME + "));";
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
onCreate(db);
}
}
public LibraryDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public LibraryDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createLibrary( String bookname, String bookauthor, String storename, String storeaddress, String storelat, String storelong) {
ContentValues initialValues = new ContentValues();
String searchValue = bookname;
//initialValues.put(KEY_LIBRARY, library);
initialValues.put(KEY_BOOKNAME, bookname);
initialValues.put(KEY_BOOKAUTHOR, bookauthor);
initialValues.put(KEY_STORENAME, storename);
initialValues.put(KEY_STOREADDRESS, storeaddress);
initialValues.put(KEY_STORELAT, storelat);
initialValues.put(KEY_STORELONG, storelong);
initialValues.put(KEY_SEARCH, searchValue);
return mDb.insert(FTS_VIRTUAL_TABLE, null, initialValues);
}
public Cursor searchBook(String inputText) throws SQLException {
Log.w(TAG, inputText);
String query = "SELECT " +
KEY_BOOKNAME +
" from " + FTS_VIRTUAL_TABLE +
" where " + KEY_SEARCH + " MATCH '" + inputText + "';";
Log.w(TAG, query);
Cursor mCursor = mDb.rawQuery(query, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean deleteAllLibrary() {
int doneDelete = 0;
doneDelete = mDb.delete(FTS_VIRTUAL_TABLE, null, null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
}
SearchViewActivity.java
package com.example.waheed.library;
import java.util.Calendar;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class SearchViewActivity extends Activity implements
SearchView.OnQueryTextListener,
SearchView.OnCloseListener {
private ListView mListView;
private SearchView searchView;
private LibraryDbAdapter mDbHelper;
private TextView inspectionDate;
private TextView bookName;
private TextView bookAuthor;
private TextView storeName;
private TextView storeAddress;
private TextView storeLat;
private TextView storeLong;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searchView = (SearchView) findViewById(R.id.search);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
mListView = (ListView) findViewById(R.id.list);
inspectionDate = (TextView) findViewById(R.id.inspectionDate);
displayDate();
mDbHelper = new LibraryDbAdapter(this);
mDbHelper.open();
//Clean all Customers
mDbHelper.deleteAllLibrary();
//Add some Customer data as a sample
mDbHelper.createLibrary("How to Win Friends and Influence People", "Dale Carnegie", "Book Centre", " Haider Rd, Rawalpindi 46000", "33.596651", "73.049094");
mDbHelper.createLibrary("The 7 Habits of Highly Effective People", "Stephen Covey", "Idris Book Bank", "Bank Rd, Rawalpindi 46000", "33.595789", "73.052405");
mDbHelper.createLibrary("Think and Grow Rich", "Napoleon Hill", "Students Book Company", "Bank Rd, Rawalpindi 44000", "33.596524", "73.051051");
mDbHelper.createLibrary("Psycho-Cybernetics", "Maxwell Maltz", "Old Book Bank", "Green Super Market, Kashmir Rd, Saddar, Rawalpindi, Punjab", "33.595810", "73.051530");
mDbHelper.createLibrary("How to stop worrying and start living", "Dale Carnegie", "Saeed Book Bank", "F-, Street 7, Islamabad 44000", "33.722446", "73.058763");
mDbHelper.createLibrary("A New Earth", " Eckhart Tolle", "Variety Books", "41-BC, Bank Rd, Saddar, Rawalpindi 46000", "33.597497", "73.049917");
mDbHelper.createLibrary("Blink: The Power of Thinking Without Thinking", "Malcolm Gladwell", "Rehan Book Shop", "Islamabad", "33.689734", "73.031652");
mDbHelper.createLibrary("First Things First", "Stephen Covey", "Idris Book Bank", "Bank Rd, Rawalpindi 46000", "33.595789", "73.052405");
mDbHelper.createLibrary("Emotional Intelligence", "Daniel Goleman", "Book Centre", " Haider Rd, Rawalpindi 46000", "33.596651", "73.049094");
mDbHelper.createLibrary("Anatomy of the Spirit", "Caroline Myss", "Students Book Company", "Bank Rd, Rawalpindi 44000", "33.596524", "73.051051");
}
#Override
protected void onDestroy() {
super.onDestroy();
if (mDbHelper != null) {
mDbHelper.close();
}
}
public boolean onQueryTextChange(String newText) {
showResults(newText + "*");
return false;
}
public boolean onQueryTextSubmit(String query) {
showResults(query + "*");
return false;
}
public boolean onClose() {
showResults("");
return false;
}
private void showResults(String query) {
Cursor cursor = mDbHelper.searchBook((query != null ? query.toString() : "####"));
if (cursor == null) {
//
} else {
// Specify the columns we want to display in the result
String[] from = new String[]{
LibraryDbAdapter.KEY_BOOKNAME,
LibraryDbAdapter.KEY_BOOKAUTHOR,
LibraryDbAdapter.KEY_STORENAME,
LibraryDbAdapter.KEY_STOREADDRESS,
LibraryDbAdapter.KEY_STORELAT,
LibraryDbAdapter.KEY_STORELONG
};
// Specify the Corresponding layout elements where we want the columns to go
int[] to = new int[] { R.id.sbook,
R.id.sauthor,
R.id.sstore,
R.id.sstoreaddress,
R.id.slat,
R.id.slong};
// Create a simple cursor adapter for the definitions and apply them to the ListView
SimpleCursorAdapter books = new SimpleCursorAdapter(this,R.layout.bookresult, cursor, from, to);
mListView.setAdapter(books);
// Define the on-click listener for the list items
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) mListView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String Book = cursor.getString(cursor.getColumnIndexOrThrow("Book"));
String Author = cursor.getString(cursor.getColumnIndexOrThrow("Author"));
String Store = cursor.getString(cursor.getColumnIndexOrThrow("Store"));
String storeaddress = cursor.getString(cursor.getColumnIndexOrThrow("storeaddress"));
String lat = cursor.getString(cursor.getColumnIndexOrThrow("lat"));
String lon = cursor.getString(cursor.getColumnIndexOrThrow("lon"));
//Check if the Layout already exists
LinearLayout bookLayout = (LinearLayout)findViewById(R.id.booklayout);
if(bookLayout == null){
//Inflate the Customer Information View
LinearLayout leftLayout = (LinearLayout)findViewById(R.id.rightLayout);
View bookInfo = getLayoutInflater().inflate(R.layout.bookinfo, leftLayout, false);
leftLayout.addView(bookInfo);
}
//Get References to the TextViews
bookName = (TextView) findViewById(R.id.book);
bookAuthor = (TextView) findViewById(R.id.author);
storeName = (TextView) findViewById(R.id.store);
storeAddress = (TextView) findViewById(R.id.storeaddress);
storeLat = (TextView) findViewById(R.id.latitude);
storeLong = (TextView) findViewById(R.id.longitude);
// Update the parent class's TextView
bookName.setText(Book);
bookAuthor.setText(Author);
storeName.setText(Store);
storeAddress.setText(storeaddress);
storeLat.setText(lat);
storeLong.setText(lon);
searchView.setQuery("",true);
}
});
}
}
private void displayDate() {
final Calendar c = Calendar.getInstance();
inspectionDate.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(c.get(Calendar.MONTH) + 1).append("/")
.append(c.get(Calendar.DAY_OF_MONTH)).append("/")
.append(c.get(Calendar.YEAR)).append(" "));
}
}

Android: SQLite error in adding data

I'm working on a PiggyBank-like application wherein users can make wishlists of their wanted item and helps them save for that item. My app is still in prototype because i'm still learning android.
The problem i'm experiencing right now is every time i add a new data, it always returns false. Here's my code:
DatabaseHelper.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "people_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "price";
private static final String COL4 = "totalsavings";
private static final String COL5 = "duedate";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT, " + COL3 + "TEXT, " + COL4 +"INTEGER)";
db.execSQL(createTable);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, item);
contentValues.put(COL4, item);
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
/**
* Returns all the data from database
* #return
*/
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Returns only the ID that matches the name passed in
* #param name
* #return
*/
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
/**
* Updates the name field
* #param newName
* #param id
* #param oldName
*/
public void updateName(String newName, int id, String oldName){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET " + COL2 +
" = '" + newName + "' WHERE " + COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + oldName + "'";
Log.d(TAG, "updateName: query: " + query);
Log.d(TAG, "updateName: Setting name to " + newName);
db.execSQL(query);
}
/**
* Delete from database
* #param id
* #param name
*/
public void deleteName(int id, String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "DELETE FROM " + TABLE_NAME + " WHERE "
+ COL1 + " = '" + id + "'" +
" AND " + COL2 + " = '" + name + "'";
Log.d(TAG, "deleteName: query: " + query);
Log.d(TAG, "deleteName: Deleting " + name + " from database.");
db.execSQL(query);
}
}
MainActivity.java
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
DatabaseHelper mDatabaseHelper;
private Button btnAdd, btnViewData;
private EditText editText, editText2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for inputs
editText = (EditText) findViewById(R.id.editText);
editText2 =(EditText) findViewById(R.id.editText2);
//buttons
btnAdd = (Button) findViewById(R.id.btnAdd);
btnViewData = (Button) findViewById(R.id.btnView);
//call database
mDatabaseHelper = new DatabaseHelper(this);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newEntry = editText.getText().toString();
if (editText.length() != 0) {
AddData(newEntry);
editText.setText("");
} else {
toastMessage("You must put something in the text field!");
}
String newPriceEntry = editText2.getText().toString();
if (editText2.length() != 0) {
AddData(newPriceEntry);
editText2.setText("");
} else {
toastMessage("You must put something in the text field!");
}
}
});
btnViewData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListDataActivity.class);
startActivity(intent);
}
});
}
public void AddData(String newEntry) {
boolean insertData = mDatabaseHelper.addData(newEntry);
if (insertData) {
toastMessage("Data Successfully Inserted!");
} else {
toastMessage("Something went wrong");
}
}
/**
* customizable toast
* #param message
*/
private void toastMessage(String message){
Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}
}
ListDataActivity.java
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
/**
* Created by User on 2/28/2017.
*/
public class ListDataActivity extends AppCompatActivity {
private static final String TAG = "ListDataActivity";
DatabaseHelper mDatabaseHelper;
private ListView mListView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
mListView = (ListView) findViewById(R.id.listView);
mDatabaseHelper = new DatabaseHelper(this);
populateListView();
}
private void populateListView() {
Log.d(TAG, "populateListView: Displaying data in the ListView.");
//get the data and append to a list
Cursor data = mDatabaseHelper.getData();
ArrayList<String> listData = new ArrayList<>();
while(data.moveToNext()){
//get the value from the database in column 1
//then add it to the ArrayList
listData.add(data.getString(1));
}
//create the list adapter and set the adapter
ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listData);
mListView.setAdapter(adapter);
//set an onItemClickListener to the ListView
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
Log.d(TAG, "onItemClick: You Clicked on " + name);
Cursor data = mDatabaseHelper.getItemID(name); //get the id associated with that name
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
Log.d(TAG, "onItemClick: The ID is: " + itemID);
Intent editScreenIntent = new Intent(ListDataActivity.this, EditDataActivity.class);
editScreenIntent.putExtra("id",itemID);
editScreenIntent.putExtra("name",name);
startActivity(editScreenIntent);
}
else{
toastMessage("No ID associated with that name");
}
}
});
}
/**
* customizable toast
* #param message
*/
private void toastMessage(String message){
Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}
}
EditDataActivity.java
In this activity, the data will be displayed in an EditText (name), the savings goal (price). (I'm still working on the totalsavings).
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class EditDataActivity extends AppCompatActivity {
private static final String TAG = "EditDataActivity";
private TextView myGoal, mySavings;
private Button btnSave,btnDelete, btnDeposit;
private EditText editable_item, depositInput;
DatabaseHelper mDatabaseHelper;
private String selectedName, selectedPrice;
private int selectedID, selectedTotalSavings;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_data_layout);
myGoal = (TextView) findViewById(R.id.displayGoal);
mySavings = (TextView) findViewById(R.id.displayTotalSavings);
btnSave = (Button) findViewById(R.id.btnSave);
btnDelete = (Button) findViewById(R.id.btnDelete);
btnDeposit = (Button) findViewById(R.id.btnDeposit);
editable_item = (EditText) findViewById(R.id.editable_item);
mDatabaseHelper = new DatabaseHelper(this);
//get the intent extra from the ListDataActivity
Intent receivedIntent = getIntent();
//now get the itemID we passed as an extra
selectedID = receivedIntent.getIntExtra("id",-1); //NOTE: -1 is just the default value
//now get the name we passed as an extra
selectedName = receivedIntent.getStringExtra("name");
//now get the price we passed as an extra
selectedPrice = receivedIntent.getStringExtra("price");
//now we get the totalsavings we passed as an extra
selectedTotalSavings = receivedIntent.getIntExtra("totalsavings", -1);
//set the text to show the current selected name
editable_item.setText(selectedName);
//set the text to show the user's saving goal
myGoal.setText(selectedPrice);
//set text to show the user's total savings so far
mySavings.setText(selectedTotalSavings);
//-----------------------------------DIALOG BOX-----------------------------------------
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter Deposit");
builder.setMessage("Enter your deposit!");
depositInput= new EditText(this);
builder.setView(depositInput);
//SET POSITIVE BUTTON
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String depositTxt=depositInput.getText().toString();
selectedTotalSavings = Integer.parseInt(selectedTotalSavings + depositTxt);
mySavings.setText(selectedTotalSavings);
Toast.makeText(getApplicationContext(),depositTxt, Toast.LENGTH_LONG).show();
}
});
//SET NEGATIVE BUTTON
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
//CREATE THE DIALOG
final AlertDialog depositPrompt=builder.create();
//--------------------------------------------------------------------------------------
//buttons
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String item = editable_item.getText().toString();
if(!item.equals("")){
mDatabaseHelper.updateName(item,selectedID,selectedName);
}else{
toastMessage("You must enter a name");
}
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mDatabaseHelper.deleteName(selectedID,selectedName);
editable_item.setText("");
toastMessage("removed from database");
}
});
btnDeposit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0){
depositPrompt.show();
}
});
}
/**
* customizable toast
* #param message
*/
private void toastMessage(String message){
Toast.makeText(this,message, Toast.LENGTH_SHORT).show();
}
}
Here's the Sample Syntax
User enters details for name and price
User clicks add data
User can View Data and the data will be displayed on a ListView
User can edit and view data in the EditDataActivity
I'm not sure what the problem is.
UPDATE
so i found this code on the MainActivity Class. I'm trying to add the data from editText2 but i don't know how.
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newEntry = editText.getText().toString();
if (editText.length() != 0) {
AddData(newEntry);
editText.setText("");
} else {
toastMessage("You must put something in the text field!");
}
String newPriceEntry = editText2.getText().toString();
if (editText2.length() != 0) {
AddData(newPriceEntry);
editText2.setText("");
} else {
toastMessage("You must put something in the text field!");
}
}
});
btnViewData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListDataActivity.class);
startActivity(intent);
}
});
}
public void AddData(String newEntry) {
boolean insertData = mDatabaseHelper.addData(newEntry);
if (insertData) {
toastMessage("Data Successfully Inserted!");
} else {
toastMessage("Something went wrong");
}
}
Do i have to make a new AddData?
You need to insert the right type of values as mentioned below :
public boolean addData(String item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, item);
contentValues.put(COL4, item); // pass an integer instead of a String as you have mentioned its datatype as INTEGER or parse it using Integer.parseInt()
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
The problem I think is in the CREATE TABLE query.
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 +" TEXT, " + COL3 + "TEXT, " + COL4 +"INTEGER)";
There is no space between COL3 and "TEXT" (same for COL4 and "INTEGER"). So just add a space like so.
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 +" TEXT, " + COL3 + " TEXT, " + COL4 +" INTEGER)";
Problem is in your addData() method. COL4 holds INTEGER value.
Try this:
public boolean addData(String item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL3, item);
contentValues.put(COL4, Integer.parseInt(item));
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if date as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
The return of db.insert() is a long and you're comparing it to an int. Change
if (result == -1) {
return false;
} else {
return true;
}
To
return (int)result == -1;

How to display database fields in seperate textViews

I have created a database film app where a user can search for films, the search results then display in a scroll view. The user can then click on a film result which will lead to another activity which will display the full film details (film, actors, directors). How would I retrieve the database fields and display these in separate textViews?
MainActivity:
public void search(View v){
EditText search = (EditText)findViewById(R.id.edtSearch);
String searchresult = "%" + search.getText().toString() + "%";
db = new DbHelper(this).getReadableDatabase();
String[] tblColumns = {"*"};
String where = "film LIKE ? OR actor LIKE ? OR actor2 LIKE ? OR director LIKE ?";
String[] args = {searchresult, searchresult, searchresult, searchresult};
Cursor results = db.query("FILMTABLE", tblColumns, where, args, null, null, null);
film(results);
}
public void film (Cursor c){
c.moveToFirst();
int titleint = c.getColumnIndex("film");
int id= c.getColumnIndex("id");
String title = c.getString(titleint);
int filmID = c.getInt(id);
TextView txt = new TextView(getApplicationContext());
txt.setId(filmID);
txt.setText(title);
txt.setTextColor(Color.BLACK);
txt.setTextSize(15);
txt.setClickable(true);
txt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity4.class);
intent.putExtra("FILM_ID_KEY", String.valueOf(v.getId()));
startActivity(intent);
}
});
ScrollView scrollView = (ScrollView)findViewById(R.id.scrolLView);
scrollView.addView(txt);
}
MainActivity4:
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class MainActivity4 extends ActionBarActivity {
String filmId;
protected SQLiteDatabase db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
Intent intent = getIntent();
filmId = intent.getStringExtra(MainActivity.FILM_ID_KEY);
}
}
DbHelper:
public class DbHelper extends SQLiteOpenHelper {
private static final String ID = "id";
private static final String FILM = "film";
private static final String ACTOR = "actor";
private static final String ACTOR2 = "actor2";
private static final String DIRECTOR = "director";
private static final String DESCRIPTION = "description";
private static final String TABLE_NAME = "FILMTABLE";
private static final String DATABASE_NAME = "filmdatabase3";
private static final int DATABASE_VERSION = 1;
private static final boolean FAVOURITE = false;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FILM + " TEXT NOT NULL, " +
ACTOR + " TEXT NOT NULL, " +
ACTOR2 + " TEXT NOT NULL, " +
DIRECTOR + " TEXT NOT NULL, " +
FAVOURITE + " BOOLEAN, " +
DESCRIPTION + " TEXT NOT NULL);"
);
Cursor countRows = db.rawQuery("SELECT count(*) FROM FILMTABLE", null);
countRows.moveToFirst();
int NumRows = countRows.getInt(0);
countRows.close();
if (NumRows == 0) {
ContentValues values = new ContentValues();
values.put("film", "Wolf of Wall Street");
values.put("actor", "Leonardo Dicaprio");
values.put("actor2", "Jonah Hill");
values.put("director", "Martin Scorses");
values.put("description", "Description");
db.insert("FILMTABLE", null, values);
values.clear();
values.put("film", "Captain Philips");
values.put("actor", "Tom Hanks");
values.put("actor2", "Catherine Keener");
values.put("director", "Paul Greengrass");
values.put("description", "Description");
db.insert("FILMTABLE", null, values);
values.clear();
}
}
The answer to this question lies in the use of ListViews (https://developer.android.com/reference/android/widget/ListView.html). Basically you should create an ArrayAdapter which takes in a List of FilmObjects you retrieve from the database. In this adapter you will define how you want this data shown for each row. When you attach the adapter to the listview Android will take care of the visualization of the each row.
This tutorial will definitely help:http://developer.android.com/guide/topics/ui/layout/listview.html
I would advice to get rid of the seperate Strings that make up a movie but instead make a class that contains all these fields. This allows for much easier data handling.

Updating specific column using a spinner widget

I am trying to update current credits column of the only row in the database using a drop down spinner which gets values from an arraylist. Very unsure about how to go about doing this operation. Thank you for any help in advance.
My database code:
package com.example.parkangel;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class UDbHelper extends SQLiteOpenHelper
{
public static final String KEY_ROWID = "_id";
public static final String KEY_PFNAME = "payeeFname";
public static final String KEY_PSNAME = "payeeSname";
public static final String KEY_CARD = "card";
public static final String KEY_CREDITS = "credits";
private static final String DATABASE_NAME = "UserData.db";
private static final String DATABASE_TABLE = "UserTable";
private static final int DATABASE_VERSION = 1;
//private UDbHelper dbHelper;
//private final Context ourContext;
private static UDbHelper instance;
private SQLiteDatabase ourDatabase;
public UDbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static UDbHelper getInstance(Context context)
{
if (instance == null)
{
instance = new UDbHelper(context);
}
return instance;
}
#Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_PFNAME + " TEXT NOT NULL, " + KEY_PSNAME + "
TEXT NOT NULL, " +
KEY_CARD + " TEXT NOT NULL, " + KEY_CREDITS + " TEXT
NOT NULL);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public synchronized UDbHelper open() throws SQLException
{
System.out.println ("running open");
if(ourDatabase == null || !ourDatabase.isOpen())
ourDatabase = getWritableDatabase();
return this;
}
public String getData()
{
// TODO Auto-generated method stub
String[] columns = new String[] {KEY_ROWID, KEY_PFNAME, KEY_PSNAME,
KEY_CARD, KEY_CREDITS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null,
null, null, null);
String result = " ";
int iRow = c.getColumnIndexOrThrow(KEY_ROWID);
int iPFname = c.getColumnIndexOrThrow(KEY_PFNAME);
int iPSname = c.getColumnIndexOrThrow(KEY_PSNAME);
int iCard = c.getColumnIndexOrThrow(KEY_CARD);
int iCredits = c.getColumnIndexOrThrow(KEY_CREDITS);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " +
c.getString(iPFname) + " " +
c.getString(iPSname)
+ " " + c.getString(iCard) + " " +
c.getString(iCredits) + "\n";
}
return result;
}
}
My main activity code I will be doing the operation through:
package com.example.parkangel;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class Balance extends Activity{
Button add;
TextView display;
Spinner spinner3;
String[] money = {"Select amount", "£1", "£2", "£5", "£10"};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.balance_layout);
TextView tv = (TextView) findViewById(R.id.firstn);
UDbHelper db = new UDbHelper(this);
db.open();
String data = db.getData();
db.close();
tv.setText(data);
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(Balance.this,
android.R.layout.simple_spinner_item, money);
spinner3 = (Spinner) findViewById (R.id.moneytoadd);
spinner3.setAdapter(adapter3);
add = (Button) findViewById(R.id.topup);
}
public void onClick(View arg0)
{
}
public void updateActivity(View view){
Intent book = new Intent(Balance.this, BookTicket.class);
startActivity(book);
}
public void addBalance(View view){
Intent addB = new Intent(Balance.this, Balance.class);
startActivity(addB);
}
public void doUpdate(View view){
Intent upd = new Intent(Balance.this, UpdateTicket.class);
startActivity(upd);
}
}
Your question is fairly compound, getting selected field from spinner, updating database... I'm not going to provide a complete answer, but this should get you started:
This is how you would get the selected field from the spinner, which you can then use to update your database. One word of warning, I believe setOnItemSelectedListener is called when it is initially set, so you may need to ignore the first call.
Spinner spinner;
spinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View view, int arg2,
long arg3) {
if(! (view instanceof TextView)){
// view is probably a textview, but record type if not.
System.out.println("incorrect view type " + view.getClass().getSimpleName());
return;
}
EditText et = (EditText) view;
String fieldName = et.getText().toString().trim();
//Now we got selected name, send name
//to a function that updates our database.
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I didn't find the problem but I can tell you that your code is VERY INEFFICIENT. You are using String to build your result in the getData method. Each time you try to append the new data (Row) to the old one you are creating a new string to hold the new data. If you are retrieving 1000 row from the database, this means that you are creating 1000 string to build the final one. I recommend using StringBuilder instead as following:
StringBuilder strBuilder= new StringBuilder();
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
strBuilder.append( c.getString(iRow) + " " +
c.getString(iPFname) + " " +
c.getString(iPSname)
+ " " + c.getString(iCard) + " " +
c.getString(iCredits) + "\n");
}
return str= strBuilder.toString();
Here is an example of how to insert into your DB:
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PFNAME , editText1.getText().toString());
values.put(KEY_PSNAME , stringArrayList.get(position));
values.put(KEY_CARD , "12345677");
values.put(KEY_CREDITS , "55");
// Inserting Row
db.insert(DATABASE_TABLE, null, values);
db.close(); // Closing database connection

Categories