Estou trazendo os resultados do Mysql no Android com TextView dessa forma:
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);
}
}
Porém quando retiro o TextView e incluo o ListView, a linha abaixo dá erro. Como posso corrigir isso?
O código está dessa forma:
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class ListarDados extends AppCompatActivity {
//public static TextView data;
private ListView data;
//String url = ""; //
@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);
}
}
E o XML está dessa forma:
<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>