Search code examples
javaandroidbuttontextedit

Reading from textbox when button is pushed in android


I'm trying to retrieve the TextBox value when I press the button, but it does not work. Here is my code. Any idea?

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.httpxml);
    httpstuff  = (TextView) findViewById(R.id.http);
    client = new DefaultHttpClient();
    button = (Button)findViewById(R.id.shoppingprice);
    button.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

                //shoppingapi price =  new shoppingapi();
                et=(EditText)findViewById(R.id.text);
                txt=et.getText().toString();

            }

        });


   new Read().execute("displayprice");
}

@SuppressLint("ShowToast")
public JSONObject productprice(String productname) throws ClientProtocolException,IOException,JSONException
{

    StringBuilder url = new StringBuilder(URL);
    url.append(productname);
    url.append("&searchType=keyword&contentType=json");

    HttpGet get = new HttpGet(url.toString());

    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();

    Log.d("Price", "asdasd");

    if(status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
         jObj = new JSONObject(data);
        JSONObject jsonData = jObj.getJSONObject("mercadoresult");
        JSONObject jsonProducts = jsonData.getJSONObject("products");
        JSONArray jsonArray = jsonProducts.getJSONArray("product");
        jsonArray = (JSONArray) jsonArray.get(1);

        jObj = (JSONObject)jsonArray.get(0);

        return jObj;
        }
    else
    {
    Toast.makeText(MainActivity.this,"error",Toast.LENGTH_LONG).show();

    return null;    

  }
}

public class Read extends AsyncTask<String,Integer,String>
{


    @Override
    protected String doInBackground(String... params) {

        try {

            json = productprice(txt); 

            return json.getString("displayprice");

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        }

        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        //super.onPostExecute(result);

        //httpstuff.setText("The price of the Product is ");
        httpstuff.setText(result);
        httpstuff.setText(txt);
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

It shows no error but it shows txt value as blank


Solution

  • Because you are calling this line

    new Read().execute("displayprice");
    

    in onCreate

    Where as txt value is changing when you click on button.

    So you are accessing txt value before assigning it. if you want to use the value change like this and try like this

    public void onClick(View arg0) {
    
                et=(EditText)findViewById(R.id.text);
                txt=et.getText().toString();
    
                new Read().execute("displayprice");
    
            }
    
        });