Working with the SQLite-Database - Cursors

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby Katharnavas » Wed Feb 20, 2008 10:45 am

Sportsdude11751 wrote:The source code I used was plusminus's source. I will copy and paste it next time I can get on that computer.
Even if it can't find the database, in +-'s source he had a create database statement.


For you i tried the same database code with M5 version and it worked perfectly for me. Did you checked from the command line.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Top

Postby königdmx » Wed Feb 20, 2008 1:41 pm

hi,

for the part of output i received an error on compilation:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. this.setListAdapter(new ArrayAdapter<String>(this,
  3.  
  4.                         android.R.layout.simple_list_item_1, results));
  5.  
  6.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

eclipse meaning "setListAdapter" = "The method setListAdapter(ArrayAdapter<String>) is undefined for the type createDB"

What can i do instead of this?
königdmx
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Feb 19, 2008 11:09 am

Postby Katharnavas » Wed Feb 20, 2008 1:54 pm

königdmx wrote:hi,

for the part of output i received an error on compilation:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.setListAdapter(new ArrayAdapter<String>(this,
  2.                         android.R.layout.simple_list_item_1, results));
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

eclipse meaning "setListAdapter" = "The method setListAdapter(ArrayAdapter<String>) is undefined for the type createDB"

What can i do instead of this?


What i think is some header files are being missed. use Shift+Ctrl O and it will solve the problem.
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Postby königdmx » Wed Feb 20, 2008 2:48 pm

@Katharnavas: thx for reply, but it didn´t help me. The error is the same..

these are the imports:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.io.FileNotFoundException;
  3.  
  4. import java.util.ArrayList;
  5.  
  6.  
  7.  
  8. import android.app.Activity;
  9.  
  10. import android.database.Cursor;
  11.  
  12. import android.database.sqlite.SQLiteDatabase;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.widget.ArrayAdapter;
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


The problem: when i enter
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
the code completion do not offer me
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setListAdapter
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
königdmx
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Feb 19, 2008 11:09 am

Postby plusminus » Wed Feb 20, 2008 3:49 pm

Hello königdmx,

is your class extending ListActivity :?:

Looks like you are extending just Activity.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby königdmx » Wed Feb 20, 2008 3:56 pm

yepp, thats it!
thx!!
königdmx
Junior Developer
Junior Developer
 
Posts: 13
Joined: Tue Feb 19, 2008 11:09 am

Top

Postby cabernet1976 » Wed Feb 27, 2008 3:10 pm

Hi all,

The new method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public Cursor  rawQuery(String sql, String[] selectionArgs)
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
seems able to replace the previous method query, but Eclipse cannot distinguish rawQuery and says it is undifined.
I don't know why.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby res » Wed Mar 05, 2008 5:22 pm

cabernet1976 wrote:Hi all,

The new method
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public Cursor  rawQuery(String sql, String[] selectionArgs)
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
seems able to replace the previous method query, but Eclipse cannot distinguish rawQuery and says it is undifined.
I don't know why.


Make sure you have updated Eclipse to point to the new m5 sdk directory.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Select statement

Postby MobileBen » Thu Mar 20, 2008 4:16 am

Hello everyone,

I read the post today (Its the first time I am here in this forum) and it helped me very much. Thx

But I have a question to the rawQuery.
How can I make a "Select statement" like it was done befor (the example from plusminus) with this query?
Can anybody give me a code example, because Android always crys, doesn't matter what I d :(

Thx guys
MobileBen
Freshman
Freshman
 
Posts: 4
Joined: Thu Mar 20, 2008 4:10 am

startManagingCursor(cursor)

Postby mill » Sat Mar 29, 2008 5:56 pm

Hi plusmimus, I see you did not use startManagingCursor(cursor) in your code, will it cause any issue?

How do I call startManagingCursor(cursor) in a IntentReceiver or in a Service ?

Thanks
mill
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Mar 29, 2008 5:50 pm
Location: Malaysia

Re: startManagingCursor(cursor)

Postby plusminus » Sat Mar 29, 2008 7:31 pm

Hello mill,

mill wrote:Hi plusmimus, I see you did not use startManagingCursor(cursor) in your code, will it cause any issue?

How do I call startManagingCursor(cursor) in a IntentReceiver or in a Service ?


