Help with onitemclick listview

Asked

Viewed 91 times

3

I’m trying to get a Toast appears as soon as user chooses an option from Listview, however, when the user chooses the option, it is first loaded to Acticity and then the Toast, and what I really want is the opposite.

Follows the code:

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           LayoutInflater layoutInflater = getLayoutInflater();
                    int layout = R.layout.toast_custom;
                    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.toast_layout_root);
                    view = layoutInflater.inflate(layout, viewGroup);
                    TextView tv_texto = (TextView) view.findViewById(R.id.texto);
                    TextView slogan_text = (TextView) view.findViewById(R.id.slogan);
                    tv_texto.setText("Aguarde...");
                    slogan_text.setText("Carregando " + opcoes[position]);
                    Toast toast = new Toast(context);
          if (opcoes[position] == 1){
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(view);
                toast.show();
                Intent intent = new Intent(context, Eventos.class);
                startActivity(intent);
          }
}

How to make the first execution Toast and then start the Activity?

  • See if it helps you: http://stackoverflow.com/questions/10524836/start-activity-after-toast-message

  • 1

    Thanks @Diegofelipe exactly! Thank you.

  • @diegofm writes a reply.

1 answer

1

I believe an asyncTask solves:

if (opcoes[position] == 1){             
    new AsyncTask().execute();    
}

...

private class AsyncTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

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

            try {
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(view);
                    toast.show();
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            Intent intent = new Intent(context, Eventos.class);
            startActivity(intent);
        }
    }  

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.