U
U
Ulyan Romanov2015-08-04 18:43:22
Java
Ulyan Romanov, 2015-08-04 18:43:22

How to output result of AsyncTask in second Activity?

I am newbie. I use Jsoup for parsing the page (so far I'm just trying to pull out the group number) and AsyncTask for splitting threads, I'll have to parse enough. I have 2 Activities and 2 classes for them. One class inherits from another (is it right to do so?). Inside one class, there is another one that inherits from AsyncTask. Here I compare the group entered by the user with the available data in the array (as I understand it, it’s better to do this in a separate thread when pressed?), Then it gives the result (the same group and gives out) in the second Activity (This gives out normally). And AsyncTask connects to the page, looks for a tag with a class and must pass the text to the second activity. What's wrong? Read how to use AsyncTask, onPostExecute - UI thread, to access elements; doInBackground - a separate thread for performing a complex operation.

public class SearchGroup extends MainActivity implements TextWatcher {
        private AutoCompleteTextView group;
        protected String[] spisokGrp = {"ИВ-14-1", "ИВ-14-2", "ИВ-14-21", "ИВ-201", "ИВ-202",
                "ИВ-301", "ИВ-302", "ИВ-401", "ИВ-402", "ИП-14-22", "ИП-14-3", "ИП-14-4", "ИП-203",
                "ИП-204", "ИП-303", "ИП-304", "ИП-312", "ИП-403", "ИП-404", "ИП-411", "РА-14-5", "РА-14-6",
                "РА-205", "РА-305", "РА-314", "РА-405", "РА-414", "РЭ-14-7С", "СР-14-12", "СР-14-13",
                "СР-208", "СР-308", "СР-408", "ТМ-14-9", "ТМ-206", "ТМ-306", "ТМ-406", "ТО-14-8", "ТО-210", "ТО-310",
                "ТО-410", "УД-14-11", "УД-211", "УД-213К", "УК-14-10", "УК-207", "УК-307", "УК-311", "УК-407",
                "ЭЭ-14-23", "ЭЭ-313", "ЮС-14-14", "ЮС-14-15К", "ЮС-14-16К", "ЮС-209", "ЮС-212К", "ЮС-309"};
        protected Intent intent;
        private String groupName;
        private searchGroup sG =new searchGroup();;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.srchgroup);
            group = (AutoCompleteTextView) findViewById(R.id.editText);
            group.addTextChangedListener(this);
            group.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, spisokGrp));
            /**button.setOnClickListener(this);*/
        }
        class searchGroup extends AsyncTask<Void, Void, String> {
            @Override
            protected String doInBackground(Void... params) {
                groupName = group.getText().toString().toUpperCase();
                Document doc = null;
                Elements nameGrp;
                try {
                    doc = Jsoup.connect("http://pkgh.edu.ru/obuchenie/shedule-of-classes.html").get();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (doc != null) {
                    nameGrp = doc.select("h4.expanded");
                    return nameGrp.text();
                }
                return null;
            }
            protected void onPostExecute(String result) {
                intent.putExtra("nameGroup", result);
            }
        }
        public void click(View view) {
            int i;
            boolean bool;
            sG.execute();
            groupName = group.getText().toString().toUpperCase();
            intent = new Intent(this, MainActivity.class);
            bool = false;
            do {
                for (i = 0; i < spisokGrp.length; i++) {
                    if (groupName.equals(spisokGrp[i])) {
                        groupName = spisokGrp[i];
                        bool = true;
                        intent.putExtra("group", groupName);
                        startActivity(intent);
                    } else {
                        Toast.makeText(this, "Неверный ввод", Toast.LENGTH_SHORT).show();
                        bool = true;
                    }
                }
            } while (!bool);
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }
    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2015-08-05
@Dit81

Maybe it's better to write like this?

protected void onPostExecute(String result) {
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    intent.putExtra("nameGroup", result);
}

I
IceJOKER, 2015-08-04
@IceJOKER

I'm too lazy to read your code, but I'll explain the principle:
You declare an interface with the taskCompleted(ResultType result());
Create a MyAsyncTask class (a descendant of AsyncTask), add a field for the interface:
add a constructor where you accept an activity (must implement the interface created above) and cast it to the interface type:
in onPostExecution:
task.taskCompleted(result);

Z
z0rgoyok, 2015-08-04
@z0rgoyok

The text in the activity should not be transferred, you should use the database, you can try ActiveAndroid.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question