I have 2 buttons where when clicking I will request information from the server and receive it in a list.
The problem is the order because when I first click on the button it does not give me the data I want in the immediate list of the data to be stored in the array.
Example if clicking Button 1 the goal was to receive:
- 1
- 2
- 3
Clicking on button 2 meant to receive:
- A
- B
- C
What happens is that we imagine that I had clicked the button 1 previously but at the moment I am clicking the button 2 I am receiving the numbers but if I click again on the button 2 I already 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 do 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();
}
}
}
Download AsyncTask 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;
}