开发者

I have problems when using sqlite inside AsyncTask in android?

I face two main problems when using a sqlite command inside an AsncTask in android.

  1. When I execute a select command on the first try I get no results but on the second try (loading a activity that has this Asynctask) I do get results.

  2. Sometimes I get an error that the database is not closed despite that it is already closed/

What is the problem with this?

UPDATE: This is the code that retrive data from database (db.getAllMessage)

private ArrayList<FriendMessagesResulted> getMessagesFromCach(Context c){

     FriendMessagesResulted messagesResulted1 = new FriendMessagesResulted();
     DBAdapter db = new DBAdapter(c);
     Cursor c1;
     db.open();

     c1 = db.getAllMessage(Settings.getCurrentUserId(c),Integer.parseInt(fId));
     Log.d("***Database count",c1.getCount()+" from: "+Settings.getCurrentUserId(c)+"  to:"+Integer.parseInt(fId));
     c1.moveToFirst();
     if(c1.getCount()>0)
            status=true;

     if (messagesResultedList == null) {
            messagesResultedList = new ArrayList<FriendMessagesResulted>();
    }
        else
            messagesResultedList.clear();

        while (c1.isAfterLast() == false) {
            if(Integer.parseInt(c1.getString(0))>maxId)
                maxId=Integer.parseInt(c1.getString(0));

            messagesResulted1.set_mId(Integer.parseInt(c1.getString(0)));
            messagesResulted1.set_msgTxt("MD:"+c1.getString(3));
            messagesResulted1.set_MessageTime(c1.getString(4));
            messagesResulted1.set_dir(c1.getString(5));
            messagesResultedList.add(messagesResulted1);
            c1.moveToNext();
        }

     db.close();
     c1.close();    
     return messagesResultedList;
}

and this the code for AsyncTask, where I call get getMessagesFromCach method

private void getMessages(final Context c)
{
     handler = new Handler();
        r=new Runnable() {
            public void run() {
                 class RecentMessageLoader extends AsyncTask<Void, Void, ArrayList<FriendMessagesResulted>>{
                     ArrayList<FriendMessagesResulted> messagesResultedList=null;


                     @Override
                     protected ArrayList<FriendMessagesResulted> doInBackground(Void... params) {

                         if(!finishLoadingPastMessages)
                            {
                             messagesResultedList=getMessagesFromCach(c);

                             if(!status){
                                Log.d("Where are you","I'm in JSON");
                    开发者_StackOverflow            messagesResultedList=getMessagesFromJSON(c);
                             }
                            }
                         else{
                            Log.d("Where are you","I'm in Recent messages");
                            messagesResultedList=getRecentMessages(c,Settings.getCurrentUserId(c),Integer.parseInt(fId));
                         }
                          return messagesResultedList;
                     }
                     protected void onPostExecute( ArrayList<FriendMessagesResulted> FMRList ) {

                         // to disappear loading message
                         d.dismiss();
                         finishLoadingPastMessages=true;
                         if(FMRList!=null){
                            for(int i=FMRList.size()-1;i>=0;i--)
                                addMessage(FMRList.get(i),c);
                        }
                        handler.postDelayed(r, 2000);
                     }
             }
             new RecentMessageLoader().execute();
            }
        };
        handler.post(r);
}   

UPDATE 2 : Cach class ..

public class Cach {
    static DBAdapter db;
    public Cach(Context c)
    {
    }

    public static void AddMessages(Context c,
                                  int id,
                                  int fromId,
                                  int toId,
                                  String message,
                                  String dir,
                                  String MessageTime)
    {
         db = new DBAdapter(c);
        db.open();        
        long id2;
        id2 = db.insertMessage(id, fromId, toId, message, dir,MessageTime);
        db.close();
    }
}


It seems the problem is with the type of variables you are using.. there must be Static variables of instance variables which are getting set from many sources... try not to use static variables and use local variables I mean in the methods implicitly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