mardi 26 mai 2015

How to run a Background Task in AsyncTask on android

I have been inserting data from CSV file to SQTlite database then its showing on the UI that is expandable list-view. I called getExpandableListData() method from doInBackground() method but it takes long time around 2-3 minutes and the size of CSV file is only 261 KB. Please suggest me how to reduce the processing time?
MainActivity

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<ChildInfo>>();
        databaseHelper = new DatabaseHelper(this);

        new NearBuyAsyncTask().execute();
        expListView = (ExpandableListView) findViewById(R.id.expandableListView);
        listAdapter = new ExpandableListAdapter(this, listDataHeader,
                listDataChild);
        expListView.setAdapter(listAdapter);
        synchronized (expListView) {
            expListView.notify();
        }
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                String selectedChildPhone = listDataChild
                        .get(listDataHeader.get(groupPosition))
                        .get(childPosition).getPhoneNumber();
                String phoneNo = "tel:" + selectedChildPhone.trim();
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(phoneNo));
                startActivity(intent);
                return false;
            }
        });
    }

    public void getExpandableListData() {
        Cursor cursor;
        cursor = databaseHelper.getLoadMoreData();
        cursor.moveToFirst();
        do {
            String categoryDescription = cursor.getString(cursor
                    .getColumnIndex("categorydesc"));
            int categoryId = cursor.getInt(cursor.getColumnIndex("CategoryId"));
            listDataHeader.add(categoryDescription);
            List<ChildInfo> childList = new ArrayList<ChildInfo>();
            Cursor cursorChild = databaseHelper.getChildData(categoryId);
                    cursorChild.moveToFirst();
            do {
                String businessName = cursorChild.getString(cursorChild
                        .getColumnIndex("BusinessName"));
                phoneNumber = cursorChild.getString(cursorChild
                        .getColumnIndex("Phone"));
                String landMark = cursorChild.getString(cursorChild
                        .getColumnIndex("LandMark"));
                ChildInfo childInfo = new ChildInfo(businessName, phoneNumber,
                        landMark);
                childList.add(childInfo);
                listDataChild.put(categoryDescription, childList);
            } while (cursorChild.moveToNext());

        } while (cursor.moveToNext());
        cursor.close();
    }

    public void csvToSqlite() {
        try {
            String reader = "";
            boolean skipheader = true;
            InputStream inputStream = getResources().openRawResource(
                    R.raw.kalngr);
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    inputStream));
            while ((reader = in.readLine()) != null) {
                if (skipheader) {
                    skipheader = false;
                    continue;
                }
                String[] RowData = reader.split(",");
                if (databaseHelper.insertContact(RowData) == true) {
                    Log.i("inside csvToSqlite()", " data inserted successfully");
                } else {
                    Log.i("inside csvToSqlite()",
                            " data is not inserted into db");
                }
            }
            in.close();
        } catch (Exception e) {
            e.getMessage();
        }
    }

    private class NearBuyAsyncTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Please wait...");
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

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

            csvToSqlite();
            getExpandableListData();
            if (isCancelled()) {
                return null;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            if (progressDialog.isShowing())
                progressDialog.dismiss();
            listAdapter.notifyDataSetChanged();
            super.onPostExecute(result);
        }
    }

Aucun commentaire:

Enregistrer un commentaire