Pass value to Textview

Asked

Viewed 2,238 times

3

I am passing a value to a textview, but that do so after my variable receives this value.

public class ariesFragment extends Fragment
{
public String[]textoSeparado2;

        public ariesFragment()
    {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        //executando MyAsyncTask
        MyAsyncTask myAsyncTask = new MyAsyncTask();
        myAsyncTask.execute();

        View view = inflater.inflate(R.layout.fragment_aries, container, false);
        TextView text = (TextView)view.findViewById(R.id.textAries);



        if(textoSeparado2 == null)
        {
            text.setText("ERROR VOCÊ NÃO TEM UMA CONEXÃO COM A INTERNET");
            System.out.println("erro");
        }

        else
        {
            text.setText(textoSeparado2[5]);
        }

        return view;
    }

    private class MyAsyncTask extends AsyncTask
    {
        @Override
        protected Object doInBackground(Object[] params)
        {
            HttpURLConnection conection;
            try
            {
                //Configuração de conexao
                URL url = new URL("http://meusite");
                conection = (HttpURLConnection) url.openConnection();
                conection.connect();

                // Lendo os dados

                InputStream inputStream = conection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuffer stringBuffer = new StringBuffer();

                //Storing data

                String line = new String();

                while ((line = bufferedReader.readLine())!= null)
                {
                    textoSeparado2 = line.split("\\|");
                    System.out.println(textoSeparado2[0]);
                    //System.out.println(line);
                }

                //Close Conection
                inputStream.close();
                conection.disconnect();

            }

            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }

            catch (IOException e)
            {
                e.printStackTrace();
            }


            return  null;
        }

        @Override
        protected void onPostExecute(Object o)
        {
            super.onPostExecute(o);
        }
    }
}

It searches the php file on the internet, but when it checks if my array is null, it says yes, because I still have no values in it and only then will receive.

How do I pass the value to the textview after searching the php file? I tried so many ways and I couldn’t

View view = inflater.inflate(R.layout.fragment_aries, container, false);
TextView text = (TextView)view.findViewById(R.id.textAries);

Using this I can pass to textview.

Thank you in advance

1 answer

7

You can create a method to load this Textview.

private void carregarTexto(String texto) {
    TextView text = (TextView)view.findViewById(R.id.textAries);
    text.setText(texto);
}

And your Asynctask should be like this:

private class MyAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        ...
        String texto; //Use essa variável ao invés de textoSeparado2
        ...
        //Close Conection
        inputStream.close();
        conection.disconnect();

        return texto;
    }

    @Override
    protected void onPostExecute(String result)
    {
        carregarTexto(result);
    }
}

Note that I changed the type of Asynctask (as a consequence of the Ackground and onPostExecute method).

  • I didn’t understand how to do it, I did it this way and I have many mistakes... mainly in the creation of the method for text view

Browser other questions tagged

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