ListView does not appear

0

After hours of code analysis I still can not find the error, it just does not appear on my list with the schedules that are received by json! I took code that I did not find necessary for the error. The code is getting 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();
            }



        }
    }










    }





}

My schedules.xml

    <?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>
    
asked by anonymous 08.06.2016 / 19:41

1 answer

1

Speak James,

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 stay in the R.id.title id and the dates will stay in the R.id.dateStart id.

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

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);
   }

In this way, you put each element in its proper place.

And finally, when declaring your adapter, do this:

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

If it does not work, let me know if I can help.

Hugs.

    
08.06.2016 / 20:36