Error adding ListView

0

I'm bringing the results of Mysql on Android with TextView like this:

public class ListarDados extends AppCompatActivity {

    public static TextView data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listar_dados);

        data = (TextView) findViewById(R.id.textView);

        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if(networkInfo != null && networkInfo.isConnected()){

            new SolicitaDados().execute();

        }else {
            Toast.makeText(getApplicationContext(), "Nenhuma conexão ativa", Toast.LENGTH_LONG).show();
        }
    }

    private class SolicitaDados extends AsyncTask<Void, Void, Void> {

        String data = "";
        String dataParsed = "";
        String singleParsed = "";

        @Override
        protected Void doInBackground(Void... voids){
            try {
                URL url = new URL("http://192.168.0.13/plataforma/android/listar.php");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                String linhas = "";

                while(linhas != null){
                    linhas = bufferedReader.readLine();
                    data = data + linhas;
                }

                JSONArray JA = new JSONArray(data);

                for(int i = 0; i < JA.length(); i++){
                    JSONObject JO = (JSONObject) JA.get(i);
                    singleParsed = "Email:" + JO.get("Email") + "Senha:" + JO.get("Senha") + "\n";
                    dataParsed = dataParsed + singleParsed + "\n";
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            ListarDados.data.setText(this.dataParsed);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Intent voltar = new Intent(ListarDados.this, ConteudoSistema.class);
        startActivity(voltar);
    }
}

But when I retrieve the TextView and include the ListView, the line below gives an error. How can I fix this?

Thecodelookslikethis:

importandroid.content.Context;importandroid.content.Intent;importandroid.net.ConnectivityManager;importandroid.net.NetworkInfo;importandroid.os.AsyncTask;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.widget.ListView;importandroid.widget.TextView;importandroid.widget.Toast;importorg.json.JSONArray;importorg.json.JSONException;importorg.json.JSONObject;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.MalformedURLException;importjava.net.URL;publicclassListarDadosextendsAppCompatActivity{//publicstaticTextViewdata;privateListViewdata;//Stringurl=""; //

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listar_dados);

       /// data = (TextView) findViewById(R.id.textView);

        data = (ListView) findViewById(R.id.listView);

        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if(networkInfo != null && networkInfo.isConnected()){

            new SolicitaDados().execute();

        }else {
            Toast.makeText(getApplicationContext(), "Nenhuma conexão ativa", Toast.LENGTH_LONG).show();
        }
    }

    private class SolicitaDados extends AsyncTask<Void, Void, Void> {

        String data = "";
        String dataParsed = "";
        String singleParsed = "";

        @Override
        protected Void doInBackground(Void... voids){
            try {
                URL url = new URL("http://192.168.0.13/plataforma/android/listar.php");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                String linhas = "";

                while(linhas != null){
                    linhas = bufferedReader.readLine();
                    data = data + linhas;
                }

                JSONArray JA = new JSONArray(data);

                for(int i = 0; i < JA.length(); i++){
                    JSONObject JO = (JSONObject) JA.get(i);
                    singleParsed = "Email:" + JO.get("Email") + "Senha:" + JO.get("Senha") + "\n";
                    dataParsed = dataParsed + singleParsed + "\n";
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            // O erro ocorre nessa linha!
            ListarDados.data.setText(this.dataParsed);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Intent voltar = new Intent(ListarDados.this, ConteudoSistema.class);
        startActivity(voltar);
    }
}

And the XML looks like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ListarDados" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </ListView>

</LinearLayout>
    
asked by anonymous 01.02.2018 / 16:35

1 answer

2

The way you are trying to work with ListView is wrong, because you actually have to insert an Adapter and it has to be a String Array, here's an example below:

    String[] itens = {
            "item1",
            "item2",
            "item3",
            "item4",
            "item5"};

data.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, itens));
    
01.02.2018 / 17:38