Pass value to TextView

3

I'm passing a value to a textview, however I have to do this after my number is given 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 looks in the php file on the internet, but when it checks to see if my array has null, it says yes, since I still do not have values in it and only then will it receive.

How do I pass the value to textview after searching the php file? I tried in many ways and I could not

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

Using this I can move to the textview.

Thank you in advance

    
asked by anonymous 30.05.2016 / 02:39

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 look 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 have changed the AsyncTask type (hence the InBackground method and onPostExecute method).

    
30.05.2016 / 03:09