0
I’m bringing the results of Mysql on Android with Textview this way:
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 remove Textview and include Listview, the line below gives error. How can I fix this?
The code is that way:
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);
}
}
And XML is that way:
<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>
Hello Alan. Includes the example within
protected void onPostExecute(Void aVoid)
, howeverdata
still red.– user24136
Appears
Non-static field 'data' cannot be referenced from a static context
– user24136
Change the Listview name, put mListView just to test, there is another local variable within the Asynctask class that also calls date.
– Alan Vargas
I changed it. It’s still red....
– user24136
I got... I changed the Static public line
ListView
date;– user24136
Thank you Alan. I couldn’t have done it without you.
– user24136
You’re welcome, we need you =)
– Alan Vargas