0

how using 'do while' in asynctask for add to array list?Please explain mobile code,Thanks to all those who respond

code :

public List<msgstore> results =new ArrayList<msgstore>();


    AsyncTask<Void, Void, Void>task=new AsyncTask<Void, Void, Void>(){
                            ProgressDialog pd = new ProgressDialog(mContext);
                            @Override
                            protected void onPreExecute() {
                                pd.setTitle("Process");
                                pd.setMessage("Processing ....");
                                pd.setCancelable(false);
                                pd.setIndeterminate(true);
                                pd.show();
                            }


                            @Override
                            protected Void doInBackground(Void... params) {

                                do{

                                      String d=mCur.getString(mCur.getColumnIndex("address"));
                                      String p= mCur.getString(mCur.getColumnIndex("phone")); 
                                      int    k=mCur.getInt(mCur.getColumnIndex("key"));
                                      int    i=mCur.getInt(mCur.getColumnIndex("_id"));
                                      long t=mCur.getLong(mCur.getColumnIndex("registertime"));
                                      results.add(new msgstore(d,p,k,i,t));


                                  }while(mCur.moveToNext());                
                                return null;
                            }


                            @Override
                            protected void onPostExecute(Void result) {
                                pd.dismiss();
                            }
                        };
                        task.execute((Void[])null);
3
  • Can you explain what problem you are having? Commented Aug 29, 2015 at 23:34
  • @codeMagic, No working ,the crash app Commented Aug 29, 2015 at 23:48
  • 1
    If the app is crashing then you need to provide the stacktrace from the crash so we can help Commented Aug 30, 2015 at 0:11

1 Answer 1

1

I don't have enough reputation to comment, so I'll add my comment here. Where is mCur defined? You do a do/while loop using moveToNext which is very dangerous because the cursor may be empty.

Try doing a while loop instead

while(mCur.moveToNext()) {
     String d=mCur.getString(mCur.getColumnIndex("address"));
     String p= mCur.getString(mCur.getColumnIndex("phone")); 
     int    k=mCur.getInt(mCur.getColumnIndex("key"));
     int    i=mCur.getInt(mCur.getColumnIndex("_id"));
     long t=mCur.getLong(mCur.getColumnIndex("registertime"));
     results.add(new msgstore(d,p,k,i,t));
}

Also be sure to close the cursor afterwards

Sign up to request clarification or add additional context in comments.

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.