El código funciona para pasar string
, pero ¿cómo puedo cambiar el código para que en vez de tomar un string
, tome un int
llamado plata
de la base de datos y mostrarlo en un TextView
? y si no, ¿cómo puedo transformar el string
de plata
a un int
?
Intenté transformarlo pero siempre deja de funcionar la app.
Background plata
public class BackgroundWorkerPlata extends AsyncTask<String,Void,String> {
MainActivity ob;
Context context;
AlertDialog alertDialog;
BackgroundWorkerPlata (Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String plata_url = "plata page example";
if(type.equals("plata")) {
try {
String plata = params[1];
URL url = new URL(plata_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("plata","UTF-8")+"="+URLEncoder.encode(plata,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Cargando dinero");
}
@Override
protected void onPostExecute(String result) {
// alertDialog.setMessage(result);
// alertDialog.show();
if (!TextUtils.isEmpty(result) && result.contentEquals("platita")) {
ob.PlataET.setText(result.toString().replace(',', '\n'));
}
}
@Override
protected void onProgressUpdate (Void...values){
super.onProgressUpdate(values);
}
setText que transforma al TextView
public void cambiarTextoUpdatePlata(View v) {
PlataET = (TextView) findViewById(R.id.PlataET);
String plata = PlataET.getText().toString();
String type = "plata";
BackgroundWorkerPlata backgroundWorkerPlata = new BackgroundWorkerPlata(this);
backgroundWorkerPlata.execute(type, plata);
PlataET.setText("Tienes " + plata + " Pesos");
{
}
}