0

I read in some links that i need to convert my runOnUiThread to AsyncTask: Android: RunOnUiThread vs AsyncTask

But I am unable to get it done. I am implementing an AutoCompleteText which takes query from a database.

My runOnUiThread along with the new thread (it compiles):

new Thread(new Runnable() {
        public void run() {
            final DataBaseHelper dbHelper = new DataBaseHelper(ActivityName.this);
            dbHelper.openDataBase();
            item_list = dbHelper.getAllItemNames();

            ActivityName.this.runOnUiThread(new Runnable() {

                public void run() {
                    ArrayAdapter<String> sAdapter = new ArrayAdapter<String>(
                            ActivityName.this,
                            android.R.layout.simple_dropdown_item_1line,
                            item_list);
                    itemNameAct = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView_item_name);
                    itemNameAct.setAdapter(sAdapter);

                }
            });

        }
    }).start();

I put the worker thread part in doInBackground and the runOnUiThread part of code in onPostExecute but it crashes on launch.

Community
  • 1
  • 1
Rohit_D
  • 85
  • 1
  • 11
  • ohh! it got solved! I was extending AsyncTask and then trying to initialize with with new ClassName().execute(Object[]); I changed it to AsyncTask and then new ClassName().execute() and its working. – Rohit_D Jan 12 '13 at 08:33
  • I will post the class later when I am allowed access to self answer. – Rohit_D Jan 12 '13 at 08:43

1 Answers1

1

This is awkward. I asked a question and am answering it myself :/ Actually I was trying AsyncTask(Object, Void, Cursor) and it was not doing me any good.

Here is the class which is working:

class autoComplete extends AsyncTask<Void, Void, Void> {
    final DataBaseHelper dbHelper = new DataBaseHelper(ActivityName.this);

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

        dbHelper.openDataBase();
        item_list = dbHelper.getAllItemNames();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        ArrayAdapter<String> sAdapter = new ArrayAdapter<String>(
                ClassName.this, android.R.layout.simple_dropdown_item_1line,
                item_list);
        itemNameAct= (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView_item_name);
        itemNameAct.setAdapter(sAdapter);
    }

}

and then in onCreate i initialize it as:

new autoComplete().execute();
Rohit_D
  • 85
  • 1
  • 11