0
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() 
        {

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
    year = selectedYear;
    month = selectedMonth;
    day = selectedDay;              
    try
     {
db=openOrCreateDatabase("Lecturer",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.execSQL("Create Table If Not Exists temp (Month INTEGER,Date INTEGER,Year INTEGER,Event TEXT)");
db.execSQL("INSERT INTO temp(Month, Date, Year, Event) VALUES (11,17,2012,'first')");
db.execSQL("INSERT INTO temp(Month, Date, Year, Event) VALUES (10,9,2012,'second')");
Cursor c=db.rawQuery("SELECT * FROM temp",null);
c.moveToFirst();
while(c.isAfterLast() == false)
    {
     t2=c.getString(0);
    // Log.e("",""+t2);
     int n = Integer.parseInt(t2);
     t3=c.getString(1);
     int n1 = Integer.parseInt(t3);
     t4=c.getString(2);
     int n2 = Integer.parseInt(t4);
     t5=c.getString(3);
     EditText t=(EditText)findViewById(R.id.editText1);
     Log.e("",""+day);
     if(day==n&& month==n1&&year==n2)
        {
            Log.e("",""+t5);
            t.setText(t5);
        }
    else
        {
            t.setText("No Event");
        }       
            c.moveToNext();
        }
            c.close();

                }
                catch(SQLException e)
                {

                }
                // set selected date into textview
                tvDisplayDate.setText(new StringBuilder().append(month + 1)
                        .append("-").append(day).append("-").append(year)
                        .append(" "));

                // set selected date into datepicker also
                dpResult.init(year, month, day, null);

            }
        };


}

I am facing some problem and force close while storing and fetchind events in DB.please help me with any helpfull suggestions...here i am trying to compare date selected by datepicker with database stored date and displaying the event on that day.

1
  • 1
    you shouldn't be doing this in a callback. callbacks happen on the main thread and DB/network operations should always be done in the background. personally, i think it's best to create you tables in the onCreate method of you sqlite helper class. Commented Dec 20, 2012 at 17:38

2 Answers 2

1

Starting from ICS you are crashing if you do any DB operation on main thread. As you are just trying to write a value in DB you could do like this:

new Thread(new Runnable() {
    public void run() {
        do_the_db_operations
    }
 }.start();
Sign up to request clarification or add additional context in comments.

7 Comments

Why ? The only issue with this can be in terms of synchronisation. If the app is DB intensive and these kind of problems can arrise a queuing mechanism will be needed.
just spawning a thread doesn't allow any kind of easy return. you can call runOnUiThread at the end of your runnable, but it's easier (and IMHO better) to use an AsyncTask instead of a simple thread.
That's true if you need to return on main thread. In the case described by the OP it's not.
he's updating the ui based on a DB read. so yes, he is returning to the main thread.
can anyone please suggest me an easy way to solve this problem?i am very much in need of it.thanks
|
0

DB accesses can be run on the UI thread. An Activity can call finish() on itself. But any Android engineer who saw you doing this would shake his or her head sadly.

In other words, do DB access on the background.

What's missing from this whole discussion is the logCat for the force close. What's going wrong? We're only speculating here, and without a logCat, the speculation is purely while (true) { int i = 0; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.