Listview does not appear

Asked

Viewed 532 times

0

After hours of code analysis I still can’t find the error simply does not appear my list with the schedules that are received by json ! I took code that I didn’t find necessary for error. The code is getting the json I think the problem is here :

            ListAdapter adapter = new SimpleAdapter(HorariosActivity.this, horariosList, R.layout.list_item,
                    new String[] {TAG_TITLE, TAG_DATESTART},
                    new int[] {R.id.title, R.id.dateStart});
            listaHorarios.setAdapter(adapter);

My Activity:

public class HorariosActivity extends ActionBarActivity
        implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    private static final String TAG_TITLE = "Titulo";
    private static final String TAG_DATESTART = "DataInicio";
    private PagerAdapter mPagerAdapter;
    private Context context;
    ProgressDialog dialog;
    private Toolbar mToolbar;

    private Toolbar mToolbarBottom;

    String horarios2;
    ListView  listaHorarios;


boolean estado;
    String link ;
    String varjson ="horarioHora";
    ArrayList<String> horarios = new ArrayList<>();
    ArrayList<String> linhas3 = new ArrayList<>();
    ArrayList<HashMap<String,String>> horariosList;
    private Drawer.Result navigationDrawerLeft;





    private OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(IDrawerItem iDrawerItem, CompoundButton compoundButton, boolean b) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.horarios);
        estado=true;




        btnpesquisa = (Button) findViewById(R.id.btnpesquisa);
        btnparagens = (Button) findViewById(R.id.btnparagens);


        tvCoordinate = (TextView) findViewById(R.id.Localizacaotv);
        link="http://dagobah.grifin.pt/tiagocoelho/horariosAndroid.php?linhaid=4&paragemid=36";
        new Download().execute();

        mToolbar = (Toolbar) findViewById(R.id.tb_main);
        mToolbar.setTitle("Tumg");
        mToolbar.setSubtitle("Marinha Grande");
        listaHorarios = (ListView) findViewById(R.id.listEventos);

        mToolbar.setLogo(R.drawable.ic_launcher);
        setSupportActionBar(mToolbar);


        GPSManager gps = new GPSManager(
                HorariosActivity.this);
        gps.start();
  callConnection();


    }

    public class Download extends AsyncTask<Void, Void, String> {


        protected String doInBackground(Void... params) {

            String out = null;

            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();

                String url_all_empresas =  link;

                final HttpParams httpParameters = httpClient.getParams();

                HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
                HttpConnectionParams.setSoTimeout(httpParameters, 15000);

                HttpGet httpPost = new HttpGet(url_all_empresas);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                out = EntityUtils.toString(httpEntity, HTTP.UTF_8);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return out;
        }
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            estado = true;
            dialog = ProgressDialog.show(HorariosActivity.this, "A autenticar", "A contactar o servidor, por favor, aguarde alguns instantes.", true, false);
        }


        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @Override
        public void onPostExecute(final String result) {
            super.onPostExecute(result);


            try {
                JSONArray jsonArray = new JSONArray(result);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsa = jsonArray.getJSONObject(i);
                    horarios2= jsa.getString(varjson);
                    dialog.dismiss();

                    String estado= jsa.getString("horarioEstado");

                    HashMap<String, String> evento = new HashMap<>();
                    evento.put("horarioHora", horarios2);
                    evento.put("horarioEstado", estado);
                    horariosList.add(evento);


                }



                ListAdapter adapter = new SimpleAdapter(HorariosActivity.this, horariosList, R.layout.list_item,
                        new String[] {TAG_TITLE, TAG_DATESTART},
                        new int[] {R.id.title, R.id.dateStart});
                listaHorarios.setAdapter(adapter);
                estado = false;




            } catch (Exception e) {
                e.printStackTrace();
            }



        }
    }










    }





}

This is my xml schedule.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">
    <android.support.v7.widget.Toolbar
        android:id="@+id/tb_main"

        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="#82d808" />
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/listEventos"
        android:layout_alignParentTop="true"

        android:layout_margin="15dp"
        tools:listitem="@layout/list_item"/>
</RelativeLayout>

My list_item.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="dateStart"
        android:id="@+id/dateStart"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="15dp"
        android:textSize="15dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="title"
        android:id="@+id/title"
        android:layout_marginLeft="15dp"
        android:layout_marginBottom="15dp"
        android:textSize="20dp" />
</LinearLayout>
  • There is no error ? The map is being populated right ?

  • What do you mean map is being populated? There is no mistake

  • From what I understand you transform the received data in JSON into a Hashmap and insert them into a list ? This map is getting the data correctly ? Maybe by debugging it you’ll notice if the key and value are in accordance with what you expect when mounting the Adapter

1 answer

1

Fala Tiago,

The problem is when you declare these TAGS, they are not being identified by Simpleadapter.

I did a test here and it worked, declare your To and From as follows:

String[] from = new String[] {TAG_TITLE, TAG_DATESTART};
int[] to = new int[] { R.id.title, R.id.dateStart};

With this, you define that all titles will be in the id R.id.title and the dates will be in the id R.id.dateStart.

At the time of looping, where you mount the Map, do it this way:

List<HashMap<String, String>> horariosList = new ArrayList<>();
   for(int i = 0; i < jsonArray.length(); i++){
      HashMap<String, String> map = new HashMap<>();
      map.put(TAG_TITLE, horarios2);
      map.put(TAG_DATESTART, estado);
      horariosList.add(map);
   }

That way, you put each element in its proper place.

And finally, when it comes to declaring your Adapter, do so:

ListAdapter adapter = new SimpleAdapter(MainActivity.this, horariosList, R.layout.list_item, from, to);
listaHorarios.setAdapter(adapter);

If it doesn’t work, let me know and I’ll help you.

Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.