This method allows the activity to take care of managing the given Cursor's lifecycle for you based on the activity's lifecycle. That is, when the activity is stopped it will automatically call deactivate() on the given Cursor, and when it is later restarted it will call requery() for you.

:arrow: It is not needed, but would be nicer, yes.

I'm not sure, but IntentReceivers last only 'one call' and Services last 'forever' without being really stopped...

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby NithinG » Wed Apr 16, 2008 3:11 pm

Hi plus minus,

I faced a problem while creating database. The problem is.

I am creating a database in a class which extends activity, and that is defined in a function.
This function i have to call from another class which doesnt extent activity( normal class ).
So i am not able to give context value. So i am not able to create constructor(Context ctx) also.

So while running its giving and Exception :- java.lang.RuntimeException: Not supported in system context

if this is the case what can i do?

Can you pease help me to solve this problem.

Thanks and regards,

NithinG
NithinG
Developer
Developer
 
Posts: 36
Joined: Wed Feb 27, 2008 6:24 am

Display DB

Postby lmsa » Mon Apr 21, 2008 4:50 pm

Hy PlusMinus,

I'm working on Android's DB and I've a problem for the output of my DB.
I don't know how to display it.

I give you my source code I hope you can help me.



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. import java.io.FileNotFoundException;
  5.  
  6. import java.util.ArrayList;
  7.  
  8. import java.util.List;
  9.  
  10.  
  11.  
  12. import android.content.ContentValues;
  13.  
  14. import android.content.Context;
  15.  
  16. import android.database.Cursor;
  17.  
  18. import android.database.SQLException;
  19.  
  20. import android.database.sqlite.SQLiteDatabase;
  21.  
  22. import android.util.Log;
  23.  
  24.  
  25.  
  26.        
  27.  
  28.         class Contact extends Object {
  29.  
  30.             public long contactId;
  31.  
  32.             public String phone;
  33.  
  34.             public String name;
  35.  
  36.         }
  37.  
  38.      
  39.  
  40.         public class DroidDB {
  41.  
  42.  
  43.  
  44.           private static final String CREATE_TABLE_CONTACTS = "create table contacts (contact_id integer primary key autoincrement, "
  45.  
  46.                  
  47.  
  48.                           + "phone text not null, name text not null);";
  49.  
  50.        
  51.  
  52.  
  53.  
  54.           private static final String CONTACTS_TABLE = "contacts";
  55.  
  56.           private static final String DATABASE_NAME = "newdroid";
  57.  
  58.           private static final int DATABASE_VERSION = 1;
  59.  
  60.           private SQLiteDatabase db;
  61.  
  62.  
  63.  
  64.           public DroidDB(Context ctx) {
  65.  
  66.               try {
  67.  
  68.                   db = ctx.openDatabase(DATABASE_NAME, null);
  69.  
  70.               } catch (FileNotFoundException e) {
  71.  
  72.                   try {
  73.  
  74.                       db = ctx.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0,
  75.  
  76.                               null);
  77.  
  78.                       db.execSQL(CREATE_TABLE_CONTACTS);
  79.  
  80.                   } catch (FileNotFoundException e1) {
  81.  
  82.                       db = null;
  83.  
  84.                   }
  85.  
  86.               }
  87.  
  88.           }
  89.  
  90.      
  91.  
  92.       public boolean insertContact(String phone, String name) {
  93.  
  94.           ContentValues values = new ContentValues();
  95.  
  96.           values.put("phone", phone);
  97.  
  98.           values.put("name", name.toString());
  99.  
  100.           return (db.insert(CONTACTS_TABLE, null, values) > 0);
  101.  
  102.       }
  103.  
  104.       public boolean deleteContact(Long contactId) {
  105.  
  106.           return (db.delete(CONTACTS_TABLE, "contact_id=" + contactId.toString(), null) > 0);
  107.  
  108.       }
  109.  
  110.            
  111.  
  112.      
  113.  
  114.       public List<Contact> getContacts() {
  115.  
  116.           ArrayList<Contact> contacts = new ArrayList<Contact>();
  117.  
  118.           try {
  119.  
  120.               Cursor c = db.query(CONTACTS_TABLE, new String[] { "contact_id", "phone",
  121.  
  122.                       "name" }, null, null, null, null, null);
  123.  
  124.        
  125.  
  126.               int numRows = c.count();
  127.  
  128.               c.first();
  129.  
  130.               for (int i = 0; i < numRows; ++i) {
  131.  
  132.                   Contact contact = new Contact();
  133.  
  134.                   contact.contactId = c.getLong(0);
  135.  
  136.                   contact.phone = c.getString(1);
  137.  
  138.                   contact.name = new String(c.getString(2));
  139.  
  140.                   contacts.add(contact);
  141.  
  142.                   c.next();
  143.  
  144.               }
  145.  
  146.           } catch (SQLException e) {
  147.  
  148.               Log.e("NewsDroid", e.toString());
  149.  
  150.           }
  151.  
  152.           return contacts;
  153.  
  154.       }
  155.  
  156.       }
  157.  
  158.        
