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 に答える