1

当我按下按钮时,我试图检索 TextBox 值,但它不起作用。这是我的代码。任何的想法?

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

}

它没有显示错误,但将txt值显示为空白

4

2 回答 2

3

因为你打电话给这条线

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

在 onCreate

当您单击按钮时,txt 值正在发生变化。

txt因此,您在分配价值之前正在访问它。如果您想像这样使用值更改并尝试这样

public void onClick(View arg0) {

            et=(EditText)findViewById(R.id.text);
            txt=et.getText().toString();

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

        }

    });
于 2013-03-03T10:06:55.133 回答
0

Button在单击外部引用它。

 et=(EditText)findViewById(R.id.text);

AsynTask下图直接进入里面

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

String txt = et.getText().toString();

@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);
}
 }
于 2013-03-03T10:02:30.607 回答