Parsed in 0.049 seconds, using GeSHi 1.0.8.4



Cordialy,

lmsa
lmsa
Freshman
Freshman
 
Posts: 3
Joined: Mon Apr 21, 2008 4:42 pm

Postby bjreddi » Mon Apr 28, 2008 11:51 am

Hi,

Instead of using

myDb.query()

use
myDb.rawQuery();
bjreddi
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 03, 2008 1:24 pm

Postby E.Jyothsna » Tue Apr 29, 2008 2:10 pm

in my application i am having spinner.I am setting the values and i stored in database and again i am retreving the values from database.upto this ok.how to set the retreving values to spinner?for example from spinner i selected 25MInutes and i came from that application to another application.Again if i go to that previous application and if i see that spinner it should be my selected item that means 25Minutes.But it is showing default values.How can i solve this problem?I want immediate response.Plz soon give me reply.

This is my code:i created database and my table in another file but same application

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Preferences extends Activity {
  2.  
  3.  
  4.  
  5.     private String res;
  6.  
  7.     private long timeid;
  8.  
  9.     private SQLiteDatabase myDatabase;
  10.  
  11.     private String serviceInterval;
  12.  
  13.     private String getservicerequestinterval;
  14.  
  15.     private String TimeInterval;
  16.  
  17.     private String getmStuffView;
  18.  
  19.     private String MStuffView;
  20.  
  21.     private String time;
  22.  
  23.     private String views;
  24.  
  25.     private String viewname1;
  26.  
  27.     private String servicename1;
  28.  
  29.     private String timename1;
  30.  
  31.     final ArrayList<CharSequence> results = new ArrayList<CharSequence>();
  32.  
  33.     RadioButton mStuffView;
  34.  
  35.     RadioButton mStufftext;
  36.  
  37.     RadioButton servicerequestinterval;
  38.  
  39.     RadioButton servicerequestmanual;
  40.  
  41.     Spinner timeinterval;
  42.  
  43.  
  44.  
  45.     @Override
  46.  
  47.     public void onCreate(Bundle icicle) {
  48.  
  49.         super.onCreate(icicle);
  50.  
  51.         setContentView(R.layout.preferences);
  52.  
  53.  
  54.  
  55.         try {
  56.  
  57.             myDatabase = this.openDatabase("Mobeegal", null);
  58.  
  59.             String[] col = {"views", "service", "time"};
  60.  
  61.             Cursor c = myDatabase.query(true, "Preferences", col, null, null, null, null, null);
  62.  
  63.             int viewname = c.getColumnIndex("views");
  64.  
  65.             int servicename = c.getColumnIndex("service");
  66.  
  67.             int timename = c.getColumnIndex("time");
  68.  
  69.             if (c != null) {
  70.  
  71.                 if (c.first()) {
  72.  
  73.                     do {
  74.  
  75.                         viewname1 = c.getString(viewname);
  76.  
  77.                         servicename1 = c.getString(servicename);
  78.  
  79.                         timename1 = c.getString(timename);
  80.  
  81.                         results.add(viewname1);
  82.  
  83.                         results.add(servicename1);
  84.  
  85.                         results.add(timename1);
  86.  
  87.                     } while (c.next());
  88.  
  89.                 }
  90.  
  91.             }
  92.  
  93.             res = results.toString();
  94.  
  95.             Toast.makeText(Preferences.this, timename1, Toast.LENGTH_SHORT).show();
  96.  
  97.         } catch (FileNotFoundException e) {
  98.  
  99.         }
  100.  
  101.  
  102.  
  103.         TabHost tabs = (TabHost) this.findViewById(R.id.tabs);
  104.  
  105.         tabs.setup();
  106.  
  107.  
  108.  
  109.         TabHost.TabSpec one = tabs.newTabSpec("one");
  110.  
  111.         one.setContent(R.id.mStuffView);
  112.  
  113.         one.setIndicator("mStuffView");
  114.  
  115.         tabs.addTab(one);
  116.  
  117.         mStuffView = (RadioButton) findViewById(R.id.mapviewradiobutton);
  118.  
  119.         mStufftext = (RadioButton) findViewById(R.id.textviewradiobutton);
  120.  
  121.  
  122.  
  123.         if (viewname1.toString().equals("MapView")) {
  124.  
  125.             mStuffView.setChecked(true);
  126.  
  127.             mStufftext.setChecked(false);
  128.  
  129.         } else {
  130.  
  131.             mStufftext.setChecked(true);
  132.  
  133.             mStuffView.setChecked(false);
  134.  
  135.         }
  136.  
  137.  
  138.  
  139.         TabHost.TabSpec two = tabs.newTabSpec("two");
  140.  
  141.         two.setContent(R.id.servicerequestinterval);
  142.  
  143.         two.setIndicator("Service request interval");
  144.  
  145.         tabs.addTab(two);
  146.  
  147.  
  148.  
  149.         timeinterval = (Spinner) findViewById(R.id.timeinterval);
  150.  
  151.         timeinterval.setVisibility(View.INVISIBLE);
  152.  
  153.         servicerequestinterval = (RadioButton) findViewById(R.id.autoradiobutton);
  154.  
  155.         servicerequestmanual = (RadioButton) findViewById(R.id.manualradiobutton);
  156.  
  157.         // servicerequestinterval.setChecked(true);
  158.  
  159.         if (servicename1.toString().equals("Auto")) {
  160.  
  161.             servicerequestinterval.setChecked(true);
  162.  
  163.             servicerequestmanual.setChecked(false);
  164.  
  165.             timeinterval = (Spinner) findViewById(R.id.timeinterval);
  166.  
  167.             timeinterval.setVisibility(View.INVISIBLE);
  168.  
  169.         } else {
  170.  
  171.             servicerequestmanual.setChecked(true);
  172.  
  173.             servicerequestinterval.setChecked(false);
  174.  
  175.             timeinterval = (Spinner) findViewById(R.id.timeinterval);
  176.  
  177.             timeinterval.setVisibility(View.VISIBLE);
  178.  
  179.             if (TimeInterval != timename1) {
  180.  
  181.                
  182.  
  183.              //   // Toast.makeText(Preferences.this, TimeInterval, Toast.LENGTH_SHORT).show();
  184.  
  185.                 timeinterval.setDrawSelectorOnTop(true);
  186.  
  187.             timeinterval.setVisibility(1);                
  188.  
  189.             } else {
  190.  
  191.                 timeinterval.setVisibility(View.VISIBLE);
  192.  
  193.             //  Toast.makeText(Preferences.this, TimeInterval, Toast.LENGTH_SHORT).show();
  194.  
  195.             }
  196.  
  197.  
  198.  
  199.         }
  200.  
  201.  
  202.  
  203.         ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
  204.  
  205.                 this, R.array.timeperiod, android.R.layout.simple_spinner_item);
  206.  
  207.         timeinterval.setAdapter(adapter1);
  208.  
  209.         timeinterval.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  210.  
  211.  
  212.  
  213.             public void onItemSelected(AdapterView parent, View v,
  214.  
  215.                     int position, long id) {
  216.  
  217.                 TimeInterval = (String) timeinterval.getSelectedItem();
  218.  
  219.             // timeid = timeinterval.getSelectedItemId();
  220.  
  221.             }
  222.  
  223.  
  224.  
  225.             public void onNothingSelected(AdapterView parent) {
  226.  
  227.             }
  228.  
  229.         });
  230.  
  231.  
  232.  
  233.         final RadioButton manualRadiobutton = (RadioButton) findViewById(R.id.manualradiobutton);
  234.  
  235.         if (manualRadiobutton.isEnabled()) {
  236.  
  237.             manualRadiobutton.setOnClickListener(new RadioButton.OnClickListener() {
  238.  
  239.  
  240.  
  241.                 public void onClick(View arg0) {
  242.  
  243.                     timeinterval.setVisibility(View.VISIBLE);
  244.  
  245.                 }
  246.  
  247.             });
  248.  
  249.         }
  250.  
  251.  
  252.  
  253.         final RadioButton autoRadiobutton = (RadioButton) findViewById(R.id.autoradiobutton);
  254.  
  255.         if (autoRadiobutton.isEnabled()) {
  256.  
  257.             autoRadiobutton.setOnClickListener(new RadioButton.OnClickListener() {
  258.  
  259.  
  260.  
  261.                 public void onClick(View arg0) {
  262.  
  263.                     timeinterval.setVisibility(View.INVISIBLE);
  264.  
  265.                 }
  266.  
  267.             });
  268.  
  269.         }
  270.  
  271.         tabs.setCurrentTab(0);
  272.  
  273.  
  274.  
  275.  
  276.  
  277.         final Button savebutton = (Button) findViewById(R.id.save);
  278.  
  279.         savebutton.setOnClickListener(new Button.OnClickListener() {
  280.  
  281.  
  282.  
  283.             public void onClick(View v) {
  284.  
  285.                 mStuffView = (RadioButton) findViewById(R.id.mapviewradiobutton);
  286.  
  287.                 if (mStuffView.isChecked()) {
  288.  
  289.                     getmStuffView = "MapView";
  290.  
  291.                 } else {
  292.  
  293.                     getmStuffView = "TextView";
  294.  
  295.                 }
  296.  
  297.                 servicerequestinterval = (RadioButton) findViewById(R.id.autoradiobutton);
  298.  
  299.                 if (servicerequestinterval.isChecked()) {
  300.  
  301.                     getservicerequestinterval = "Auto";
  302.  
  303.                 } else {
  304.  
  305.                     getservicerequestinterval = "Manual";
  306.  
  307.                 }
  308.  
  309.                 myDatabase.execSQL("UPDATE Preferences set views='" + getmStuffView + "',service='" + getservicerequestinterval + "',time='" + TimeInterval + "' where preference=preference");
  310.  
  311.                 Toast.makeText(Preferences.this, "Change Setting Activated", Toast.LENGTH_SHORT).show();
  312.  
  313.             }
  314.  
  315.         });
  316.  
  317.  
  318.  
  319.     }
  320.  
  321.     //  MenuView
  322.  
  323.     @Override
  324.  
  325.     public boolean onCreateOptionsMenu(Menu menu) {
  326.  
  327.         ViewMenu viewmenu = new ViewMenu();
  328.  
  329.         viewmenu.onCreateOptionsMenu(menu);
  330.  
  331.         return true;
  332.  
  333.     }
  334.  
  335. //  Menu Item
  336.  
  337.     @Override
  338.  
  339.     public boolean onOptionsItemSelected(Item item) {
  340.  
  341.  
  342.  
  343.         switch (item.getId()) {
  344.  
  345. //              mStuff Menu
  346.  
  347.             case 1:
  348.  
  349.                 Intent stuffCheckintent = new Intent(Preferences.this, MstuffMapView.class);
  350.  
  351.                 startActivity(stuffCheckintent);
  352.  
  353.                 break;
  354.  
  355.             case 2:
  356.  
  357.                 Intent intent1 = new Intent(Preferences.this, FindandInstall.class);
  358.  
  359.                 startActivity(intent1);
  360.  
  361.                 break;
  362.  
  363.             case 3:
  364.  
  365.                 Intent settings = new Intent(Preferences.this, Settings.class);
  366.  
  367.                 startActivity(settings);
  368.  
  369.                 break;
  370.  
  371.         }
  372.  
  373.         return super.onOptionsItemSelected(item);
  374.  
  375.     }
  376.  
  377. }
Parsed in 0.069 seconds, using GeSHi 1.0.8.4
E.Jyothsna
Junior Developer
Junior Developer
 
Posts: 22
Joined: Thu Feb 28, 2008 12:25 pm
Location: chennai

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests