解析值保存在edittext字段android中

时间:2012-09-06 06:27:56

标签: java android parameter-passing adapter

在这里,我必须成功解析从上一个活动到下一个活动的价值...现在我希望在我的edittext字段中保存该值...我怎样才能开发这个...

在我的代码中,必须从以前的活动中解析orderid值并显示在此活动中。其中orderid值自动保存在我的edittext字段中...我该怎么办...

在我之前的活动代码中:

 Button btninsert = (Button) findViewById(R.id.btn_insert);
btninsert.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) {
     String s= getIntent().getStringExtra("orderid");
Intent i = new Intent(getApplicationContext(), InsertionExample.class);
i.putExtra(KEY_NAME, s);
startActivity(i);
}
});

这是我的InsertionExample.java类:

public class InsertionExample extends Activity {
private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://192.168.1.168:8089/XcartLogin/services/update?wsdl";
private final String SOAP_ACTION = "http://xcart.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
String selectedItem;

static final String KEY_NAME = "orderid";
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.change_status);
    Intent in = getIntent();

    // Get XML values from previous intent
    String orderid = in.getStringExtra(KEY_NAME);

    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.textView1);


    lblName.setText(orderid);


    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    btninsert = (Button)findViewById(R.id.btn_insert1);
    btninsert.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText Orderid = (EditText) findViewById(R.id.orderid);
            String orderid = Orderid.getText().toString();
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            PropertyInfo unameProp =new PropertyInfo();
            unameProp.setName("Status");//Define the variable name in the web service method
            unameProp.setValue(selectedItem);//Define value for fname variable
            unameProp.setType(String.class);//Define the type of the variable

            request.addProperty(unameProp);
            PropertyInfo idProp =new PropertyInfo();
            idProp.setName("Orderid");//Define the variable name in the web service method
            idProp.setValue(orderid);//Define value for fname variable
            idProp.setType(String.class);//Define the type of the variable
            request.addProperty(idProp);



              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              envelope.setOutputSoapObject(request);
              HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

              try{
               androidHttpTransport.call(SOAP_ACTION, envelope);
                  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

                 TextView result = (TextView) findViewById(R.id.textView2);
                  result.setText(response.toString());
             }
           catch(Exception e){

           }
              }
    });

    //attach the listener to the spinner
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    //Dynamically generate a spinner data 
    createSpinnerDropDown();

}

//Add animals into spinner dynamically
private void createSpinnerDropDown() {

    //get reference to the spinner from the XML layout
    Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    //Array list of animals to display in the spinner
    List<String> list = new ArrayList<String>();

    list.add("Q");
    list.add("P");
    list.add("F");
    list.add("I");
    list.add("C");

    //create an ArrayAdaptar from the String Array
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    //set the view for the Drop down list
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //set the ArrayAdapter to the spinner
    spinner.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    //attach the listener to the spinner
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

}

public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

         selectedItem = parent.getItemAtPosition(pos).toString();

 }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }



    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Do nothing.
    }
}

这里我使用下面的代码在edittext字段上设置orderid:idProp.setValue(orderid); /

            EditText Orderid = (EditText) findViewById(R.id.orderid);
            String orderid = Orderid.getText().toString();
            PropertyInfo idProp =new PropertyInfo();
            idProp.setName("Orderid");//Define the variable name in the web service method
            idProp.setValue(orderid);//Define value for fname variable
            idProp.setType(String.class);//Define the type of the variable
            request.addProperty(idProp);`

但是我希望edittext字段必须自动输入我解析的orderid。 (即)

Intent in = getIntent();

    // Get XML values from previous intent
    String orderid = in.getStringExtra(KEY_NAME);

    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.textView1);


    lblName.setText(orderid);

此orderid自动进入edittext框...我该怎么办.. 请帮帮我...给我一些想法。

2 个答案:

答案 0 :(得分:0)

想要将值发送给其他人的活动会这样做.....

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

    public void onClick(View arg0) {
        Intent intent = new Intent(PutExtra.this, getExtra.class);
        intent.putExtra("text", et.getText().toString());
        startActivity(intent);
    }
});

正在调用的活动和想要的值必须这样做

override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.getextra);

        EditText tv = (EditText)findViewById(R.id.tv);

        Intent intent = getIntent();
        String extra = intent.getStringExtra("text");      
        tv.setText(extra);
    }

答案 1 :(得分:0)

使用startActivityForResult(Intent i,key k)而不是使用startActivity(Intent i)。一旦你完成了你的活动(call finish())并返回到root活动,你将在onActivityResult()中捕获'result'。

如果您需要更多信息http://android-er.blogspot.com/2011/08/return-result-to-onactivityresult.html

,请查看此链接
相关问题