• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps TodoList application help

I need some help, I tried to make my own todo list but I get some problem,
this is the source code:
Code:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {
	
	//The name of the database.
	public static final String DATABASE_NAME = "tobuydatabase";
	//The version of the database.
	public static final int DATABASE_VERSION = 1;
	
	// Database creation sql statement
	private static final String DATABASE_CREATE = "create table tobuy (_id integer primary key autoincrement, "
					+ "title text not null, url text not null, price text not null);";
	
	/**
	 * Constructor of this class.
	 */
	public DbHelper(Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	//This method is called during the creation of the database
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);
	}

	//This method is called during an upgrade of the database e.g. if you increase
	// the database version
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS tobuy");
		onCreate(db);
	}

}

Code:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DbAdapter {
	
	/**
	 * Database fields
	 */
	public static final String KEY_ROWID = "_id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_URL = "url";
	public static final String KEY_PRICE = "price";
	
	/**
	 * Name of the table
	 */
	private static final String DATABASE_TABLE = "tobuy";
	
	private SQLiteDatabase database;
	private DbHelper dbHelper;
	private Context context;
	
	/**
	 * Constructor of this class.
	 * @param context
	 */
	public DbAdapter(Context context){
		this.context = context;
	}
	
	/**
	 * This method get a connection to the database to query it.
	 * @return the same DbAdatper but with a connection to the database.
	 * @throws SQLException
	 */
	public DbAdapter open() throws SQLException{
		dbHelper = new DbHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}
	
	/**
	 * This method close the database.
	 */
	public void close(){
		dbHelper.close();
	}
	
	/**
	 * Create a new tobuy if the tobuy is successfully created return the new
	 * rowId for that note, otherwise return a -1 to indicate failure.
	 */
	public long createTobuy(String title,String url, String price){
		ContentValues initialValues = createContentValues(title,url,price);
		return database.insert(DATABASE_TABLE, null, initialValues);
	}
	
	/**
	 * Update the tobuy.	
	 */
	public boolean updateTobuy(long rowId, String title, String url,
			String price){
		ContentValues updateValues = createContentValues(title,url,price);
		return database.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId, null) > 0;
	}
	
	/**
	 * Delets tobuy
	 */
	public boolean deleteTobuy(long rowId){
		return database.delete(DATABASE_TABLE,KEY_ROWID + "=" + rowId,null) > 0;
	}
		
	/**
	 * Return a Cursor over the list of all tobuy in the database
	 * 
	 * @return Cursor over all notes
	 */
	public Cursor fetchAllTobuy(){
		return database.query(DATABASE_TABLE, new String[]{KEY_ROWID,KEY_TITLE,KEY_URL,KEY_PRICE}, null, null, null, null, null);
	}
	
	/**
	 * Return a Cursor positioned at the defined tobuy
	 */
	public Cursor fetchToBuy(long rowId) throws SQLException{
		Cursor mCursor = database.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,KEY_TITLE,KEY_URL,KEY_PRICE}, 
				KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (mCursor != null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	/**
	 * This method return a contentvalues with all the 3 information
	 * that we need to insert in the database.
	 * 
	 * @param title title of the toBuy.
	 * @param url url of the toBuy (If It's an online object).
	 * @param price price of the toBuy.
	 * @return ContentValues with all the three parameters of the method.
	 */
	private ContentValues createContentValues(String title, String url,
			String price) {
		ContentValues values = new ContentValues();
		values.put(KEY_TITLE,title);
		values.put(KEY_URL,url);
		values.put(KEY_PRICE,price);
		return values;
	}

Code:
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.SimpleCursorAdapter;



public class ToBuyListActivity extends ListActivity{
	
	private DbAdapter dbAdapter;
	private Cursor cursor;
	private static final int DELETE_ID = Menu.FIRST + 1;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tobuy_list);
		
		//this.getListView().setDividerHeight(2);
		dbAdapter = new DbAdapter(this);
		dbAdapter.open();
	    fillData();
	}

	private void fillData(){
		
		cursor = dbAdapter.fetchAllTobuy();
		startManagingCursor(cursor);
		
		String[] from = new String[] {DbAdapter.KEY_TITLE};
		int[] to = new int[] {R.id.titleTextView};
		
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,R.layout.tobuy_row,cursor,from,to);
		
		setListAdapter(notes);
		
	}
	
	@Override
 	protected void onDestroy() {
 		super.onDestroy();
 		if (dbAdapter != null) {
 			dbAdapter.close();
 		}
 	}
    
	
}

The problem that I get is that when the application crash, probably because there are some problem in the database creation or in the fetchAllToBuy() method. I don't know so please help me if you can! :)
 
Upvote 0
I would recomend using the internal storage rather than creating a sql database,

if i'm right, youre simply just saving todos? if so it isnt much to handle, therefore you wouldnt really need to go through all the sql effort.

so for example, you could have a collection of todo's,
save that collection to the internal storage as a file.

when running the app, you simply read the file,
and display all the todo's within the collection.

check out "Using the Internal Storage" within this link
Data Storage | Android Developers

to clarify you could try a concept where (note build upon this as you will),

when creating a new todo, you save it as a string into a collection, maybe an array list or something.
save that array list to a file and read it vice versa when needing to access them.

there are many ways, just think which is best for you

Valent.
 
Upvote 0
I would recomend using the internal storage rather than creating a sql database,

if i'm right, youre simply just saving todos? if so it isnt much to handle, therefore you wouldnt really need to go through all the sql effort.

so for example, you could have a collection of todo's,
save that collection to the internal storage as a file.

when running the app, you simply read the file,
and display all the todo's within the collection.

check out "Using the Internal Storage" within this link
Data Storage | Android Developers

to clarify you could try a concept where (note build upon this as you will),

when creating a new todo, you save it as a string into a collection, maybe an array list or something.
save that array list to a file and read it vice versa when needing to access them.

there are many ways, just think which is best for you

Valent.

I will try but the problem is that I also need to get only the price fields of each todos is it possible with the method that you said?

Thanks
 
Upvote 0
Yes, if youve saved the information, you can read and extract the parts you want.

Ok, thanks. Following your advice I just have to create a String array and put each ToDos (Title + Price + Url) in each string that then go in the array. Then When I read them I need to cut the string to get the price right?
Or do you think is better to save 3 string for each ToDos?

Thanks.
 
Upvote 0
Ok, thanks. Following your advice I just have to create a String array and put each ToDos (Title + Price + Url) in each string that then go in the array. Then When I read them I need to cut the string to get the price right?
Or do you think is better to save 3 string for each ToDos?

Thanks.


It's entirely upto you,

you can use your way, or you could check out doing it by using an array of strings.

i.e.

ArrayList<ArrayList<String>> toDos = new ArrayList<ArrayList<String>>();

And for every todo just create an array list of string, add the title string, price string and url string to that array list, then add that array list to the todo's.

to get the information should be basically the same but getting rather than adding.



I mean this is entirely upto you and how it fits your code.

Good luck
 
Upvote 0
It's entirely upto you,

you can use your way, or you could check out doing it by using an array of strings.

i.e.

ArrayList<ArrayList<String>> toDos = new ArrayList<ArrayList<String>>();

And for every todo just create an array list of string, add the title string, price string and url string to that array list, then add that array list to the todo's.

to get the information should be basically the same but getting rather than adding.



I mean this is entirely upto you and how it fits your code.

Good luck

Ok, your way is the best, If I understood I just create the ToDos array list then I create for each ToDo another ArrayList and I put this second array list in the first array list. Am I right? :)

Thanks!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones