0

代码不起作用。请帮忙。除数量外,该项目被添加到数据库中。它始终为零。这是为什么?

String value;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.creamy);

    EditText et = (EditText) findViewById(R.id.CreamyEdit);
    value = et.getText().toString();

    Button b = (Button) findViewById(R.id.buttonCreamy);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(Creamy.this, "Item Submitted", Toast.LENGTH_LONG).show();

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(
                    "http://10.0.2.2:8080/http/test.php");

            List<NameValuePair> pairs = new ArrayList<NameValuePair>();


       pairs.add(new BasicNameValuePair("item", "Creamy Delight"));   //reflected in db
       pairs.add(new BasicNameValuePair("quantity", value));          //not reflected in db


        try {
                post.setEntity(new UrlEncodedFormEntity(pairs));
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {
                HttpResponse response = client.execute(post);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }
        }

    });

请帮忙!

4

1 回答 1

0

将线value = et.getText().toString();移到onClick方法中。如果您调用et.getTextonCreate,它总是返回一个空字符串,因为这就是 onCreate 时 EditText 内部的全部内容。

于 2013-04-29T19:04:27.120 回答