Problems with onclick

Asked

Viewed 54 times

1

I have 2 buttons where when clicking I will ask for information from the server and receive in a list.

The problem is the order because when I click the button for the first time it does not give me the data I want in the immediate list of me yes the data that was stored in the array previously.

Example if you click on Button 1 the goal was to receive :

  • 1
  • 2
  • 3

By clicking the 2 button the goal was to receive :

  • To
  • B
  • C

what happens is that we imagine that I had clicked on button 1 before but at the moment I am clicking on button 2 I am receiving the numbers but if you click on button 2 again I find myself receiving the letters is a practical example I will put the code below my buttons :

public class btnpesquisaclicker implements Button.OnClickListener {

        @Override
        public void onClick(View v) {
            link = "http://dagobah.grifin.pt/tiagocoelho/oi.php";
            varjson="linhaNome";
            new Download().execute();

            AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
            b.setTitle(linhas2);

            b.setItems((linhas.toArray(new String[linhas.size()])), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                    int j = 0;

                        for (j = 0; j <= linhas.size(); j++) {
                            if (j == which) {

                                btncoordenadas.setText(linhas.get(j));
                                link = "http://dagobah.grifin.pt/tiagocoelho/tumglinhas.php";

                            }
                        }

                    }
                });
                b.show();

        }
    }
    public class btnparagensclicker implements Button.OnClickListener
    {
        @Override
        public void onClick(View v){
            link = "http://dagobah.grifin.pt/tiagocoelho/Conexaotumg.php";
            varjson="linhaId";
            new Download().execute();


            AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
            b.setTitle(linhas2);

            b.setItems((linhas.toArray(new String[linhas.size()])), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                    int j = 0;

                    for (j = 0; j <= linhas.size(); j++) {
                        if (j == which) {

                            btncoordenadas.setText(linhas.get(j));


                        }
                    }

                }
            });
            b.show();
        }
    }

Where will I get the data :

 @Override
    protected void onPreExecute()
    {
        super.onPreExecute();

       dialog = ProgressDialog.show(MainActivity.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);
    }


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

linhas.clear();

            dialog.dismiss();

            try {
                JSONArray jsonArray = new JSONArray(result);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsa = jsonArray.getJSONObject(i);
                   linhas2= jsa.getString(varjson);
                    linhas.add(linhas2);
                    btncoordenadas.setText(linhas2);

                }






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

Asynctask Download function :

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


        protected String doInBackground(Void... params) {

            URL url = null;
            try {
                url = new URL( link);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            HttpURLConnection conn = null;
            try {
                conn = (HttpURLConnection) url.openConnection();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
                    InputStream is = conn.getInputStream();

                }else{
                    InputStream err = conn.getErrorStream();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            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;
        }
No answers

Browser other questions tagged

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