Problems with GridView (Android) to display data

2

I'm new to programming for Android and I'm having a problem with a situation in an application that I'm developing using the GridView with an Adapter class and a list of items (object) coming from a webservice. I would like your help.

Result of WebService bringing a list of patients with the following data: SUS Card and Name. The sus card has the patient identification function, in the bank it and primary key.

Result from post JsonPost : 200 : {"paciente":
[{"cartaoSus":"123456789","nome":"Fábio"},
{"cartaoSus":"123456789111111","nome":"Teste"},
{"cartaoSus":"22222222222","nome":"Paulo"},{"cartaoSus":"33333333333","nome":"Maria"}]}

Home

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import br.com.sistemaid.modelo.Adaptador;
import br.com.sistemaid.modelo.Paciente;
import br.com.sistemaid.webservice.ClienteREST;

public class PrincipalActivity extends Activity {

GridView gv;

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

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    listarPacientesInternados();
}

public void listarPacientesInternados() {

    ClienteREST cliREST = new ClienteREST();

    gv = (GridView) this.findViewById(R.id.monitor_grid);

    try {

        ArrayList<Paciente> pacientesAtendidos = (ArrayList<Paciente>) cliREST
                .ListarPacientes();
        gv.setAdapter(new Adaptador(this, pacientesAtendidos));

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.principal, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Adapter

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.com.sistemaid.R;

public class Adaptador extends BaseAdapter {

private Context context;
private ArrayList<Paciente> lista;

public Adaptador(Context context, ArrayList<Paciente> lista) {
    this.context = context;
    this.lista = lista;
}

@Override
public int getCount() {
    return lista.size();
}

@Override
public Paciente getItem(int posicao) {
    return lista.get(posicao);
}

@Override
public long getItemId(int posicao) {
    return posicao;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    PacienteViewHolder pacienteVH = new PacienteViewHolder();

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.activity_principal_grid, parent, false);

        pacienteVH.nomePaciente = (TextView) convertView.findViewById(R.id.textView1);
        convertView.setTag(pacienteVH);
    } else {
        pacienteVH = (PacienteViewHolder) convertView.getTag();
    }

    return convertView;

}

static class PacienteViewHolder {
    TextView nomePaciente;
}

}

Activity Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF2552"
android:orientation="vertical"
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="br.com.sistemaid.PrincipalActivity" >

<GridView
    android:id="@+id/monitor_grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="60dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp" >
</GridView>

</LinearLayout>

Row Row Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.sistemaid"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="14" />

<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".PrincipalActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

By making some modifications to the system and putting it to make printstacktrace, this error began to appear in logcat:

Myfunctionreturnsthelistofpatientsis

importjava.util.ArrayList;importjava.util.List;importbr.com.sistemaid.modelo.Paciente;importbr.com.sistemaid.webservice.WebServiceCliente;importcom.google.gson.Gson;importcom.google.gson.JsonArray;importcom.google.gson.JsonParser;publicclassPacienteControle{privatestaticfinalStringURL_WS="http://192.168.1.106:8080/ID-WebService/paciente/";

public List<Paciente> listarPacientes() throws Exception {

    String[] resposta = new WebServiceCliente().get(URL_WS
            + "listarPacientes");

    if (resposta[0].equals("200")) {
        Gson gson = new Gson();
        List<Paciente> listaPacientes = new ArrayList<Paciente>();
        JsonParser parser = new JsonParser();
        JsonArray array = parser.parse(resposta[1]).getAsJsonArray();

        for (int i = 0; i < array.size(); i++) {
            listaPacientes.add(gson.fromJson(array.get(i), Paciente.class));

        }
        return listaPacientes;

    } else {
        throw new Exception(resposta[1]);
    }
}

}

The current situation on your tablet screen appears blank. Any suggestions of what to do ??

Adapter class after fixes:

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import br.com.sistemaid.R;

public class Adaptador extends BaseAdapter {

private Context context;
private ArrayList<Paciente> lista;

public Adaptador(Context context, ArrayList<Paciente> lista) {
    this.context = context;
    this.lista = lista;
}

@Override
public int getCount() {
    return lista.size();
}

@Override
public Paciente getItem(int posicao) {
    return lista.get(posicao);
}

@Override
public long getItemId(int posicao) {
    return posicao;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    PacienteViewHolder pacienteVH = null;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_grade, parent, false);

        pacienteVH = new PacienteViewHolder();
        pacienteVH.nomePaciente =  (Button) convertView.findViewById(R.id.button1);
        convertView.setTag(pacienteVH);
    } else {
        pacienteVH = (PacienteViewHolder) convertView.getTag();
    }

    pacienteVH.nomePaciente.setText(lista.get(position).getNomePaciente());

    return convertView;
}

static class PacienteViewHolder {
    Button nomePaciente;
}

}

Patient list method

public List<Paciente> listarPacientes() throws Exception {

    String[] resposta = new WebServiceCliente().get(URL_WS
            + "listarPacientes");

    if (resposta[0].equals("200")) {
        Gson gson = new Gson();
        ArrayList<Paciente> listaPacientes = new ArrayList<Paciente>();
        JsonParser parser = new JsonParser();

        JsonArray jArray = (JsonArray) parser.parse(resposta[1]).getAsJsonObject().get("paciente");

        for (int i = 0; i < jArray.size(); i++) {
            listaPacientes.add(gson.fromJson(jArray.get(i), Paciente.class));
        }
        return listaPacientes;
    } else {
        throw new Exception(resposta[1]);
    }
}

How to make this method work?

gv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Toast.makeText(getParent(), ((Button) v.findViewById(R.id.button1)).getText(), 
                        Toast.LENGTH_SHORT);

            }   
        });

Given that this variable is simply unknown.

    
asked by anonymous 05.02.2015 / 02:28

0 answers